Chromium Code Reviews| 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 Handle<FixedArray> elements(FixedArray::cast(object->elements()), | |
| 442 isolate_); | |
| 443 for (uint32_t i = 0; i < length; i++) { | |
| 444 if (i > 0) builder_.AppendCharacter(','); | |
| 445 SerializeSmi(Smi::cast(elements->get(i))); | |
| 446 } | |
| 447 break; | |
| 448 } | |
| 449 case FAST_DOUBLE_ELEMENTS: { | |
| 450 // Empty array is FixedArray but not FixedDoubleArray. | |
| 451 if (length == 0) break; | |
| 452 Handle<FixedDoubleArray> elements( | |
| 453 FixedDoubleArray::cast(object->elements()), isolate_); | |
| 454 for (uint32_t i = 0; i < length; i++) { | |
| 455 if (i > 0) builder_.AppendCharacter(','); | |
| 456 SerializeDouble(elements->get_scalar(i)); | |
| 457 } | |
| 458 break; | |
| 459 } | |
| 460 case FAST_ELEMENTS: { | |
| 461 Handle<Object> old_length(object->length(), isolate_); | |
| 462 for (uint32_t i = 0; i < length; i++) { | |
| 463 if (object->length() != *old_length || | |
| 464 object->GetElementsKind() != FAST_ELEMENTS) { | |
| 465 Result result = SerializeJSArraySlow(object, i, length); | |
| 466 if (result != SUCCESS) return result; | |
| 467 break; | |
| 468 } | |
| 469 if (i > 0) builder_.AppendCharacter(','); | |
| 470 Result result = SerializeElement( | |
| 471 isolate_, | |
| 472 Handle<Object>(FixedArray::cast(object->elements())->get(i), | |
|
Toon Verwaest
2015/11/13 09:33:23
handle(...) works too.
| |
| 473 isolate_), | |
| 474 i); | |
| 475 if (result == SUCCESS) continue; | |
| 476 if (result == UNCHANGED) { | |
| 477 builder_.AppendCString("null"); | |
| 478 } else { | |
| 479 return result; | |
| 480 } | |
| 481 } | |
| 482 break; | |
| 483 } | |
| 484 // The FAST_HOLEY_* cases could be handled in a faster way. They resemble | |
| 485 // the non-holey cases except that a lookup is necessary for holes. | |
| 486 default: { | |
| 487 Result result = SerializeJSArraySlow(object, 0, length); | |
| 488 if (result != SUCCESS) return result; | |
| 489 break; | |
| 490 } | |
| 491 } | |
| 440 builder_.AppendCharacter(']'); | 492 builder_.AppendCharacter(']'); |
| 441 StackPop(); | 493 StackPop(); |
| 442 return SUCCESS; | 494 return SUCCESS; |
| 443 } | 495 } |
| 444 | 496 |
| 445 | 497 |
| 446 BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArraySlow( | 498 BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArraySlow( |
| 447 Handle<JSArray> object, uint32_t length) { | 499 Handle<JSArray> object, uint32_t start, uint32_t length) { |
| 448 for (uint32_t i = 0; i < length; i++) { | 500 for (uint32_t i = start; i < length; i++) { |
| 449 if (i > 0) builder_.AppendCharacter(','); | 501 if (i > 0) builder_.AppendCharacter(','); |
| 450 Handle<Object> element; | 502 Handle<Object> element; |
| 451 ASSIGN_RETURN_ON_EXCEPTION_VALUE( | 503 ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 452 isolate_, element, | 504 isolate_, element, |
| 453 Object::GetElement(isolate_, object, i), | 505 Object::GetElement(isolate_, object, i), |
| 454 EXCEPTION); | 506 EXCEPTION); |
| 455 if (element->IsUndefined()) { | 507 if (element->IsUndefined()) { |
| 456 builder_.AppendCString("null"); | 508 builder_.AppendCString("null"); |
| 457 } else { | 509 } else { |
| 458 Result result = SerializeElement(isolate_, element, i); | 510 Result result = SerializeElement(isolate_, element, i); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 627 } else { | 679 } else { |
| 628 SerializeString_<uc16, uc16>(object); | 680 SerializeString_<uc16, uc16>(object); |
| 629 } | 681 } |
| 630 } | 682 } |
| 631 } | 683 } |
| 632 | 684 |
| 633 } // namespace internal | 685 } // namespace internal |
| 634 } // namespace v8 | 686 } // namespace v8 |
| 635 | 687 |
| 636 #endif // V8_JSON_STRINGIFIER_H_ | 688 #endif // V8_JSON_STRINGIFIER_H_ |
| OLD | NEW |