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

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

Issue 207613005: No longer OOM on invalid string length. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: rebase + addressed nits 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/json-parser.h ('k') | src/jsregexp.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 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
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());
269 tojson_string_ = factory_->toJSON_string(); 270 tojson_string_ = factory_->toJSON_string();
270 stack_ = factory_->NewJSArray(8); 271 stack_ = factory_->NewJSArray(8);
271 } 272 }
272 273
273 274
274 MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) { 275 MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) {
275 switch (SerializeObject(object)) { 276 switch (SerializeObject(object)) {
276 case UNCHANGED: 277 case UNCHANGED:
277 return isolate_->heap()->undefined_value(); 278 return isolate_->heap()->undefined_value();
278 case SUCCESS: { 279 case SUCCESS: {
(...skipping 23 matching lines...) Expand all
302 if (worst_case_length > 32 * KB) { // Slow path if too large. 303 if (worst_case_length > 32 * KB) { // Slow path if too large.
303 BasicJsonStringifier stringifier(isolate); 304 BasicJsonStringifier stringifier(isolate);
304 return stringifier.Stringify(object); 305 return stringifier.Stringify(object);
305 } 306 }
306 307
307 FlattenString(object); 308 FlattenString(object);
308 ASSERT(object->IsFlat()); 309 ASSERT(object->IsFlat());
309 if (object->IsOneByteRepresentationUnderneath()) { 310 if (object->IsOneByteRepresentationUnderneath()) {
310 Handle<String> result = 311 Handle<String> result =
311 isolate->factory()->NewRawOneByteString(worst_case_length); 312 isolate->factory()->NewRawOneByteString(worst_case_length);
313 ASSERT(!result.is_null());
312 DisallowHeapAllocation no_gc; 314 DisallowHeapAllocation no_gc;
313 return StringifyString_<SeqOneByteString>( 315 return StringifyString_<SeqOneByteString>(
314 isolate, 316 isolate,
315 object->GetFlatContent().ToOneByteVector(), 317 object->GetFlatContent().ToOneByteVector(),
316 result); 318 result);
317 } else { 319 } else {
318 Handle<String> result = 320 Handle<String> result =
319 isolate->factory()->NewRawTwoByteString(worst_case_length); 321 isolate->factory()->NewRawTwoByteString(worst_case_length);
322 ASSERT(!result.is_null());
320 DisallowHeapAllocation no_gc; 323 DisallowHeapAllocation no_gc;
321 return StringifyString_<SeqTwoByteString>( 324 return StringifyString_<SeqTwoByteString>(
322 isolate, 325 isolate,
323 object->GetFlatContent().ToUC16Vector(), 326 object->GetFlatContent().ToUC16Vector(),
324 result); 327 result);
325 } 328 }
326 } 329 }
327 330
328 331
329 template <typename ResultType, typename Char> 332 template <typename ResultType, typename Char>
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after
715 void BasicJsonStringifier::ShrinkCurrentPart() { 718 void BasicJsonStringifier::ShrinkCurrentPart() {
716 ASSERT(current_index_ < part_length_); 719 ASSERT(current_index_ < part_length_);
717 current_part_ = SeqString::Truncate(Handle<SeqString>::cast(current_part_), 720 current_part_ = SeqString::Truncate(Handle<SeqString>::cast(current_part_),
718 current_index_); 721 current_index_);
719 } 722 }
720 723
721 724
722 void BasicJsonStringifier::Accumulate() { 725 void BasicJsonStringifier::Accumulate() {
723 if (accumulator()->length() + current_part_->length() > String::kMaxLength) { 726 if (accumulator()->length() + current_part_->length() > String::kMaxLength) {
724 // Screw it. Simply set the flag and carry on. Throw exception at the end. 727 // 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()); 728 set_accumulator(factory_->empty_string());
727 overflowed_ = true; 729 overflowed_ = true;
728 } else { 730 } else {
729 set_accumulator(factory_->NewConsString(accumulator(), current_part_)); 731 set_accumulator(factory_->NewConsString(accumulator(), current_part_));
730 } 732 }
731 } 733 }
732 734
733 735
734 void BasicJsonStringifier::Extend() { 736 void BasicJsonStringifier::Extend() {
735 Accumulate(); 737 Accumulate();
736 if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) { 738 if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) {
737 part_length_ *= kPartLengthGrowthFactor; 739 part_length_ *= kPartLengthGrowthFactor;
738 } 740 }
739 if (is_ascii_) { 741 if (is_ascii_) {
740 current_part_ = factory_->NewRawOneByteString(part_length_); 742 current_part_ = factory_->NewRawOneByteString(part_length_);
741 } else { 743 } else {
742 current_part_ = factory_->NewRawTwoByteString(part_length_); 744 current_part_ = factory_->NewRawTwoByteString(part_length_);
743 } 745 }
746 ASSERT(!current_part_.is_null());
744 current_index_ = 0; 747 current_index_ = 0;
745 } 748 }
746 749
747 750
748 void BasicJsonStringifier::ChangeEncoding() { 751 void BasicJsonStringifier::ChangeEncoding() {
749 ShrinkCurrentPart(); 752 ShrinkCurrentPart();
750 Accumulate(); 753 Accumulate();
751 current_part_ = factory_->NewRawTwoByteString(part_length_); 754 current_part_ = factory_->NewRawTwoByteString(part_length_);
755 ASSERT(!current_part_.is_null());
752 current_index_ = 0; 756 current_index_ = 0;
753 is_ascii_ = false; 757 is_ascii_ = false;
754 } 758 }
755 759
756 760
757 template <typename SrcChar, typename DestChar> 761 template <typename SrcChar, typename DestChar>
758 int BasicJsonStringifier::SerializeStringUnchecked_(const SrcChar* src, 762 int BasicJsonStringifier::SerializeStringUnchecked_(const SrcChar* src,
759 DestChar* dest, 763 DestChar* dest,
760 int length) { 764 int length) {
761 DestChar* dest_start = dest; 765 DestChar* dest_start = dest;
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
870 SerializeString_<false, uint8_t>(object); 874 SerializeString_<false, uint8_t>(object);
871 } else { 875 } else {
872 SerializeString_<false, uc16>(object); 876 SerializeString_<false, uc16>(object);
873 } 877 }
874 } 878 }
875 } 879 }
876 880
877 } } // namespace v8::internal 881 } } // namespace v8::internal
878 882
879 #endif // V8_JSON_STRINGIFIER_H_ 883 #endif // V8_JSON_STRINGIFIER_H_
OLDNEW
« no previous file with comments | « src/json-parser.h ('k') | src/jsregexp.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698