| 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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 return false; | 95 return false; |
| 96 } | 96 } |
| 97 | 97 |
| 98 // A JSON string (production JSONString) is subset of valid JavaScript string | 98 // A JSON string (production JSONString) is subset of valid JavaScript string |
| 99 // literals. The string must only be double-quoted (not single-quoted), and | 99 // literals. The string must only be double-quoted (not single-quoted), and |
| 100 // the only allowed backslash-escapes are ", /, \, b, f, n, r, t and | 100 // the only allowed backslash-escapes are ", /, \, b, f, n, r, t and |
| 101 // four-digit hex escapes (uXXXX). Any other use of backslashes is invalid. | 101 // four-digit hex escapes (uXXXX). Any other use of backslashes is invalid. |
| 102 Handle<String> ParseJsonString() { | 102 Handle<String> ParseJsonString() { |
| 103 return ScanJsonString<false>(); | 103 return ScanJsonString<false>(); |
| 104 } | 104 } |
| 105 Handle<String> ParseJsonSymbol() { | 105 Handle<String> ParseJsonInternalizedString() { |
| 106 return ScanJsonString<true>(); | 106 return ScanJsonString<true>(); |
| 107 } | 107 } |
| 108 template <bool is_symbol> | 108 template <bool is_internalized> |
| 109 Handle<String> ScanJsonString(); | 109 Handle<String> ScanJsonString(); |
| 110 // Creates a new string and copies prefix[start..end] into the beginning | 110 // Creates a new string and copies prefix[start..end] into the beginning |
| 111 // of it. Then scans the rest of the string, adding characters after the | 111 // of it. Then scans the rest of the string, adding characters after the |
| 112 // prefix. Called by ScanJsonString when reaching a '\' or non-ASCII char. | 112 // prefix. Called by ScanJsonString when reaching a '\' or non-ASCII char. |
| 113 template <typename StringType, typename SinkChar> | 113 template <typename StringType, typename SinkChar> |
| 114 Handle<String> SlowScanJsonString(Handle<String> prefix, int start, int end); | 114 Handle<String> SlowScanJsonString(Handle<String> prefix, int start, int end); |
| 115 | 115 |
| 116 // A JSON number (production JSONNumber) is a subset of the valid JavaScript | 116 // A JSON number (production JSONNumber) is a subset of the valid JavaScript |
| 117 // decimal number literals. | 117 // decimal number literals. |
| 118 // It includes an optional minus sign, must have at least one | 118 // It includes an optional minus sign, must have at least one |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 continue; | 331 continue; |
| 332 } | 332 } |
| 333 // Not an index, fallback to the slow path. | 333 // Not an index, fallback to the slow path. |
| 334 } | 334 } |
| 335 | 335 |
| 336 position_ = start_position; | 336 position_ = start_position; |
| 337 #ifdef DEBUG | 337 #ifdef DEBUG |
| 338 c0_ = '"'; | 338 c0_ = '"'; |
| 339 #endif | 339 #endif |
| 340 | 340 |
| 341 Handle<String> key = ParseJsonSymbol(); | 341 Handle<String> key = ParseJsonInternalizedString(); |
| 342 if (key.is_null() || c0_ != ':') return ReportUnexpectedCharacter(); | 342 if (key.is_null() || c0_ != ':') return ReportUnexpectedCharacter(); |
| 343 | 343 |
| 344 AdvanceSkipWhitespace(); | 344 AdvanceSkipWhitespace(); |
| 345 Handle<Object> value = ParseJsonValue(); | 345 Handle<Object> value = ParseJsonValue(); |
| 346 if (value.is_null()) return ReportUnexpectedCharacter(); | 346 if (value.is_null()) return ReportUnexpectedCharacter(); |
| 347 | 347 |
| 348 if (key->Equals(isolate()->heap()->Proto_symbol())) { | 348 if (key->Equals(isolate()->heap()->proto_string())) { |
| 349 prototype = value; | 349 prototype = value; |
| 350 } else { | 350 } else { |
| 351 if (JSObject::TryTransitionToField(json_object, key)) { | 351 if (JSObject::TryTransitionToField(json_object, key)) { |
| 352 int index = json_object->LastAddedFieldIndex(); | 352 int index = json_object->LastAddedFieldIndex(); |
| 353 json_object->FastPropertyAtPut(index, *value); | 353 json_object->FastPropertyAtPut(index, *value); |
| 354 } else { | 354 } else { |
| 355 JSObject::SetLocalPropertyIgnoreAttributes( | 355 JSObject::SetLocalPropertyIgnoreAttributes( |
| 356 json_object, key, value, NONE); | 356 json_object, key, value, NONE); |
| 357 } | 357 } |
| 358 } | 358 } |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 isolate()->heap()->CreateFillerObjectAt(start_filler_object, delta); | 599 isolate()->heap()->CreateFillerObjectAt(start_filler_object, delta); |
| 600 } | 600 } |
| 601 ASSERT_EQ('"', c0_); | 601 ASSERT_EQ('"', c0_); |
| 602 // Advance past the last '"'. | 602 // Advance past the last '"'. |
| 603 AdvanceSkipWhitespace(); | 603 AdvanceSkipWhitespace(); |
| 604 return seq_str; | 604 return seq_str; |
| 605 } | 605 } |
| 606 | 606 |
| 607 | 607 |
| 608 template <bool seq_ascii> | 608 template <bool seq_ascii> |
| 609 template <bool is_symbol> | 609 template <bool is_internalized> |
| 610 Handle<String> JsonParser<seq_ascii>::ScanJsonString() { | 610 Handle<String> JsonParser<seq_ascii>::ScanJsonString() { |
| 611 ASSERT_EQ('"', c0_); | 611 ASSERT_EQ('"', c0_); |
| 612 Advance(); | 612 Advance(); |
| 613 if (c0_ == '"') { | 613 if (c0_ == '"') { |
| 614 AdvanceSkipWhitespace(); | 614 AdvanceSkipWhitespace(); |
| 615 return factory()->empty_string(); | 615 return factory()->empty_string(); |
| 616 } | 616 } |
| 617 | 617 |
| 618 if (seq_ascii && is_symbol) { | 618 if (seq_ascii && is_internalized) { |
| 619 // Fast path for existing symbols. If the the string being parsed is not | 619 // Fast path for existing internalized strings. If the the string being |
| 620 // a known symbol, contains backslashes or unexpectedly reaches the end of | 620 // parsed is not a known internalized string, contains backslashes or |
| 621 // string, return with an empty handle. | 621 // unexpectedly reaches the end of string, return with an empty handle. |
| 622 uint32_t running_hash = isolate()->heap()->HashSeed(); | 622 uint32_t running_hash = isolate()->heap()->HashSeed(); |
| 623 int position = position_; | 623 int position = position_; |
| 624 uc32 c0 = c0_; | 624 uc32 c0 = c0_; |
| 625 do { | 625 do { |
| 626 if (c0 == '\\') { | 626 if (c0 == '\\') { |
| 627 c0_ = c0; | 627 c0_ = c0; |
| 628 int beg_pos = position_; | 628 int beg_pos = position_; |
| 629 position_ = position; | 629 position_ = position; |
| 630 return SlowScanJsonString<SeqOneByteString, uint8_t>(source_, | 630 return SlowScanJsonString<SeqOneByteString, uint8_t>(source_, |
| 631 beg_pos, | 631 beg_pos, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 645 } | 645 } |
| 646 position++; | 646 position++; |
| 647 if (position >= source_length_) return Handle<String>::null(); | 647 if (position >= source_length_) return Handle<String>::null(); |
| 648 c0 = seq_source_->SeqOneByteStringGet(position); | 648 c0 = seq_source_->SeqOneByteStringGet(position); |
| 649 } while (c0 != '"'); | 649 } while (c0 != '"'); |
| 650 int length = position - position_; | 650 int length = position - position_; |
| 651 uint32_t hash = (length <= String::kMaxHashCalcLength) | 651 uint32_t hash = (length <= String::kMaxHashCalcLength) |
| 652 ? StringHasher::GetHashCore(running_hash) : length; | 652 ? StringHasher::GetHashCore(running_hash) : length; |
| 653 Vector<const uint8_t> string_vector( | 653 Vector<const uint8_t> string_vector( |
| 654 seq_source_->GetChars() + position_, length); | 654 seq_source_->GetChars() + position_, length); |
| 655 SymbolTable* symbol_table = isolate()->heap()->symbol_table(); | 655 StringTable* string_table = isolate()->heap()->string_table(); |
| 656 uint32_t capacity = symbol_table->Capacity(); | 656 uint32_t capacity = string_table->Capacity(); |
| 657 uint32_t entry = SymbolTable::FirstProbe(hash, capacity); | 657 uint32_t entry = StringTable::FirstProbe(hash, capacity); |
| 658 uint32_t count = 1; | 658 uint32_t count = 1; |
| 659 while (true) { | 659 while (true) { |
| 660 Object* element = symbol_table->KeyAt(entry); | 660 Object* element = string_table->KeyAt(entry); |
| 661 if (element == isolate()->heap()->undefined_value()) { | 661 if (element == isolate()->heap()->undefined_value()) { |
| 662 // Lookup failure. | 662 // Lookup failure. |
| 663 break; | 663 break; |
| 664 } | 664 } |
| 665 if (element != isolate()->heap()->the_hole_value() && | 665 if (element != isolate()->heap()->the_hole_value() && |
| 666 String::cast(element)->IsOneByteEqualTo(string_vector)) { | 666 String::cast(element)->IsOneByteEqualTo(string_vector)) { |
| 667 // Lookup success, update the current position. | 667 // Lookup success, update the current position. |
| 668 position_ = position; | 668 position_ = position; |
| 669 // Advance past the last '"'. | 669 // Advance past the last '"'. |
| 670 AdvanceSkipWhitespace(); | 670 AdvanceSkipWhitespace(); |
| 671 return Handle<String>(String::cast(element), isolate()); | 671 return Handle<String>(String::cast(element), isolate()); |
| 672 } | 672 } |
| 673 entry = SymbolTable::NextProbe(entry, count++, capacity); | 673 entry = StringTable::NextProbe(entry, count++, capacity); |
| 674 } | 674 } |
| 675 } | 675 } |
| 676 | 676 |
| 677 int beg_pos = position_; | 677 int beg_pos = position_; |
| 678 // Fast case for ASCII only without escape characters. | 678 // Fast case for ASCII only without escape characters. |
| 679 do { | 679 do { |
| 680 // Check for control character (0x00-0x1f) or unterminated string (<0). | 680 // Check for control character (0x00-0x1f) or unterminated string (<0). |
| 681 if (c0_ < 0x20) return Handle<String>::null(); | 681 if (c0_ < 0x20) return Handle<String>::null(); |
| 682 if (c0_ != '\\') { | 682 if (c0_ != '\\') { |
| 683 if (seq_ascii || c0_ <= String::kMaxOneByteCharCode) { | 683 if (seq_ascii || c0_ <= String::kMaxOneByteCharCode) { |
| 684 Advance(); | 684 Advance(); |
| 685 } else { | 685 } else { |
| 686 return SlowScanJsonString<SeqTwoByteString, uc16>(source_, | 686 return SlowScanJsonString<SeqTwoByteString, uc16>(source_, |
| 687 beg_pos, | 687 beg_pos, |
| 688 position_); | 688 position_); |
| 689 } | 689 } |
| 690 } else { | 690 } else { |
| 691 return SlowScanJsonString<SeqOneByteString, uint8_t>(source_, | 691 return SlowScanJsonString<SeqOneByteString, uint8_t>(source_, |
| 692 beg_pos, | 692 beg_pos, |
| 693 position_); | 693 position_); |
| 694 } | 694 } |
| 695 } while (c0_ != '"'); | 695 } while (c0_ != '"'); |
| 696 int length = position_ - beg_pos; | 696 int length = position_ - beg_pos; |
| 697 Handle<String> result; | 697 Handle<String> result; |
| 698 if (seq_ascii && is_symbol) { | 698 if (seq_ascii && is_internalized) { |
| 699 result = factory()->LookupOneByteSymbol(seq_source_, beg_pos, length); | 699 result = factory()->InternalizeOneByteString(seq_source_, beg_pos, length); |
| 700 } else { | 700 } else { |
| 701 result = factory()->NewRawOneByteString(length, pretenure_); | 701 result = factory()->NewRawOneByteString(length, pretenure_); |
| 702 uint8_t* dest = SeqOneByteString::cast(*result)->GetChars(); | 702 uint8_t* dest = SeqOneByteString::cast(*result)->GetChars(); |
| 703 String::WriteToFlat(*source_, dest, beg_pos, position_); | 703 String::WriteToFlat(*source_, dest, beg_pos, position_); |
| 704 } | 704 } |
| 705 ASSERT_EQ('"', c0_); | 705 ASSERT_EQ('"', c0_); |
| 706 // Advance past the last '"'. | 706 // Advance past the last '"'. |
| 707 AdvanceSkipWhitespace(); | 707 AdvanceSkipWhitespace(); |
| 708 return result; | 708 return result; |
| 709 } | 709 } |
| 710 | 710 |
| 711 } } // namespace v8::internal | 711 } } // namespace v8::internal |
| 712 | 712 |
| 713 #endif // V8_JSON_PARSER_H_ | 713 #endif // V8_JSON_PARSER_H_ |
| OLD | NEW |