| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 #include "spaces-inl.h" | 36 #include "spaces-inl.h" |
| 37 #include "token.h" | 37 #include "token.h" |
| 38 | 38 |
| 39 namespace v8 { | 39 namespace v8 { |
| 40 namespace internal { | 40 namespace internal { |
| 41 | 41 |
| 42 // A simple json parser. | 42 // A simple json parser. |
| 43 template <bool seq_ascii> | 43 template <bool seq_ascii> |
| 44 class JsonParser BASE_EMBEDDED { | 44 class JsonParser BASE_EMBEDDED { |
| 45 public: | 45 public: |
| 46 static Handle<Object> Parse(Handle<String> source, Zone* zone) { | 46 static Handle<Object> Parse(Handle<String> source) { |
| 47 return JsonParser().ParseJson(source, zone); | 47 return JsonParser(source).ParseJson(); |
| 48 } | 48 } |
| 49 | 49 |
| 50 static const int kEndOfString = -1; | 50 static const int kEndOfString = -1; |
| 51 | 51 |
| 52 private: | 52 private: |
| 53 explicit JsonParser(Handle<String> source) |
| 54 : source_(source), |
| 55 source_length_(source->length()), |
| 56 isolate_(source->map()->GetHeap()->isolate()), |
| 57 factory_(isolate_->factory()), |
| 58 zone_(isolate_), |
| 59 object_constructor_(isolate_->native_context()->object_function(), |
| 60 isolate_), |
| 61 position_(-1) { |
| 62 FlattenString(source_); |
| 63 pretenure_ = (source_length_ >= kPretenureTreshold) ? TENURED : NOT_TENURED; |
| 64 |
| 65 // Optimized fast case where we only have ASCII characters. |
| 66 if (seq_ascii) { |
| 67 seq_source_ = Handle<SeqOneByteString>::cast(source_); |
| 68 } |
| 69 } |
| 70 |
| 53 // Parse a string containing a single JSON value. | 71 // Parse a string containing a single JSON value. |
| 54 Handle<Object> ParseJson(Handle<String> source, Zone* zone); | 72 Handle<Object> ParseJson(); |
| 55 | 73 |
| 56 inline void Advance() { | 74 inline void Advance() { |
| 57 position_++; | 75 position_++; |
| 58 if (position_ >= source_length_) { | 76 if (position_ >= source_length_) { |
| 59 c0_ = kEndOfString; | 77 c0_ = kEndOfString; |
| 60 } else if (seq_ascii) { | 78 } else if (seq_ascii) { |
| 61 c0_ = seq_source_->SeqOneByteStringGet(position_); | 79 c0_ = seq_source_->SeqOneByteStringGet(position_); |
| 62 } else { | 80 } else { |
| 63 c0_ = source_->Get(position_); | 81 c0_ = source_->Get(position_); |
| 64 } | 82 } |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 | 190 |
| 173 // Mark that a parsing error has happened at the current token, and | 191 // Mark that a parsing error has happened at the current token, and |
| 174 // return a null handle. Primarily for readability. | 192 // return a null handle. Primarily for readability. |
| 175 inline Handle<Object> ReportUnexpectedCharacter() { | 193 inline Handle<Object> ReportUnexpectedCharacter() { |
| 176 return Handle<Object>::null(); | 194 return Handle<Object>::null(); |
| 177 } | 195 } |
| 178 | 196 |
| 179 inline Isolate* isolate() { return isolate_; } | 197 inline Isolate* isolate() { return isolate_; } |
| 180 inline Factory* factory() { return factory_; } | 198 inline Factory* factory() { return factory_; } |
| 181 inline Handle<JSFunction> object_constructor() { return object_constructor_; } | 199 inline Handle<JSFunction> object_constructor() { return object_constructor_; } |
| 182 inline Zone* zone() const { return zone_; } | |
| 183 | 200 |
| 184 static const int kInitialSpecialStringLength = 1024; | 201 static const int kInitialSpecialStringLength = 1024; |
| 185 static const int kPretenureTreshold = 100 * 1024; | 202 static const int kPretenureTreshold = 100 * 1024; |
| 186 | 203 |
| 187 | 204 |
| 188 private: | 205 private: |
| 206 Zone* zone() { return &zone_; } |
| 207 |
| 189 Handle<String> source_; | 208 Handle<String> source_; |
| 190 int source_length_; | 209 int source_length_; |
| 191 Handle<SeqOneByteString> seq_source_; | 210 Handle<SeqOneByteString> seq_source_; |
| 192 | 211 |
| 193 PretenureFlag pretenure_; | 212 PretenureFlag pretenure_; |
| 194 Isolate* isolate_; | 213 Isolate* isolate_; |
| 195 Factory* factory_; | 214 Factory* factory_; |
| 215 Zone zone_; |
| 196 Handle<JSFunction> object_constructor_; | 216 Handle<JSFunction> object_constructor_; |
| 197 uc32 c0_; | 217 uc32 c0_; |
| 198 int position_; | 218 int position_; |
| 199 Zone* zone_; | |
| 200 }; | 219 }; |
| 201 | 220 |
| 202 template <bool seq_ascii> | 221 template <bool seq_ascii> |
| 203 Handle<Object> JsonParser<seq_ascii>::ParseJson(Handle<String> source, | 222 Handle<Object> JsonParser<seq_ascii>::ParseJson() { |
| 204 Zone* zone) { | |
| 205 isolate_ = source->map()->GetHeap()->isolate(); | |
| 206 factory_ = isolate_->factory(); | |
| 207 object_constructor_ = Handle<JSFunction>( | |
| 208 isolate()->native_context()->object_function(), isolate()); | |
| 209 zone_ = zone; | |
| 210 FlattenString(source); | |
| 211 source_ = source; | |
| 212 source_length_ = source_->length(); | |
| 213 pretenure_ = (source_length_ >= kPretenureTreshold) ? TENURED : NOT_TENURED; | |
| 214 | |
| 215 // Optimized fast case where we only have ASCII characters. | |
| 216 if (seq_ascii) { | |
| 217 seq_source_ = Handle<SeqOneByteString>::cast(source_); | |
| 218 } | |
| 219 | |
| 220 // Set initial position right before the string. | |
| 221 position_ = -1; | |
| 222 // Advance to the first character (possibly EOS) | 223 // Advance to the first character (possibly EOS) |
| 223 AdvanceSkipWhitespace(); | 224 AdvanceSkipWhitespace(); |
| 224 Handle<Object> result = ParseJsonValue(); | 225 Handle<Object> result = ParseJsonValue(); |
| 225 if (result.is_null() || c0_ != kEndOfString) { | 226 if (result.is_null() || c0_ != kEndOfString) { |
| 226 // Some exception (for example stack overflow) is already pending. | 227 // Some exception (for example stack overflow) is already pending. |
| 227 if (isolate_->has_pending_exception()) return Handle<Object>::null(); | 228 if (isolate_->has_pending_exception()) return Handle<Object>::null(); |
| 228 | 229 |
| 229 // Parse failed. Current character is the unexpected token. | 230 // Parse failed. Current character is the unexpected token. |
| 230 const char* message; | 231 const char* message; |
| 231 Factory* factory = this->factory(); | 232 Factory* factory = this->factory(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 257 default: | 258 default: |
| 258 message = "unexpected_token"; | 259 message = "unexpected_token"; |
| 259 Handle<Object> name = | 260 Handle<Object> name = |
| 260 LookupSingleCharacterStringFromCode(isolate_, c0_); | 261 LookupSingleCharacterStringFromCode(isolate_, c0_); |
| 261 Handle<FixedArray> element = factory->NewFixedArray(1); | 262 Handle<FixedArray> element = factory->NewFixedArray(1); |
| 262 element->set(0, *name); | 263 element->set(0, *name); |
| 263 array = factory->NewJSArrayWithElements(element); | 264 array = factory->NewJSArrayWithElements(element); |
| 264 break; | 265 break; |
| 265 } | 266 } |
| 266 | 267 |
| 267 MessageLocation location(factory->NewScript(source), | 268 MessageLocation location(factory->NewScript(source_), |
| 268 position_, | 269 position_, |
| 269 position_ + 1); | 270 position_ + 1); |
| 270 Handle<Object> result = factory->NewSyntaxError(message, array); | 271 Handle<Object> result = factory->NewSyntaxError(message, array); |
| 271 isolate()->Throw(*result, &location); | 272 isolate()->Throw(*result, &location); |
| 272 return Handle<Object>::null(); | 273 return Handle<Object>::null(); |
| 273 } | 274 } |
| 274 return result; | 275 return result; |
| 275 } | 276 } |
| 276 | 277 |
| 277 | 278 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 } | 317 } |
| 317 | 318 |
| 318 | 319 |
| 319 // Parse a JSON object. Position must be right at '{'. | 320 // Parse a JSON object. Position must be right at '{'. |
| 320 template <bool seq_ascii> | 321 template <bool seq_ascii> |
| 321 Handle<Object> JsonParser<seq_ascii>::ParseJsonObject() { | 322 Handle<Object> JsonParser<seq_ascii>::ParseJsonObject() { |
| 322 HandleScope scope(isolate()); | 323 HandleScope scope(isolate()); |
| 323 Handle<JSObject> json_object = | 324 Handle<JSObject> json_object = |
| 324 factory()->NewJSObject(object_constructor(), pretenure_); | 325 factory()->NewJSObject(object_constructor(), pretenure_); |
| 325 Handle<Map> map(json_object->map()); | 326 Handle<Map> map(json_object->map()); |
| 326 ZoneScope zone_scope(zone(), DELETE_ON_EXIT); | |
| 327 ZoneList<Handle<Object> > properties(8, zone()); | 327 ZoneList<Handle<Object> > properties(8, zone()); |
| 328 ASSERT_EQ(c0_, '{'); | 328 ASSERT_EQ(c0_, '{'); |
| 329 | 329 |
| 330 bool transitioning = true; | 330 bool transitioning = true; |
| 331 | 331 |
| 332 AdvanceSkipWhitespace(); | 332 AdvanceSkipWhitespace(); |
| 333 if (c0_ != '}') { | 333 if (c0_ != '}') { |
| 334 do { | 334 do { |
| 335 if (c0_ != '"') return ReportUnexpectedCharacter(); | 335 if (c0_ != '"') return ReportUnexpectedCharacter(); |
| 336 | 336 |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 } | 462 } |
| 463 } | 463 } |
| 464 AdvanceSkipWhitespace(); | 464 AdvanceSkipWhitespace(); |
| 465 return scope.CloseAndEscape(json_object); | 465 return scope.CloseAndEscape(json_object); |
| 466 } | 466 } |
| 467 | 467 |
| 468 // Parse a JSON array. Position must be right at '['. | 468 // Parse a JSON array. Position must be right at '['. |
| 469 template <bool seq_ascii> | 469 template <bool seq_ascii> |
| 470 Handle<Object> JsonParser<seq_ascii>::ParseJsonArray() { | 470 Handle<Object> JsonParser<seq_ascii>::ParseJsonArray() { |
| 471 HandleScope scope(isolate()); | 471 HandleScope scope(isolate()); |
| 472 ZoneScope zone_scope(zone(), DELETE_ON_EXIT); | |
| 473 ZoneList<Handle<Object> > elements(4, zone()); | 472 ZoneList<Handle<Object> > elements(4, zone()); |
| 474 ASSERT_EQ(c0_, '['); | 473 ASSERT_EQ(c0_, '['); |
| 475 | 474 |
| 476 AdvanceSkipWhitespace(); | 475 AdvanceSkipWhitespace(); |
| 477 if (c0_ != ']') { | 476 if (c0_ != ']') { |
| 478 do { | 477 do { |
| 479 Handle<Object> element = ParseJsonValue(); | 478 Handle<Object> element = ParseJsonValue(); |
| 480 if (element.is_null()) return ReportUnexpectedCharacter(); | 479 if (element.is_null()) return ReportUnexpectedCharacter(); |
| 481 elements.Add(element, zone()); | 480 elements.Add(element, zone()); |
| 482 } while (MatchSkipWhiteSpace(',')); | 481 } while (MatchSkipWhiteSpace(',')); |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 801 | 800 |
| 802 ASSERT_EQ('"', c0_); | 801 ASSERT_EQ('"', c0_); |
| 803 // Advance past the last '"'. | 802 // Advance past the last '"'. |
| 804 AdvanceSkipWhitespace(); | 803 AdvanceSkipWhitespace(); |
| 805 return result; | 804 return result; |
| 806 } | 805 } |
| 807 | 806 |
| 808 } } // namespace v8::internal | 807 } } // namespace v8::internal |
| 809 | 808 |
| 810 #endif // V8_JSON_PARSER_H_ | 809 #endif // V8_JSON_PARSER_H_ |
| OLD | NEW |