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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 INLINE(static MaybeObject* StringifyString(Isolate* isolate, | 44 INLINE(static MaybeObject* StringifyString(Isolate* isolate, |
45 Handle<String> object)); | 45 Handle<String> object)); |
46 | 46 |
47 private: | 47 private: |
48 static const int kInitialPartLength = 32; | 48 static const int kInitialPartLength = 32; |
49 static const int kMaxPartLength = 16 * 1024; | 49 static const int kMaxPartLength = 16 * 1024; |
50 static const int kPartLengthGrowthFactor = 2; | 50 static const int kPartLengthGrowthFactor = 2; |
51 | 51 |
52 enum Result { UNCHANGED, SUCCESS, EXCEPTION, CIRCULAR, STACK_OVERFLOW }; | 52 enum Result { UNCHANGED, SUCCESS, EXCEPTION, CIRCULAR, STACK_OVERFLOW }; |
53 | 53 |
54 void Accumulate(); | |
55 | |
56 void Extend(); | 54 void Extend(); |
57 | 55 |
58 void ChangeEncoding(); | 56 void ChangeEncoding(); |
59 | 57 |
60 INLINE(void ShrinkCurrentPart()); | 58 INLINE(void ShrinkCurrentPart()); |
61 | 59 |
62 template <bool is_ascii, typename Char> | 60 template <bool is_ascii, typename Char> |
63 INLINE(void Append_(Char c)); | 61 INLINE(void Append_(Char c)); |
64 | 62 |
65 template <bool is_ascii, typename Char> | 63 template <bool is_ascii, typename Char> |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 Factory* factory_; | 171 Factory* factory_; |
174 // We use a value wrapper for the string accumulator to keep the | 172 // We use a value wrapper for the string accumulator to keep the |
175 // (indirect) handle to it in the outermost handle scope. | 173 // (indirect) handle to it in the outermost handle scope. |
176 Handle<JSValue> accumulator_store_; | 174 Handle<JSValue> accumulator_store_; |
177 Handle<String> current_part_; | 175 Handle<String> current_part_; |
178 Handle<String> tojson_string_; | 176 Handle<String> tojson_string_; |
179 Handle<JSArray> stack_; | 177 Handle<JSArray> stack_; |
180 int current_index_; | 178 int current_index_; |
181 int part_length_; | 179 int part_length_; |
182 bool is_ascii_; | 180 bool is_ascii_; |
183 bool overflowed_; | |
184 | 181 |
185 static const int kJsonEscapeTableEntrySize = 8; | 182 static const int kJsonEscapeTableEntrySize = 8; |
186 static const char* const JsonEscapeTable; | 183 static const char* const JsonEscapeTable; |
187 }; | 184 }; |
188 | 185 |
189 | 186 |
190 // Translation table to escape ASCII characters. | 187 // Translation table to escape ASCII characters. |
191 // Table entries start at a multiple of 8 and are null-terminated. | 188 // Table entries start at a multiple of 8 and are null-terminated. |
192 const char* const BasicJsonStringifier::JsonEscapeTable = | 189 const char* const BasicJsonStringifier::JsonEscapeTable = |
193 "\\u0000\0 \\u0001\0 \\u0002\0 \\u0003\0 " | 190 "\\u0000\0 \\u0001\0 \\u0002\0 \\u0003\0 " |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 "\344\0 \345\0 \346\0 \347\0 " | 247 "\344\0 \345\0 \346\0 \347\0 " |
251 "\350\0 \351\0 \352\0 \353\0 " | 248 "\350\0 \351\0 \352\0 \353\0 " |
252 "\354\0 \355\0 \356\0 \357\0 " | 249 "\354\0 \355\0 \356\0 \357\0 " |
253 "\360\0 \361\0 \362\0 \363\0 " | 250 "\360\0 \361\0 \362\0 \363\0 " |
254 "\364\0 \365\0 \366\0 \367\0 " | 251 "\364\0 \365\0 \366\0 \367\0 " |
255 "\370\0 \371\0 \372\0 \373\0 " | 252 "\370\0 \371\0 \372\0 \373\0 " |
256 "\374\0 \375\0 \376\0 \377\0 "; | 253 "\374\0 \375\0 \376\0 \377\0 "; |
257 | 254 |
258 | 255 |
259 BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate) | 256 BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate) |
260 : isolate_(isolate), | 257 : isolate_(isolate), current_index_(0), is_ascii_(true) { |
261 current_index_(0), | |
262 is_ascii_(true), | |
263 overflowed_(false) { | |
264 factory_ = isolate_->factory(); | 258 factory_ = isolate_->factory(); |
265 accumulator_store_ = Handle<JSValue>::cast( | 259 accumulator_store_ = Handle<JSValue>::cast( |
266 factory_->ToObject(factory_->empty_string())); | 260 factory_->ToObject(factory_->empty_string())); |
267 part_length_ = kInitialPartLength; | 261 part_length_ = kInitialPartLength; |
268 current_part_ = factory_->NewRawOneByteString(part_length_); | 262 current_part_ = factory_->NewRawOneByteString(part_length_); |
269 tojson_string_ = factory_->toJSON_string(); | 263 tojson_string_ = factory_->toJSON_string(); |
270 stack_ = factory_->NewJSArray(8); | 264 stack_ = factory_->NewJSArray(8); |
271 } | 265 } |
272 | 266 |
273 | 267 |
274 MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) { | 268 MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) { |
275 switch (SerializeObject(object)) { | 269 switch (SerializeObject(object)) { |
276 case UNCHANGED: | 270 case UNCHANGED: |
277 return isolate_->heap()->undefined_value(); | 271 return isolate_->heap()->undefined_value(); |
278 case SUCCESS: { | 272 case SUCCESS: |
279 ShrinkCurrentPart(); | 273 ShrinkCurrentPart(); |
280 Accumulate(); | 274 return *factory_->NewConsString(accumulator(), current_part_); |
281 if (overflowed_) return isolate_->ThrowInvalidStringLength(); | |
282 return *accumulator(); | |
283 } | |
284 case CIRCULAR: | 275 case CIRCULAR: |
285 return isolate_->Throw(*factory_->NewTypeError( | 276 return isolate_->Throw(*factory_->NewTypeError( |
286 "circular_structure", HandleVector<Object>(NULL, 0))); | 277 "circular_structure", HandleVector<Object>(NULL, 0))); |
287 case STACK_OVERFLOW: | 278 case STACK_OVERFLOW: |
288 return isolate_->StackOverflow(); | 279 return isolate_->StackOverflow(); |
289 default: | 280 default: |
290 return Failure::Exception(); | 281 return Failure::Exception(); |
291 } | 282 } |
292 } | 283 } |
293 | 284 |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 SerializeDeferredKey(deferred_comma, key); | 479 SerializeDeferredKey(deferred_comma, key); |
489 } | 480 } |
490 | 481 |
491 Handle<String> result_string = Handle<String>::cast(result); | 482 Handle<String> result_string = Handle<String>::cast(result); |
492 // Shrink current part, attach it to the accumulator, also attach the result | 483 // Shrink current part, attach it to the accumulator, also attach the result |
493 // string to the accumulator, and allocate a new part. | 484 // string to the accumulator, and allocate a new part. |
494 ShrinkCurrentPart(); // Shrink. | 485 ShrinkCurrentPart(); // Shrink. |
495 part_length_ = kInitialPartLength; // Allocate conservatively. | 486 part_length_ = kInitialPartLength; // Allocate conservatively. |
496 Extend(); // Attach current part and allocate new part. | 487 Extend(); // Attach current part and allocate new part. |
497 // Attach result string to the accumulator. | 488 // Attach result string to the accumulator. |
498 Handle<String> cons = factory_->NewConsString(accumulator(), result_string); | 489 set_accumulator(factory_->NewConsString(accumulator(), result_string)); |
499 RETURN_IF_EMPTY_HANDLE_VALUE(isolate_, cons, EXCEPTION); | |
500 set_accumulator(cons); | |
501 return SUCCESS; | 490 return SUCCESS; |
502 } | 491 } |
503 | 492 |
504 | 493 |
505 BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSValue( | 494 BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSValue( |
506 Handle<JSValue> object) { | 495 Handle<JSValue> object) { |
507 bool has_exception = false; | 496 bool has_exception = false; |
508 String* class_name = object->class_name(); | 497 String* class_name = object->class_name(); |
509 if (class_name == isolate_->heap()->String_string()) { | 498 if (class_name == isolate_->heap()->String_string()) { |
510 Handle<Object> value = | 499 Handle<Object> value = |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 } | 701 } |
713 | 702 |
714 | 703 |
715 void BasicJsonStringifier::ShrinkCurrentPart() { | 704 void BasicJsonStringifier::ShrinkCurrentPart() { |
716 ASSERT(current_index_ < part_length_); | 705 ASSERT(current_index_ < part_length_); |
717 current_part_ = SeqString::Truncate(Handle<SeqString>::cast(current_part_), | 706 current_part_ = SeqString::Truncate(Handle<SeqString>::cast(current_part_), |
718 current_index_); | 707 current_index_); |
719 } | 708 } |
720 | 709 |
721 | 710 |
722 void BasicJsonStringifier::Accumulate() { | |
723 if (accumulator()->length() + current_part_->length() > String::kMaxLength) { | |
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. | |
726 set_accumulator(factory_->empty_string()); | |
727 overflowed_ = true; | |
728 } else { | |
729 set_accumulator(factory_->NewConsString(accumulator(), current_part_)); | |
730 } | |
731 } | |
732 | |
733 | |
734 void BasicJsonStringifier::Extend() { | 711 void BasicJsonStringifier::Extend() { |
735 Accumulate(); | 712 set_accumulator(factory_->NewConsString(accumulator(), current_part_)); |
736 if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) { | 713 if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) { |
737 part_length_ *= kPartLengthGrowthFactor; | 714 part_length_ *= kPartLengthGrowthFactor; |
738 } | 715 } |
739 if (is_ascii_) { | 716 if (is_ascii_) { |
740 current_part_ = factory_->NewRawOneByteString(part_length_); | 717 current_part_ = factory_->NewRawOneByteString(part_length_); |
741 } else { | 718 } else { |
742 current_part_ = factory_->NewRawTwoByteString(part_length_); | 719 current_part_ = factory_->NewRawTwoByteString(part_length_); |
743 } | 720 } |
744 current_index_ = 0; | 721 current_index_ = 0; |
745 } | 722 } |
746 | 723 |
747 | 724 |
748 void BasicJsonStringifier::ChangeEncoding() { | 725 void BasicJsonStringifier::ChangeEncoding() { |
749 ShrinkCurrentPart(); | 726 ShrinkCurrentPart(); |
750 Accumulate(); | 727 set_accumulator(factory_->NewConsString(accumulator(), current_part_)); |
751 current_part_ = factory_->NewRawTwoByteString(part_length_); | 728 current_part_ = factory_->NewRawTwoByteString(part_length_); |
752 current_index_ = 0; | 729 current_index_ = 0; |
753 is_ascii_ = false; | 730 is_ascii_ = false; |
754 } | 731 } |
755 | 732 |
756 | 733 |
757 template <typename SrcChar, typename DestChar> | 734 template <typename SrcChar, typename DestChar> |
758 int BasicJsonStringifier::SerializeStringUnchecked_(const SrcChar* src, | 735 int BasicJsonStringifier::SerializeStringUnchecked_(const SrcChar* src, |
759 DestChar* dest, | 736 DestChar* dest, |
760 int length) { | 737 int length) { |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
870 SerializeString_<false, uint8_t>(object); | 847 SerializeString_<false, uint8_t>(object); |
871 } else { | 848 } else { |
872 SerializeString_<false, uc16>(object); | 849 SerializeString_<false, uc16>(object); |
873 } | 850 } |
874 } | 851 } |
875 } | 852 } |
876 | 853 |
877 } } // namespace v8::internal | 854 } } // namespace v8::internal |
878 | 855 |
879 #endif // V8_JSON_STRINGIFIER_H_ | 856 #endif // V8_JSON_STRINGIFIER_H_ |
OLD | NEW |