OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_JSON_STRINGIFIER_H_ | 5 #ifndef V8_JSON_STRINGIFIER_H_ |
6 #define V8_JSON_STRINGIFIER_H_ | 6 #define V8_JSON_STRINGIFIER_H_ |
7 | 7 |
8 #include "src/conversions.h" | 8 #include "src/conversions.h" |
9 #include "src/lookup.h" | 9 #include "src/lookup.h" |
10 #include "src/messages.h" | 10 #include "src/messages.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 Result SerializeDouble(double number); | 75 Result SerializeDouble(double number); |
76 INLINE(Result SerializeHeapNumber(Handle<HeapNumber> object)) { | 76 INLINE(Result SerializeHeapNumber(Handle<HeapNumber> object)) { |
77 return SerializeDouble(object->value()); | 77 return SerializeDouble(object->value()); |
78 } | 78 } |
79 | 79 |
80 Result SerializeJSValue(Handle<JSValue> object); | 80 Result SerializeJSValue(Handle<JSValue> object); |
81 | 81 |
82 INLINE(Result SerializeJSArray(Handle<JSArray> object)); | 82 INLINE(Result SerializeJSArray(Handle<JSArray> object)); |
83 INLINE(Result SerializeJSObject(Handle<JSObject> object)); | 83 INLINE(Result SerializeJSObject(Handle<JSObject> object)); |
84 | 84 |
85 Result SerializeJSArraySlow(Handle<JSArray> object, uint32_t length); | 85 Result SerializeJSArraySlow(Handle<JSArray> object, uint32_t start, |
| 86 uint32_t length); |
86 | 87 |
87 void SerializeString(Handle<String> object); | 88 void SerializeString(Handle<String> object); |
88 | 89 |
89 template <typename SrcChar, typename DestChar> | 90 template <typename SrcChar, typename DestChar> |
90 INLINE(static void SerializeStringUnchecked_( | 91 INLINE(static void SerializeStringUnchecked_( |
91 Vector<const SrcChar> src, | 92 Vector<const SrcChar> src, |
92 IncrementalStringBuilder::NoExtend<DestChar>* dest)); | 93 IncrementalStringBuilder::NoExtend<DestChar>* dest)); |
93 | 94 |
94 template <typename SrcChar, typename DestChar> | 95 template <typename SrcChar, typename DestChar> |
95 INLINE(void SerializeString_(Handle<String> string)); | 96 INLINE(void SerializeString_(Handle<String> string)); |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 | 429 |
429 | 430 |
430 BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArray( | 431 BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArray( |
431 Handle<JSArray> object) { | 432 Handle<JSArray> object) { |
432 HandleScope handle_scope(isolate_); | 433 HandleScope handle_scope(isolate_); |
433 Result stack_push = StackPush(object); | 434 Result stack_push = StackPush(object); |
434 if (stack_push != SUCCESS) return stack_push; | 435 if (stack_push != SUCCESS) return stack_push; |
435 uint32_t length = 0; | 436 uint32_t length = 0; |
436 CHECK(object->length()->ToArrayLength(&length)); | 437 CHECK(object->length()->ToArrayLength(&length)); |
437 builder_.AppendCharacter('['); | 438 builder_.AppendCharacter('['); |
438 Result result = SerializeJSArraySlow(object, length); | 439 switch (object->GetElementsKind()) { |
439 if (result != SUCCESS) return result; | 440 case FAST_SMI_ELEMENTS: { |
| 441 DisallowJavascriptExecution no_js(isolate_); |
| 442 Handle<FixedArray> elements(FixedArray::cast(object->elements()), |
| 443 isolate_); |
| 444 for (uint32_t i = 0; i < length; i++) { |
| 445 if (i > 0) builder_.AppendCharacter(','); |
| 446 SerializeSmi(Smi::cast(elements->get(i))); |
| 447 } |
| 448 break; |
| 449 } |
| 450 case FAST_DOUBLE_ELEMENTS: { |
| 451 DisallowJavascriptExecution no_js(isolate_); |
| 452 // Empty array is FixedArray but not FixedDoubleArray. |
| 453 if (length == 0) break; |
| 454 Handle<FixedDoubleArray> elements( |
| 455 FixedDoubleArray::cast(object->elements()), isolate_); |
| 456 for (uint32_t i = 0; i < length; i++) { |
| 457 if (i > 0) builder_.AppendCharacter(','); |
| 458 SerializeDouble(elements->get_scalar(i)); |
| 459 } |
| 460 break; |
| 461 } |
| 462 case FAST_ELEMENTS: { |
| 463 Handle<FixedArray> elements(FixedArray::cast(object->elements()), |
| 464 isolate_); |
| 465 Result result; |
| 466 if (elements->length() < length) { |
| 467 result = SerializeJSArraySlow(object, 0, length); |
| 468 if (result != SUCCESS) return result; |
| 469 } else { |
| 470 JavascriptExecutionObserver observer(isolate_); |
| 471 for (uint32_t i = 0; i < length; i++) { |
| 472 if (observer.observed()) { |
| 473 result = SerializeJSArraySlow(object, i, length); |
| 474 if (result != SUCCESS) return result; |
| 475 break; |
| 476 } |
| 477 if (i > 0) builder_.AppendCharacter(','); |
| 478 result = SerializeElement( |
| 479 isolate_, Handle<Object>(elements->get(i), isolate_), i); |
| 480 if (result == SUCCESS) continue; |
| 481 if (result == UNCHANGED) { |
| 482 builder_.AppendCString("null"); |
| 483 } else { |
| 484 return result; |
| 485 } |
| 486 } |
| 487 } |
| 488 break; |
| 489 } |
| 490 // The FAST_HOLEY_* cases could be handled in a faster way. They resemble |
| 491 // the non-holey cases except that a lookup is necessary for holes. |
| 492 default: { |
| 493 Result result = SerializeJSArraySlow(object, 0, length); |
| 494 if (result != SUCCESS) return result; |
| 495 break; |
| 496 } |
| 497 } |
440 builder_.AppendCharacter(']'); | 498 builder_.AppendCharacter(']'); |
441 StackPop(); | 499 StackPop(); |
442 return SUCCESS; | 500 return SUCCESS; |
443 } | 501 } |
444 | 502 |
445 | 503 |
446 BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArraySlow( | 504 BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArraySlow( |
447 Handle<JSArray> object, uint32_t length) { | 505 Handle<JSArray> object, uint32_t start, uint32_t length) { |
448 for (uint32_t i = 0; i < length; i++) { | 506 for (uint32_t i = start; i < length; i++) { |
449 if (i > 0) builder_.AppendCharacter(','); | 507 if (i > 0) builder_.AppendCharacter(','); |
450 Handle<Object> element; | 508 Handle<Object> element; |
451 ASSIGN_RETURN_ON_EXCEPTION_VALUE( | 509 ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
452 isolate_, element, | 510 isolate_, element, |
453 Object::GetElement(isolate_, object, i), | 511 Object::GetElement(isolate_, object, i), |
454 EXCEPTION); | 512 EXCEPTION); |
455 if (element->IsUndefined()) { | 513 if (element->IsUndefined()) { |
456 builder_.AppendCString("null"); | 514 builder_.AppendCString("null"); |
457 } else { | 515 } else { |
458 Result result = SerializeElement(isolate_, element, i); | 516 Result result = SerializeElement(isolate_, element, i); |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 } else { | 685 } else { |
628 SerializeString_<uc16, uc16>(object); | 686 SerializeString_<uc16, uc16>(object); |
629 } | 687 } |
630 } | 688 } |
631 } | 689 } |
632 | 690 |
633 } // namespace internal | 691 } // namespace internal |
634 } // namespace v8 | 692 } // namespace v8 |
635 | 693 |
636 #endif // V8_JSON_STRINGIFIER_H_ | 694 #endif // V8_JSON_STRINGIFIER_H_ |
OLD | NEW |