OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_JSON_STRINGIFIER_H_ | 5 #ifndef V8_JSON_STRINGIFIER_H_ |
6 #define V8_JSON_STRINGIFIER_H_ | 6 #define V8_JSON_STRINGIFIER_H_ |
7 | 7 |
8 #include "src/v8.h" | 8 #include "src/v8.h" |
9 | 9 |
10 #include "src/conversions.h" | 10 #include "src/conversions.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 INLINE(static void SerializeStringUnchecked_( | 90 INLINE(static void SerializeStringUnchecked_( |
91 Vector<const SrcChar> src, | 91 Vector<const SrcChar> src, |
92 IncrementalStringBuilder::NoExtend<DestChar>* dest)); | 92 IncrementalStringBuilder::NoExtend<DestChar>* dest)); |
93 | 93 |
94 template <typename SrcChar, typename DestChar> | 94 template <typename SrcChar, typename DestChar> |
95 INLINE(void SerializeString_(Handle<String> string)); | 95 INLINE(void SerializeString_(Handle<String> string)); |
96 | 96 |
97 template <typename Char> | 97 template <typename Char> |
98 INLINE(static bool DoNotEscape(Char c)); | 98 INLINE(static bool DoNotEscape(Char c)); |
99 | 99 |
100 template <typename Char> | |
101 INLINE(static Vector<const Char> GetCharVector(Handle<String> string)); | |
102 | |
103 Result StackPush(Handle<Object> object); | 100 Result StackPush(Handle<Object> object); |
104 void StackPop(); | 101 void StackPop(); |
105 | 102 |
106 Factory* factory() { return isolate_->factory(); } | 103 Factory* factory() { return isolate_->factory(); } |
107 | 104 |
108 Isolate* isolate_; | 105 Isolate* isolate_; |
109 IncrementalStringBuilder builder_; | 106 IncrementalStringBuilder builder_; |
110 Handle<String> tojson_string_; | 107 Handle<String> tojson_string_; |
111 Handle<JSArray> stack_; | 108 Handle<JSArray> stack_; |
112 | 109 |
(...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 void BasicJsonStringifier::SerializeString_(Handle<String> string) { | 624 void BasicJsonStringifier::SerializeString_(Handle<String> string) { |
628 int length = string->length(); | 625 int length = string->length(); |
629 builder_.Append<uint8_t, DestChar>('"'); | 626 builder_.Append<uint8_t, DestChar>('"'); |
630 // We make a rough estimate to find out if the current string can be | 627 // We make a rough estimate to find out if the current string can be |
631 // serialized without allocating a new string part. The worst case length of | 628 // serialized without allocating a new string part. The worst case length of |
632 // an escaped character is 6. Shifting the remainin string length right by 3 | 629 // an escaped character is 6. Shifting the remainin string length right by 3 |
633 // is a more pessimistic estimate, but faster to calculate. | 630 // is a more pessimistic estimate, but faster to calculate. |
634 int worst_case_length = length << 3; | 631 int worst_case_length = length << 3; |
635 if (builder_.CurrentPartCanFit(worst_case_length)) { | 632 if (builder_.CurrentPartCanFit(worst_case_length)) { |
636 DisallowHeapAllocation no_gc; | 633 DisallowHeapAllocation no_gc; |
637 Vector<const SrcChar> vector = GetCharVector<SrcChar>(string); | 634 Vector<const SrcChar> vector = string->GetCharVector<SrcChar>(); |
638 IncrementalStringBuilder::NoExtendBuilder<DestChar> no_extend( | 635 IncrementalStringBuilder::NoExtendBuilder<DestChar> no_extend( |
639 &builder_, worst_case_length); | 636 &builder_, worst_case_length); |
640 SerializeStringUnchecked_(vector, &no_extend); | 637 SerializeStringUnchecked_(vector, &no_extend); |
641 } else { | 638 } else { |
642 FlatStringReader reader(isolate_, string); | 639 FlatStringReader reader(isolate_, string); |
643 for (int i = 0; i < reader.length(); i++) { | 640 for (int i = 0; i < reader.length(); i++) { |
644 SrcChar c = reader.Get<SrcChar>(i); | 641 SrcChar c = reader.Get<SrcChar>(i); |
645 if (DoNotEscape(c)) { | 642 if (DoNotEscape(c)) { |
646 builder_.Append<SrcChar, DestChar>(c); | 643 builder_.Append<SrcChar, DestChar>(c); |
647 } else { | 644 } else { |
(...skipping 11 matching lines...) Expand all Loading... |
659 return c >= '#' && c <= '~' && c != '\\'; | 656 return c >= '#' && c <= '~' && c != '\\'; |
660 } | 657 } |
661 | 658 |
662 | 659 |
663 template <> | 660 template <> |
664 bool BasicJsonStringifier::DoNotEscape(uint16_t c) { | 661 bool BasicJsonStringifier::DoNotEscape(uint16_t c) { |
665 return c >= '#' && c != '\\' && c != 0x7f; | 662 return c >= '#' && c != '\\' && c != 0x7f; |
666 } | 663 } |
667 | 664 |
668 | 665 |
669 template <> | |
670 Vector<const uint8_t> BasicJsonStringifier::GetCharVector( | |
671 Handle<String> string) { | |
672 String::FlatContent flat = string->GetFlatContent(); | |
673 DCHECK(flat.IsOneByte()); | |
674 return flat.ToOneByteVector(); | |
675 } | |
676 | |
677 | |
678 template <> | |
679 Vector<const uc16> BasicJsonStringifier::GetCharVector(Handle<String> string) { | |
680 String::FlatContent flat = string->GetFlatContent(); | |
681 DCHECK(flat.IsTwoByte()); | |
682 return flat.ToUC16Vector(); | |
683 } | |
684 | |
685 | |
686 void BasicJsonStringifier::SerializeString(Handle<String> object) { | 666 void BasicJsonStringifier::SerializeString(Handle<String> object) { |
687 object = String::Flatten(object); | 667 object = String::Flatten(object); |
688 if (builder_.CurrentEncoding() == String::ONE_BYTE_ENCODING) { | 668 if (builder_.CurrentEncoding() == String::ONE_BYTE_ENCODING) { |
689 if (object->IsOneByteRepresentationUnderneath()) { | 669 if (object->IsOneByteRepresentationUnderneath()) { |
690 SerializeString_<uint8_t, uint8_t>(object); | 670 SerializeString_<uint8_t, uint8_t>(object); |
691 } else { | 671 } else { |
692 builder_.ChangeEncoding(); | 672 builder_.ChangeEncoding(); |
693 SerializeString(object); | 673 SerializeString(object); |
694 } | 674 } |
695 } else { | 675 } else { |
696 if (object->IsOneByteRepresentationUnderneath()) { | 676 if (object->IsOneByteRepresentationUnderneath()) { |
697 SerializeString_<uint8_t, uc16>(object); | 677 SerializeString_<uint8_t, uc16>(object); |
698 } else { | 678 } else { |
699 SerializeString_<uc16, uc16>(object); | 679 SerializeString_<uc16, uc16>(object); |
700 } | 680 } |
701 } | 681 } |
702 } | 682 } |
703 | 683 |
704 } } // namespace v8::internal | 684 } } // namespace v8::internal |
705 | 685 |
706 #endif // V8_JSON_STRINGIFIER_H_ | 686 #endif // V8_JSON_STRINGIFIER_H_ |
OLD | NEW |