OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 "x\0 y\0 z\0 {\0 " | 212 "x\0 y\0 z\0 {\0 " |
213 "|\0 }\0 ~\0 \177\0 "; | 213 "|\0 }\0 ~\0 \177\0 "; |
214 | 214 |
215 | 215 |
216 BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate) | 216 BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate) |
217 : isolate_(isolate), current_index_(0), is_ascii_(true) { | 217 : isolate_(isolate), current_index_(0), is_ascii_(true) { |
218 factory_ = isolate_->factory(); | 218 factory_ = isolate_->factory(); |
219 accumulator_store_ = Handle<JSValue>::cast( | 219 accumulator_store_ = Handle<JSValue>::cast( |
220 factory_->ToObject(factory_->empty_string())); | 220 factory_->ToObject(factory_->empty_string())); |
221 part_length_ = kInitialPartLength; | 221 part_length_ = kInitialPartLength; |
222 current_part_ = factory_->NewRawAsciiString(kInitialPartLength); | 222 current_part_ = factory_->NewRawOneByteString(kInitialPartLength); |
223 tojson_symbol_ = factory_->LookupAsciiSymbol("toJSON"); | 223 tojson_symbol_ = factory_->LookupAsciiSymbol("toJSON"); |
224 stack_ = factory_->NewJSArray(8); | 224 stack_ = factory_->NewJSArray(8); |
225 } | 225 } |
226 | 226 |
227 | 227 |
228 MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) { | 228 MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) { |
229 switch (SerializeObject(object)) { | 229 switch (SerializeObject(object)) { |
230 case UNCHANGED: | 230 case UNCHANGED: |
231 return isolate_->heap()->undefined_value(); | 231 return isolate_->heap()->undefined_value(); |
232 case SUCCESS: | 232 case SUCCESS: |
233 ShrinkCurrentPart(); | 233 ShrinkCurrentPart(); |
234 return *factory_->NewConsString(accumulator(), current_part_); | 234 return *factory_->NewConsString(accumulator(), current_part_); |
235 case CIRCULAR: | 235 case CIRCULAR: |
236 return isolate_->Throw(*factory_->NewTypeError( | 236 return isolate_->Throw(*factory_->NewTypeError( |
237 "circular_structure", HandleVector<Object>(NULL, 0))); | 237 "circular_structure", HandleVector<Object>(NULL, 0))); |
238 case STACK_OVERFLOW: | 238 case STACK_OVERFLOW: |
239 return isolate_->StackOverflow(); | 239 return isolate_->StackOverflow(); |
240 default: | 240 default: |
241 return Failure::Exception(); | 241 return Failure::Exception(); |
242 } | 242 } |
243 } | 243 } |
244 | 244 |
245 | 245 |
246 template <bool is_ascii, typename Char> | 246 template <bool is_ascii, typename Char> |
247 void BasicJsonStringifier::Append_(Char c) { | 247 void BasicJsonStringifier::Append_(Char c) { |
248 if (is_ascii) { | 248 if (is_ascii) { |
249 SeqAsciiString::cast(*current_part_)->SeqAsciiStringSet( | 249 SeqOneByteString::cast(*current_part_)->SeqOneByteStringSet( |
250 current_index_++, c); | 250 current_index_++, c); |
251 } else { | 251 } else { |
252 SeqTwoByteString::cast(*current_part_)->SeqTwoByteStringSet( | 252 SeqTwoByteString::cast(*current_part_)->SeqTwoByteStringSet( |
253 current_index_++, c); | 253 current_index_++, c); |
254 } | 254 } |
255 if (current_index_ == part_length_) Extend(); | 255 if (current_index_ == part_length_) Extend(); |
256 } | 256 } |
257 | 257 |
258 | 258 |
259 template <bool is_ascii, typename Char> | 259 template <bool is_ascii, typename Char> |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 | 629 |
630 void BasicJsonStringifier::ShrinkCurrentPart() { | 630 void BasicJsonStringifier::ShrinkCurrentPart() { |
631 ASSERT(current_index_ < part_length_); | 631 ASSERT(current_index_ < part_length_); |
632 if (current_index_ == 0) { | 632 if (current_index_ == 0) { |
633 current_part_ = factory_->empty_string(); | 633 current_part_ = factory_->empty_string(); |
634 return; | 634 return; |
635 } | 635 } |
636 | 636 |
637 int string_size, allocated_string_size; | 637 int string_size, allocated_string_size; |
638 if (is_ascii_) { | 638 if (is_ascii_) { |
639 allocated_string_size = SeqAsciiString::SizeFor(part_length_); | 639 allocated_string_size = SeqOneByteString::SizeFor(part_length_); |
640 string_size = SeqAsciiString::SizeFor(current_index_); | 640 string_size = SeqOneByteString::SizeFor(current_index_); |
641 } else { | 641 } else { |
642 allocated_string_size = SeqTwoByteString::SizeFor(part_length_); | 642 allocated_string_size = SeqTwoByteString::SizeFor(part_length_); |
643 string_size = SeqTwoByteString::SizeFor(current_index_); | 643 string_size = SeqTwoByteString::SizeFor(current_index_); |
644 } | 644 } |
645 | 645 |
646 int delta = allocated_string_size - string_size; | 646 int delta = allocated_string_size - string_size; |
647 current_part_->set_length(current_index_); | 647 current_part_->set_length(current_index_); |
648 | 648 |
649 // String sizes are pointer size aligned, so that we can use filler objects | 649 // String sizes are pointer size aligned, so that we can use filler objects |
650 // that are a multiple of pointer size. | 650 // that are a multiple of pointer size. |
651 Address end_of_string = current_part_->address() + string_size; | 651 Address end_of_string = current_part_->address() + string_size; |
652 isolate_->heap()->CreateFillerObjectAt(end_of_string, delta); | 652 isolate_->heap()->CreateFillerObjectAt(end_of_string, delta); |
653 if (Marking::IsBlack(Marking::MarkBitFrom(*current_part_))) { | 653 if (Marking::IsBlack(Marking::MarkBitFrom(*current_part_))) { |
654 MemoryChunk::IncrementLiveBytesFromMutator( | 654 MemoryChunk::IncrementLiveBytesFromMutator( |
655 current_part_->address(), -delta); | 655 current_part_->address(), -delta); |
656 } | 656 } |
657 } | 657 } |
658 | 658 |
659 | 659 |
660 void BasicJsonStringifier::Extend() { | 660 void BasicJsonStringifier::Extend() { |
661 set_accumulator(factory_->NewConsString(accumulator(), current_part_)); | 661 set_accumulator(factory_->NewConsString(accumulator(), current_part_)); |
662 if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) { | 662 if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) { |
663 part_length_ *= kPartLengthGrowthFactor; | 663 part_length_ *= kPartLengthGrowthFactor; |
664 } | 664 } |
665 if (is_ascii_) { | 665 if (is_ascii_) { |
666 current_part_ = factory_->NewRawAsciiString(part_length_); | 666 current_part_ = factory_->NewRawOneByteString(part_length_); |
667 } else { | 667 } else { |
668 current_part_ = factory_->NewRawTwoByteString(part_length_); | 668 current_part_ = factory_->NewRawTwoByteString(part_length_); |
669 } | 669 } |
670 current_index_ = 0; | 670 current_index_ = 0; |
671 } | 671 } |
672 | 672 |
673 | 673 |
674 void BasicJsonStringifier::ChangeEncoding() { | 674 void BasicJsonStringifier::ChangeEncoding() { |
675 ShrinkCurrentPart(); | 675 ShrinkCurrentPart(); |
676 set_accumulator(factory_->NewConsString(accumulator(), current_part_)); | 676 set_accumulator(factory_->NewConsString(accumulator(), current_part_)); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 Append_<is_ascii, char>('"'); | 712 Append_<is_ascii, char>('"'); |
713 // We make a rough estimate to find out if the current string can be | 713 // We make a rough estimate to find out if the current string can be |
714 // serialized without allocating a new string part. The worst case length of | 714 // serialized without allocating a new string part. The worst case length of |
715 // an escaped character is 6. Shifting the remainin string length right by 3 | 715 // an escaped character is 6. Shifting the remainin string length right by 3 |
716 // is a more pessimistic estimate, but faster to calculate. | 716 // is a more pessimistic estimate, but faster to calculate. |
717 | 717 |
718 if (((part_length_ - current_index_) >> 3) > length) { | 718 if (((part_length_ - current_index_) >> 3) > length) { |
719 if (is_ascii) { | 719 if (is_ascii) { |
720 SerializeStringUnchecked_( | 720 SerializeStringUnchecked_( |
721 vector.start(), | 721 vector.start(), |
722 SeqAsciiString::cast(*current_part_)->GetChars(), | 722 SeqOneByteString::cast(*current_part_)->GetChars(), |
723 length); | 723 length); |
724 } else { | 724 } else { |
725 SerializeStringUnchecked_( | 725 SerializeStringUnchecked_( |
726 vector.start(), | 726 vector.start(), |
727 SeqTwoByteString::cast(*current_part_)->GetChars(), | 727 SeqTwoByteString::cast(*current_part_)->GetChars(), |
728 length); | 728 length); |
729 } | 729 } |
730 } else { | 730 } else { |
731 String* string_location = *string; | 731 String* string_location = *string; |
732 for (int i = 0; i < length; i++) { | 732 for (int i = 0; i < length; i++) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
792 SerializeString_<false, char>(flat.ToAsciiVector(), object); | 792 SerializeString_<false, char>(flat.ToAsciiVector(), object); |
793 } else { | 793 } else { |
794 SerializeString_<false, uc16>(flat.ToUC16Vector(), object); | 794 SerializeString_<false, uc16>(flat.ToUC16Vector(), object); |
795 } | 795 } |
796 } | 796 } |
797 } | 797 } |
798 | 798 |
799 } } // namespace v8::internal | 799 } } // namespace v8::internal |
800 | 800 |
801 #endif // V8_JSON_STRINGIFIER_H_ | 801 #endif // V8_JSON_STRINGIFIER_H_ |
OLD | NEW |