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: |
189 Handle<String> source_; | 206 Handle<String> source_; |
190 int source_length_; | 207 int source_length_; |
191 Handle<SeqOneByteString> seq_source_; | 208 Handle<SeqOneByteString> seq_source_; |
192 | 209 |
193 PretenureFlag pretenure_; | 210 PretenureFlag pretenure_; |
194 Isolate* isolate_; | 211 Isolate* isolate_; |
195 Factory* factory_; | 212 Factory* factory_; |
213 Zone zone_; | |
196 Handle<JSFunction> object_constructor_; | 214 Handle<JSFunction> object_constructor_; |
197 uc32 c0_; | 215 uc32 c0_; |
198 int position_; | 216 int position_; |
199 Zone* zone_; | |
200 }; | 217 }; |
201 | 218 |
202 template <bool seq_ascii> | 219 template <bool seq_ascii> |
203 Handle<Object> JsonParser<seq_ascii>::ParseJson(Handle<String> source, | 220 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) | 221 // Advance to the first character (possibly EOS) |
223 AdvanceSkipWhitespace(); | 222 AdvanceSkipWhitespace(); |
224 Handle<Object> result = ParseJsonValue(); | 223 Handle<Object> result = ParseJsonValue(); |
225 if (result.is_null() || c0_ != kEndOfString) { | 224 if (result.is_null() || c0_ != kEndOfString) { |
226 // Some exception (for example stack overflow) is already pending. | 225 // Some exception (for example stack overflow) is already pending. |
227 if (isolate_->has_pending_exception()) return Handle<Object>::null(); | 226 if (isolate_->has_pending_exception()) return Handle<Object>::null(); |
228 | 227 |
229 // Parse failed. Current character is the unexpected token. | 228 // Parse failed. Current character is the unexpected token. |
230 const char* message; | 229 const char* message; |
231 Factory* factory = this->factory(); | 230 Factory* factory = this->factory(); |
(...skipping 25 matching lines...) Expand all Loading... | |
257 default: | 256 default: |
258 message = "unexpected_token"; | 257 message = "unexpected_token"; |
259 Handle<Object> name = | 258 Handle<Object> name = |
260 LookupSingleCharacterStringFromCode(isolate_, c0_); | 259 LookupSingleCharacterStringFromCode(isolate_, c0_); |
261 Handle<FixedArray> element = factory->NewFixedArray(1); | 260 Handle<FixedArray> element = factory->NewFixedArray(1); |
262 element->set(0, *name); | 261 element->set(0, *name); |
263 array = factory->NewJSArrayWithElements(element); | 262 array = factory->NewJSArrayWithElements(element); |
264 break; | 263 break; |
265 } | 264 } |
266 | 265 |
267 MessageLocation location(factory->NewScript(source), | 266 MessageLocation location(factory->NewScript(source_), |
268 position_, | 267 position_, |
269 position_ + 1); | 268 position_ + 1); |
270 Handle<Object> result = factory->NewSyntaxError(message, array); | 269 Handle<Object> result = factory->NewSyntaxError(message, array); |
271 isolate()->Throw(*result, &location); | 270 isolate()->Throw(*result, &location); |
272 return Handle<Object>::null(); | 271 return Handle<Object>::null(); |
273 } | 272 } |
274 return result; | 273 return result; |
275 } | 274 } |
276 | 275 |
277 | 276 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
316 } | 315 } |
317 | 316 |
318 | 317 |
319 // Parse a JSON object. Position must be right at '{'. | 318 // Parse a JSON object. Position must be right at '{'. |
320 template <bool seq_ascii> | 319 template <bool seq_ascii> |
321 Handle<Object> JsonParser<seq_ascii>::ParseJsonObject() { | 320 Handle<Object> JsonParser<seq_ascii>::ParseJsonObject() { |
322 HandleScope scope(isolate()); | 321 HandleScope scope(isolate()); |
323 Handle<JSObject> json_object = | 322 Handle<JSObject> json_object = |
324 factory()->NewJSObject(object_constructor(), pretenure_); | 323 factory()->NewJSObject(object_constructor(), pretenure_); |
325 Handle<Map> map(json_object->map()); | 324 Handle<Map> map(json_object->map()); |
326 ZoneScope zone_scope(zone()); | 325 ZoneList<Handle<Object> > properties(8, &zone_); |
danno
2013/06/26 13:06:58
here an else where, does it make sense to wrap zon
Benedikt Meurer
2013/06/26 13:34:39
Done.
| |
327 ZoneList<Handle<Object> > properties(8, zone()); | |
328 ASSERT_EQ(c0_, '{'); | 326 ASSERT_EQ(c0_, '{'); |
329 | 327 |
330 bool transitioning = true; | 328 bool transitioning = true; |
331 | 329 |
332 AdvanceSkipWhitespace(); | 330 AdvanceSkipWhitespace(); |
333 if (c0_ != '}') { | 331 if (c0_ != '}') { |
334 do { | 332 do { |
335 if (c0_ != '"') return ReportUnexpectedCharacter(); | 333 if (c0_ != '"') return ReportUnexpectedCharacter(); |
336 | 334 |
337 int start_position = position_; | 335 int start_position = position_; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
413 | 411 |
414 if (value->FitsRepresentation(expected_representation)) { | 412 if (value->FitsRepresentation(expected_representation)) { |
415 // If the target representation is double and the value is already | 413 // If the target representation is double and the value is already |
416 // double, use the existing box. | 414 // double, use the existing box. |
417 if (FLAG_track_double_fields && | 415 if (FLAG_track_double_fields && |
418 value->IsSmi() && | 416 value->IsSmi() && |
419 expected_representation.IsDouble()) { | 417 expected_representation.IsDouble()) { |
420 value = factory()->NewHeapNumber( | 418 value = factory()->NewHeapNumber( |
421 Handle<Smi>::cast(value)->value()); | 419 Handle<Smi>::cast(value)->value()); |
422 } | 420 } |
423 properties.Add(value, zone()); | 421 properties.Add(value, &zone_); |
424 map = target; | 422 map = target; |
425 continue; | 423 continue; |
426 } else { | 424 } else { |
427 transitioning = false; | 425 transitioning = false; |
428 } | 426 } |
429 } | 427 } |
430 | 428 |
431 // Commit the intermediate state to the object and stop transitioning. | 429 // Commit the intermediate state to the object and stop transitioning. |
432 JSObject::AllocateStorageForMap(json_object, map); | 430 JSObject::AllocateStorageForMap(json_object, map); |
433 int length = properties.length(); | 431 int length = properties.length(); |
(...skipping 28 matching lines...) Expand all Loading... | |
462 } | 460 } |
463 } | 461 } |
464 AdvanceSkipWhitespace(); | 462 AdvanceSkipWhitespace(); |
465 return scope.CloseAndEscape(json_object); | 463 return scope.CloseAndEscape(json_object); |
466 } | 464 } |
467 | 465 |
468 // Parse a JSON array. Position must be right at '['. | 466 // Parse a JSON array. Position must be right at '['. |
469 template <bool seq_ascii> | 467 template <bool seq_ascii> |
470 Handle<Object> JsonParser<seq_ascii>::ParseJsonArray() { | 468 Handle<Object> JsonParser<seq_ascii>::ParseJsonArray() { |
471 HandleScope scope(isolate()); | 469 HandleScope scope(isolate()); |
472 ZoneScope zone_scope(zone()); | 470 ZoneList<Handle<Object> > elements(4, &zone_); |
473 ZoneList<Handle<Object> > elements(4, zone()); | |
474 ASSERT_EQ(c0_, '['); | 471 ASSERT_EQ(c0_, '['); |
475 | 472 |
476 AdvanceSkipWhitespace(); | 473 AdvanceSkipWhitespace(); |
477 if (c0_ != ']') { | 474 if (c0_ != ']') { |
478 do { | 475 do { |
479 Handle<Object> element = ParseJsonValue(); | 476 Handle<Object> element = ParseJsonValue(); |
480 if (element.is_null()) return ReportUnexpectedCharacter(); | 477 if (element.is_null()) return ReportUnexpectedCharacter(); |
481 elements.Add(element, zone()); | 478 elements.Add(element, &zone_); |
482 } while (MatchSkipWhiteSpace(',')); | 479 } while (MatchSkipWhiteSpace(',')); |
483 if (c0_ != ']') { | 480 if (c0_ != ']') { |
484 return ReportUnexpectedCharacter(); | 481 return ReportUnexpectedCharacter(); |
485 } | 482 } |
486 } | 483 } |
487 AdvanceSkipWhitespace(); | 484 AdvanceSkipWhitespace(); |
488 // Allocate a fixed array with all the elements. | 485 // Allocate a fixed array with all the elements. |
489 Handle<FixedArray> fast_elements = | 486 Handle<FixedArray> fast_elements = |
490 factory()->NewFixedArray(elements.length(), pretenure_); | 487 factory()->NewFixedArray(elements.length(), pretenure_); |
491 for (int i = 0, n = elements.length(); i < n; i++) { | 488 for (int i = 0, n = elements.length(); i < n; i++) { |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
801 | 798 |
802 ASSERT_EQ('"', c0_); | 799 ASSERT_EQ('"', c0_); |
803 // Advance past the last '"'. | 800 // Advance past the last '"'. |
804 AdvanceSkipWhitespace(); | 801 AdvanceSkipWhitespace(); |
805 return result; | 802 return result; |
806 } | 803 } |
807 | 804 |
808 } } // namespace v8::internal | 805 } } // namespace v8::internal |
809 | 806 |
810 #endif // V8_JSON_PARSER_H_ | 807 #endif // V8_JSON_PARSER_H_ |
OLD | NEW |