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 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate) | 259 BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate) |
260 : isolate_(isolate), | 260 : isolate_(isolate), |
261 current_index_(0), | 261 current_index_(0), |
262 is_ascii_(true), | 262 is_ascii_(true), |
263 overflowed_(false) { | 263 overflowed_(false) { |
264 factory_ = isolate_->factory(); | 264 factory_ = isolate_->factory(); |
265 accumulator_store_ = Handle<JSValue>::cast( | 265 accumulator_store_ = Handle<JSValue>::cast( |
266 factory_->ToObject(factory_->empty_string())); | 266 factory_->ToObject(factory_->empty_string())); |
267 part_length_ = kInitialPartLength; | 267 part_length_ = kInitialPartLength; |
268 current_part_ = factory_->NewRawOneByteString(part_length_); | 268 current_part_ = factory_->NewRawOneByteString(part_length_); |
269 ASSERT(!current_part_.is_null()); | |
270 tojson_string_ = factory_->toJSON_string(); | 269 tojson_string_ = factory_->toJSON_string(); |
271 stack_ = factory_->NewJSArray(8); | 270 stack_ = factory_->NewJSArray(8); |
272 } | 271 } |
273 | 272 |
274 | 273 |
275 MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) { | 274 MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) { |
276 switch (SerializeObject(object)) { | 275 switch (SerializeObject(object)) { |
277 case UNCHANGED: | 276 case UNCHANGED: |
278 return isolate_->heap()->undefined_value(); | 277 return isolate_->heap()->undefined_value(); |
279 case SUCCESS: { | 278 case SUCCESS: { |
(...skipping 23 matching lines...) Expand all Loading... |
303 if (worst_case_length > 32 * KB) { // Slow path if too large. | 302 if (worst_case_length > 32 * KB) { // Slow path if too large. |
304 BasicJsonStringifier stringifier(isolate); | 303 BasicJsonStringifier stringifier(isolate); |
305 return stringifier.Stringify(object); | 304 return stringifier.Stringify(object); |
306 } | 305 } |
307 | 306 |
308 FlattenString(object); | 307 FlattenString(object); |
309 ASSERT(object->IsFlat()); | 308 ASSERT(object->IsFlat()); |
310 if (object->IsOneByteRepresentationUnderneath()) { | 309 if (object->IsOneByteRepresentationUnderneath()) { |
311 Handle<String> result = | 310 Handle<String> result = |
312 isolate->factory()->NewRawOneByteString(worst_case_length); | 311 isolate->factory()->NewRawOneByteString(worst_case_length); |
313 ASSERT(!result.is_null()); | |
314 DisallowHeapAllocation no_gc; | 312 DisallowHeapAllocation no_gc; |
315 return StringifyString_<SeqOneByteString>( | 313 return StringifyString_<SeqOneByteString>( |
316 isolate, | 314 isolate, |
317 object->GetFlatContent().ToOneByteVector(), | 315 object->GetFlatContent().ToOneByteVector(), |
318 result); | 316 result); |
319 } else { | 317 } else { |
320 Handle<String> result = | 318 Handle<String> result = |
321 isolate->factory()->NewRawTwoByteString(worst_case_length); | 319 isolate->factory()->NewRawTwoByteString(worst_case_length); |
322 ASSERT(!result.is_null()); | |
323 DisallowHeapAllocation no_gc; | 320 DisallowHeapAllocation no_gc; |
324 return StringifyString_<SeqTwoByteString>( | 321 return StringifyString_<SeqTwoByteString>( |
325 isolate, | 322 isolate, |
326 object->GetFlatContent().ToUC16Vector(), | 323 object->GetFlatContent().ToUC16Vector(), |
327 result); | 324 result); |
328 } | 325 } |
329 } | 326 } |
330 | 327 |
331 | 328 |
332 template <typename ResultType, typename Char> | 329 template <typename ResultType, typename Char> |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
718 void BasicJsonStringifier::ShrinkCurrentPart() { | 715 void BasicJsonStringifier::ShrinkCurrentPart() { |
719 ASSERT(current_index_ < part_length_); | 716 ASSERT(current_index_ < part_length_); |
720 current_part_ = SeqString::Truncate(Handle<SeqString>::cast(current_part_), | 717 current_part_ = SeqString::Truncate(Handle<SeqString>::cast(current_part_), |
721 current_index_); | 718 current_index_); |
722 } | 719 } |
723 | 720 |
724 | 721 |
725 void BasicJsonStringifier::Accumulate() { | 722 void BasicJsonStringifier::Accumulate() { |
726 if (accumulator()->length() + current_part_->length() > String::kMaxLength) { | 723 if (accumulator()->length() + current_part_->length() > String::kMaxLength) { |
727 // Screw it. Simply set the flag and carry on. Throw exception at the end. | 724 // Screw it. Simply set the flag and carry on. Throw exception at the end. |
| 725 // We most likely will trigger a real OOM before even reaching this point. |
728 set_accumulator(factory_->empty_string()); | 726 set_accumulator(factory_->empty_string()); |
729 overflowed_ = true; | 727 overflowed_ = true; |
730 } else { | 728 } else { |
731 set_accumulator(factory_->NewConsString(accumulator(), current_part_)); | 729 set_accumulator(factory_->NewConsString(accumulator(), current_part_)); |
732 } | 730 } |
733 } | 731 } |
734 | 732 |
735 | 733 |
736 void BasicJsonStringifier::Extend() { | 734 void BasicJsonStringifier::Extend() { |
737 Accumulate(); | 735 Accumulate(); |
738 if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) { | 736 if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) { |
739 part_length_ *= kPartLengthGrowthFactor; | 737 part_length_ *= kPartLengthGrowthFactor; |
740 } | 738 } |
741 if (is_ascii_) { | 739 if (is_ascii_) { |
742 current_part_ = factory_->NewRawOneByteString(part_length_); | 740 current_part_ = factory_->NewRawOneByteString(part_length_); |
743 } else { | 741 } else { |
744 current_part_ = factory_->NewRawTwoByteString(part_length_); | 742 current_part_ = factory_->NewRawTwoByteString(part_length_); |
745 } | 743 } |
746 ASSERT(!current_part_.is_null()); | |
747 current_index_ = 0; | 744 current_index_ = 0; |
748 } | 745 } |
749 | 746 |
750 | 747 |
751 void BasicJsonStringifier::ChangeEncoding() { | 748 void BasicJsonStringifier::ChangeEncoding() { |
752 ShrinkCurrentPart(); | 749 ShrinkCurrentPart(); |
753 Accumulate(); | 750 Accumulate(); |
754 current_part_ = factory_->NewRawTwoByteString(part_length_); | 751 current_part_ = factory_->NewRawTwoByteString(part_length_); |
755 ASSERT(!current_part_.is_null()); | |
756 current_index_ = 0; | 752 current_index_ = 0; |
757 is_ascii_ = false; | 753 is_ascii_ = false; |
758 } | 754 } |
759 | 755 |
760 | 756 |
761 template <typename SrcChar, typename DestChar> | 757 template <typename SrcChar, typename DestChar> |
762 int BasicJsonStringifier::SerializeStringUnchecked_(const SrcChar* src, | 758 int BasicJsonStringifier::SerializeStringUnchecked_(const SrcChar* src, |
763 DestChar* dest, | 759 DestChar* dest, |
764 int length) { | 760 int length) { |
765 DestChar* dest_start = dest; | 761 DestChar* dest_start = dest; |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
874 SerializeString_<false, uint8_t>(object); | 870 SerializeString_<false, uint8_t>(object); |
875 } else { | 871 } else { |
876 SerializeString_<false, uc16>(object); | 872 SerializeString_<false, uc16>(object); |
877 } | 873 } |
878 } | 874 } |
879 } | 875 } |
880 | 876 |
881 } } // namespace v8::internal | 877 } } // namespace v8::internal |
882 | 878 |
883 #endif // V8_JSON_STRINGIFIER_H_ | 879 #endif // V8_JSON_STRINGIFIER_H_ |
OLD | NEW |