Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: src/json-stringifier.h

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

Powered by Google App Engine
This is Rietveld 408576698