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 24 matching lines...) Expand all Loading... |
35 namespace v8 { | 35 namespace v8 { |
36 namespace internal { | 36 namespace internal { |
37 | 37 |
38 class BasicJsonStringifier BASE_EMBEDDED { | 38 class BasicJsonStringifier BASE_EMBEDDED { |
39 public: | 39 public: |
40 explicit BasicJsonStringifier(Isolate* isolate); | 40 explicit BasicJsonStringifier(Isolate* isolate); |
41 | 41 |
42 MaybeObject* Stringify(Handle<Object> object); | 42 MaybeObject* Stringify(Handle<Object> object); |
43 | 43 |
44 private: | 44 private: |
45 static const int kPartLength = 8 * 1024; | 45 static const int kInitialPartLength = 32; |
| 46 static const int kMaxPartLength = 16 * 1024; |
| 47 static const int kPartLengthGrowthFactor = 2; |
46 static const int kStackLimit = 8 * 1024; | 48 static const int kStackLimit = 8 * 1024; |
47 | 49 |
48 enum Result { UNCHANGED, SUCCESS, BAILOUT, CIRCULAR, STACK_OVERFLOW }; | 50 enum Result { UNCHANGED, SUCCESS, BAILOUT, CIRCULAR, STACK_OVERFLOW }; |
49 | 51 |
50 template <bool is_ascii> void Extend(); | 52 template <bool is_ascii> void Extend(); |
51 | 53 |
52 void ChangeEncoding(); | 54 void ChangeEncoding(); |
53 | 55 |
54 void ShrinkCurrentPart(); | 56 void ShrinkCurrentPart(); |
55 | 57 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 Result SerializeObject(Handle<JSObject> object); | 125 Result SerializeObject(Handle<JSObject> object); |
124 | 126 |
125 void SerializeString(Handle<String> object); | 127 void SerializeString(Handle<String> object); |
126 | 128 |
127 template <bool is_ascii, typename Char> | 129 template <bool is_ascii, typename Char> |
128 INLINE(void SerializeString_(Vector<const Char> vector)); | 130 INLINE(void SerializeString_(Vector<const Char> vector)); |
129 | 131 |
130 INLINE(Result StackPush(Handle<Object> object)); | 132 INLINE(Result StackPush(Handle<Object> object)); |
131 INLINE(void StackPop()); | 133 INLINE(void StackPop()); |
132 | 134 |
| 135 INLINE(Handle<String> accumulator()) { |
| 136 return Handle<String>(String::cast(accumulator_store_->value())); |
| 137 } |
| 138 |
| 139 INLINE(void set_accumulator(Handle<String> string)) { |
| 140 return accumulator_store_->set_value(*string); |
| 141 } |
| 142 |
133 Isolate* isolate_; | 143 Isolate* isolate_; |
134 Handle<String> accumulator_; | 144 // We use a value wrapper for the string accumulator to keep the |
| 145 // (indirect) handle to it in the outermost handle scope. |
| 146 Handle<JSValue> accumulator_store_; |
135 Handle<String> current_part_; | 147 Handle<String> current_part_; |
136 Handle<String> tojson_symbol_; | 148 Handle<String> tojson_symbol_; |
137 Handle<JSArray> stack_; | 149 Handle<JSArray> stack_; |
138 int current_index_; | 150 int current_index_; |
| 151 int part_length_; |
139 bool is_ascii_; | 152 bool is_ascii_; |
140 | 153 |
141 static const int kJsonQuotesCharactersPerEntry = 8; | 154 static const int kJsonQuotesCharactersPerEntry = 8; |
142 static const char* const JsonQuotes; | 155 static const char* const JsonQuotes; |
143 }; | 156 }; |
144 | 157 |
145 | 158 |
146 const char* const BasicJsonStringifier::JsonQuotes = | 159 const char* const BasicJsonStringifier::JsonQuotes = |
147 "\\u0000\0 \\u0001\0 \\u0002\0 \\u0003\0 " | 160 "\\u0000\0 \\u0001\0 \\u0002\0 \\u0003\0 " |
148 "\\u0004\0 \\u0005\0 \\u0006\0 \\u0007\0 " | 161 "\\u0004\0 \\u0005\0 \\u0006\0 \\u0007\0 " |
(...skipping 24 matching lines...) Expand all Loading... |
173 "h\0 i\0 j\0 k\0 " | 186 "h\0 i\0 j\0 k\0 " |
174 "l\0 m\0 n\0 o\0 " | 187 "l\0 m\0 n\0 o\0 " |
175 "p\0 q\0 r\0 s\0 " | 188 "p\0 q\0 r\0 s\0 " |
176 "t\0 u\0 v\0 w\0 " | 189 "t\0 u\0 v\0 w\0 " |
177 "x\0 y\0 z\0 {\0 " | 190 "x\0 y\0 z\0 {\0 " |
178 "|\0 }\0 ~\0 \177\0 "; | 191 "|\0 }\0 ~\0 \177\0 "; |
179 | 192 |
180 | 193 |
181 BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate) | 194 BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate) |
182 : isolate_(isolate), current_index_(0), is_ascii_(true) { | 195 : isolate_(isolate), current_index_(0), is_ascii_(true) { |
183 accumulator_ = isolate_->factory()->empty_string(); | 196 accumulator_store_ = Handle<JSValue>::cast( |
| 197 isolate_->factory()->ToObject(isolate_->factory()->empty_string())); |
| 198 part_length_ = kInitialPartLength; |
184 current_part_ = | 199 current_part_ = |
185 isolate_->factory()->NewRawAsciiString(kPartLength); | 200 isolate_->factory()->NewRawAsciiString(kInitialPartLength); |
186 tojson_symbol_ = isolate_->factory()->LookupAsciiSymbol("toJSON"); | 201 tojson_symbol_ = isolate_->factory()->LookupAsciiSymbol("toJSON"); |
187 stack_ = isolate_->factory()->NewJSArray(8); | 202 stack_ = isolate_->factory()->NewJSArray(8); |
188 } | 203 } |
189 | 204 |
190 | 205 |
191 MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) { | 206 MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) { |
192 switch (Serialize(object)) { | 207 switch (Serialize(object)) { |
193 case SUCCESS: | 208 case SUCCESS: |
194 ShrinkCurrentPart(); | 209 ShrinkCurrentPart(); |
195 return *isolate_->factory()->NewConsString(accumulator_, current_part_); | 210 return *isolate_->factory()->NewConsString(accumulator(), current_part_); |
196 case UNCHANGED: | 211 case UNCHANGED: |
197 return isolate_->heap()->undefined_value(); | 212 return isolate_->heap()->undefined_value(); |
198 case CIRCULAR: | 213 case CIRCULAR: |
199 return isolate_->Throw(*isolate_->factory()->NewTypeError( | 214 return isolate_->Throw(*isolate_->factory()->NewTypeError( |
200 "circular_structure", HandleVector<Object>(NULL, 0))); | 215 "circular_structure", HandleVector<Object>(NULL, 0))); |
201 case STACK_OVERFLOW: | 216 case STACK_OVERFLOW: |
202 return isolate_->StackOverflow(); | 217 return isolate_->StackOverflow(); |
203 default: | 218 default: |
204 return Smi::FromInt(0); | 219 return Smi::FromInt(0); |
205 } | 220 } |
206 } | 221 } |
207 | 222 |
208 | 223 |
209 template <bool is_ascii, typename Char> | 224 template <bool is_ascii, typename Char> |
210 void BasicJsonStringifier::Append_(Char c) { | 225 void BasicJsonStringifier::Append_(Char c) { |
211 if (is_ascii) { | 226 if (is_ascii) { |
212 SeqAsciiString::cast(*current_part_)->SeqAsciiStringSet( | 227 SeqAsciiString::cast(*current_part_)->SeqAsciiStringSet( |
213 current_index_++, c); | 228 current_index_++, c); |
214 } else { | 229 } else { |
215 SeqTwoByteString::cast(*current_part_)->SeqTwoByteStringSet( | 230 SeqTwoByteString::cast(*current_part_)->SeqTwoByteStringSet( |
216 current_index_++, c); | 231 current_index_++, c); |
217 } | 232 } |
218 if (current_index_ == kPartLength) Extend<is_ascii>(); | 233 if (current_index_ == part_length_) Extend<is_ascii>(); |
219 } | 234 } |
220 | 235 |
221 | 236 |
222 template <bool is_ascii, typename Char> | 237 template <bool is_ascii, typename Char> |
223 void BasicJsonStringifier::AppendUnchecked_(Char c) { | 238 void BasicJsonStringifier::AppendUnchecked_(Char c) { |
224 if (is_ascii) { | 239 if (is_ascii) { |
225 SeqAsciiString::cast(*current_part_)->SeqAsciiStringSet( | 240 SeqAsciiString::cast(*current_part_)->SeqAsciiStringSet( |
226 current_index_++, c); | 241 current_index_++, c); |
227 } else { | 242 } else { |
228 SeqTwoByteString::cast(*current_part_)->SeqTwoByteStringSet( | 243 SeqTwoByteString::cast(*current_part_)->SeqTwoByteStringSet( |
229 current_index_++, c); | 244 current_index_++, c); |
230 } | 245 } |
231 ASSERT(current_index_ < kPartLength); | 246 ASSERT(current_index_ < part_length_); |
232 } | 247 } |
233 | 248 |
234 | 249 |
235 template <bool is_ascii, typename Char> | 250 template <bool is_ascii, typename Char> |
236 void BasicJsonStringifier::Append_(Char* chars) { | 251 void BasicJsonStringifier::Append_(Char* chars) { |
237 for ( ; *chars != '\0'; chars++) Append_<is_ascii>(*chars); | 252 for ( ; *chars != '\0'; chars++) Append_<is_ascii>(*chars); |
238 } | 253 } |
239 | 254 |
240 | 255 |
241 template <bool is_ascii, typename Char> | 256 template <bool is_ascii, typename Char> |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 static const int kBufferSize = 100; | 411 static const int kBufferSize = 100; |
397 char chars[kBufferSize]; | 412 char chars[kBufferSize]; |
398 Vector<char> buffer(chars, kBufferSize); | 413 Vector<char> buffer(chars, kBufferSize); |
399 Append(DoubleToCString(number, buffer)); | 414 Append(DoubleToCString(number, buffer)); |
400 return SUCCESS; | 415 return SUCCESS; |
401 } | 416 } |
402 | 417 |
403 | 418 |
404 BasicJsonStringifier::Result BasicJsonStringifier::SerializeArray( | 419 BasicJsonStringifier::Result BasicJsonStringifier::SerializeArray( |
405 Handle<JSArray> object) { | 420 Handle<JSArray> object) { |
| 421 HandleScope handle_scope(isolate_); |
406 if (StackPush(object) == CIRCULAR) return CIRCULAR; | 422 if (StackPush(object) == CIRCULAR) return CIRCULAR; |
407 int length = Smi::cast(object->length())->value(); | 423 int length = Smi::cast(object->length())->value(); |
408 Append('['); | 424 Append('['); |
409 switch (object->GetElementsKind()) { | 425 switch (object->GetElementsKind()) { |
410 case FAST_SMI_ELEMENTS: { | 426 case FAST_SMI_ELEMENTS: { |
411 Handle<FixedArray> elements = Handle<FixedArray>( | 427 Handle<FixedArray> elements = Handle<FixedArray>( |
412 FixedArray::cast(object->elements())); | 428 FixedArray::cast(object->elements())); |
413 for (int i = 0; i < length; i++) { | 429 for (int i = 0; i < length; i++) { |
414 if (i > 0) Append(','); | 430 if (i > 0) Append(','); |
415 SerializeSmi(Smi::cast(elements->get(i))); | 431 SerializeSmi(Smi::cast(elements->get(i))); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 return result; | 469 return result; |
454 } | 470 } |
455 } | 471 } |
456 break; | 472 break; |
457 } | 473 } |
458 default: | 474 default: |
459 return BAILOUT; | 475 return BAILOUT; |
460 } | 476 } |
461 Append(']'); | 477 Append(']'); |
462 StackPop(); | 478 StackPop(); |
| 479 current_part_ = handle_scope.CloseAndEscape(current_part_); |
463 return SUCCESS; | 480 return SUCCESS; |
464 } | 481 } |
465 | 482 |
466 | 483 |
467 BasicJsonStringifier::Result BasicJsonStringifier::SerializeObject( | 484 BasicJsonStringifier::Result BasicJsonStringifier::SerializeObject( |
468 Handle<JSObject> object) { | 485 Handle<JSObject> object) { |
| 486 HandleScope handle_scope(isolate_); |
469 Result stack_push = StackPush(object); | 487 Result stack_push = StackPush(object); |
470 if (stack_push != SUCCESS) return stack_push; | 488 if (stack_push != SUCCESS) return stack_push; |
471 if (object->IsJSGlobalProxy()) return BAILOUT; | 489 if (object->IsJSGlobalProxy()) return BAILOUT; |
472 bool threw = false; | 490 bool threw = false; |
473 Handle<FixedArray> contents = | 491 Handle<FixedArray> contents = |
474 GetKeysInFixedArrayFor(object, LOCAL_ONLY, &threw); | 492 GetKeysInFixedArrayFor(object, LOCAL_ONLY, &threw); |
475 if (threw) return BAILOUT; | 493 if (threw) return BAILOUT; |
476 Append('{'); | 494 Append('{'); |
477 int length = contents->length(); | 495 int length = contents->length(); |
478 bool comma = false; | 496 bool comma = false; |
(...skipping 16 matching lines...) Expand all Loading... |
495 property = GetProperty(object, key_handle); | 513 property = GetProperty(object, key_handle); |
496 } | 514 } |
497 } | 515 } |
498 if (property.is_null()) return BAILOUT; | 516 if (property.is_null()) return BAILOUT; |
499 Result result = SerializeDeferred(property, comma, key_handle); | 517 Result result = SerializeDeferred(property, comma, key_handle); |
500 if (!comma && result == SUCCESS) comma = true; | 518 if (!comma && result == SUCCESS) comma = true; |
501 if (result >= BAILOUT) return result; | 519 if (result >= BAILOUT) return result; |
502 } | 520 } |
503 Append('}'); | 521 Append('}'); |
504 StackPop(); | 522 StackPop(); |
| 523 current_part_ = handle_scope.CloseAndEscape(current_part_); |
505 return SUCCESS; | 524 return SUCCESS; |
506 } | 525 } |
507 | 526 |
508 | 527 |
509 void BasicJsonStringifier::ShrinkCurrentPart() { | 528 void BasicJsonStringifier::ShrinkCurrentPart() { |
510 ASSERT(current_index_ < kPartLength); | 529 ASSERT(current_index_ < part_length_); |
511 if (current_index_ == 0) { | 530 if (current_index_ == 0) { |
512 current_part_ = isolate_->factory()->empty_string(); | 531 current_part_ = isolate_->factory()->empty_string(); |
513 return; | 532 return; |
514 } | 533 } |
515 | 534 |
516 int string_size, allocated_string_size; | 535 int string_size, allocated_string_size; |
517 if (is_ascii_) { | 536 if (is_ascii_) { |
518 allocated_string_size = SeqAsciiString::SizeFor(kPartLength); | 537 allocated_string_size = SeqAsciiString::SizeFor(part_length_); |
519 string_size = SeqAsciiString::SizeFor(current_index_); | 538 string_size = SeqAsciiString::SizeFor(current_index_); |
520 } else { | 539 } else { |
521 allocated_string_size = SeqTwoByteString::SizeFor(kPartLength); | 540 allocated_string_size = SeqTwoByteString::SizeFor(part_length_); |
522 string_size = SeqTwoByteString::SizeFor(current_index_); | 541 string_size = SeqTwoByteString::SizeFor(current_index_); |
523 } | 542 } |
524 | 543 |
525 int delta = allocated_string_size - string_size; | 544 int delta = allocated_string_size - string_size; |
526 current_part_->set_length(current_index_); | 545 current_part_->set_length(current_index_); |
527 | 546 |
528 // String sizes are pointer size aligned, so that we can use filler objects | 547 // String sizes are pointer size aligned, so that we can use filler objects |
529 // that are a multiple of pointer size. | 548 // that are a multiple of pointer size. |
530 Address end_of_string = current_part_->address() + string_size; | 549 Address end_of_string = current_part_->address() + string_size; |
531 isolate_->heap()->CreateFillerObjectAt(end_of_string, delta); | 550 isolate_->heap()->CreateFillerObjectAt(end_of_string, delta); |
532 if (Marking::IsBlack(Marking::MarkBitFrom(*current_part_))) { | 551 if (Marking::IsBlack(Marking::MarkBitFrom(*current_part_))) { |
533 MemoryChunk::IncrementLiveBytesFromMutator( | 552 MemoryChunk::IncrementLiveBytesFromMutator( |
534 current_part_->address(), -delta); | 553 current_part_->address(), -delta); |
535 } | 554 } |
536 } | 555 } |
537 | 556 |
538 | 557 |
539 template <bool is_ascii> | 558 template <bool is_ascii> |
540 void BasicJsonStringifier::Extend() { | 559 void BasicJsonStringifier::Extend() { |
541 accumulator_ = | 560 set_accumulator( |
542 isolate_->factory()->NewConsString(accumulator_, current_part_); | 561 isolate_->factory()->NewConsString(accumulator(), current_part_)); |
| 562 if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) { |
| 563 part_length_ *= kPartLengthGrowthFactor; |
| 564 } |
543 if (is_ascii) { | 565 if (is_ascii) { |
544 current_part_ = | 566 current_part_ = |
545 isolate_->factory()->NewRawAsciiString(kPartLength); | 567 isolate_->factory()->NewRawAsciiString(part_length_); |
546 } else { | 568 } else { |
547 current_part_ = | 569 current_part_ = |
548 isolate_->factory()->NewRawTwoByteString(kPartLength); | 570 isolate_->factory()->NewRawTwoByteString(part_length_); |
549 } | 571 } |
550 current_index_ = 0; | 572 current_index_ = 0; |
551 } | 573 } |
552 | 574 |
553 | 575 |
554 void BasicJsonStringifier::ChangeEncoding() { | 576 void BasicJsonStringifier::ChangeEncoding() { |
555 ShrinkCurrentPart(); | 577 ShrinkCurrentPart(); |
556 accumulator_ = isolate_->factory()->NewConsString(accumulator_, | 578 set_accumulator( |
557 current_part_); | 579 isolate_->factory()->NewConsString(accumulator(), current_part_)); |
558 current_part_ = | 580 current_part_ = |
559 isolate_->factory()->NewRawTwoByteString(kPartLength); | 581 isolate_->factory()->NewRawTwoByteString(part_length_); |
560 current_index_ = 0; | 582 current_index_ = 0; |
561 is_ascii_ = false; | 583 is_ascii_ = false; |
562 } | 584 } |
563 | 585 |
564 | 586 |
565 template <bool is_ascii, typename Char> | 587 template <bool is_ascii, typename Char> |
566 void BasicJsonStringifier::SerializeString_(Vector<const Char> vector) { | 588 void BasicJsonStringifier::SerializeString_(Vector<const Char> vector) { |
567 int length = vector.length(); | 589 int length = vector.length(); |
568 if (current_index_ + (length << 3) < (kPartLength - 2)) { | 590 if (current_index_ + (length << 3) < (part_length_ - 2)) { |
569 AppendUnchecked_<is_ascii, char>('"'); | 591 AppendUnchecked_<is_ascii, char>('"'); |
570 for (int i = 0; i < length; i++) { | 592 for (int i = 0; i < length; i++) { |
571 Char c = vector[i]; | 593 Char c = vector[i]; |
572 if ((c >= '#' && c <= '~' && c != '\\') || | 594 if ((c >= '#' && c <= '~' && c != '\\') || |
573 (!is_ascii && ((c & 0xFF80) != 0))) { | 595 (!is_ascii && ((c & 0xFF80) != 0))) { |
574 AppendUnchecked_<is_ascii, Char>(c); | 596 AppendUnchecked_<is_ascii, Char>(c); |
575 } else { | 597 } else { |
576 AppendUnchecked_<is_ascii, char>( | 598 AppendUnchecked_<is_ascii, char>( |
577 &JsonQuotes[c * kJsonQuotesCharactersPerEntry]); | 599 &JsonQuotes[c * kJsonQuotesCharactersPerEntry]); |
578 } | 600 } |
(...skipping 30 matching lines...) Expand all Loading... |
609 SerializeString_<false, char>(flat.ToAsciiVector()); | 631 SerializeString_<false, char>(flat.ToAsciiVector()); |
610 } else { | 632 } else { |
611 SerializeString_<false, uc16>(flat.ToUC16Vector()); | 633 SerializeString_<false, uc16>(flat.ToUC16Vector()); |
612 } | 634 } |
613 } | 635 } |
614 } | 636 } |
615 | 637 |
616 } } // namespace v8::internal | 638 } } // namespace v8::internal |
617 | 639 |
618 #endif // V8_JSON_STRINGIFIER_H_ | 640 #endif // V8_JSON_STRINGIFIER_H_ |
OLD | NEW |