| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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 #include "src/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
| 6 | 6 |
| 7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
| 8 #include "src/code-stubs.h" | 8 #include "src/code-stubs.h" |
| 9 #include "src/conversions-inl.h" | 9 #include "src/conversions-inl.h" |
| 10 #include "src/elements.h" | 10 #include "src/elements.h" |
| (...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, element_k, | 628 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, element_k, |
| 629 Object::GetProperty(&it)); | 629 Object::GetProperty(&it)); |
| 630 if (search_element->StrictEquals(*element_k)) { | 630 if (search_element->StrictEquals(*element_k)) { |
| 631 return *index_obj; | 631 return *index_obj; |
| 632 } | 632 } |
| 633 } | 633 } |
| 634 } | 634 } |
| 635 return Smi::FromInt(-1); | 635 return Smi::FromInt(-1); |
| 636 } | 636 } |
| 637 | 637 |
| 638 RUNTIME_FUNCTION(Runtime_SpreadIterablePrepare) { | 638 namespace { |
| 639 HandleScope scope(isolate); | |
| 640 DCHECK_EQ(1, args.length()); | |
| 641 CONVERT_ARG_HANDLE_CHECKED(Object, spread, 0); | |
| 642 | 639 |
| 640 bool MustIterate(Isolate* isolate, Handle<Object> spread) { |
| 643 if (spread->IsJSArray()) { | 641 if (spread->IsJSArray()) { |
| 644 // Check that the spread arg has fast elements | 642 // Check that the spread arg has fast elements |
| 645 Handle<JSArray> spread_array = Handle<JSArray>::cast(spread); | 643 Handle<JSArray> spread_array = Handle<JSArray>::cast(spread); |
| 646 ElementsKind array_kind = spread_array->GetElementsKind(); | 644 ElementsKind array_kind = spread_array->GetElementsKind(); |
| 647 | 645 |
| 648 // And that it has the orignal ArrayPrototype | 646 // And that it has the orignal ArrayPrototype |
| 649 JSObject* array_proto = JSObject::cast(spread_array->map()->prototype()); | 647 JSObject* array_proto = JSObject::cast(spread_array->map()->prototype()); |
| 650 Map* iterator_map = isolate->initial_array_iterator_prototype()->map(); | 648 Map* iterator_map = isolate->initial_array_iterator_prototype()->map(); |
| 651 | 649 |
| 652 // Check that the iterator acts as expected. | 650 // Check that the iterator acts as expected. |
| 653 // If IsArrayIteratorLookupChainIntact(), then we know that the initial | 651 // If IsArrayIteratorLookupChainIntact(), then we know that the initial |
| 654 // ArrayIterator is being used. If the map of the prototype has changed, | 652 // ArrayIterator is being used. If the map of the prototype has changed, |
| 655 // then take the slow path. | 653 // then take the slow path. |
| 656 | |
| 657 if (isolate->is_initial_array_prototype(array_proto) && | 654 if (isolate->is_initial_array_prototype(array_proto) && |
| 658 isolate->IsArrayIteratorLookupChainIntact() && | 655 isolate->IsArrayIteratorLookupChainIntact() && |
| 659 isolate->is_initial_array_iterator_prototype_map(iterator_map)) { | 656 isolate->is_initial_array_iterator_prototype_map(iterator_map)) { |
| 660 if (IsFastPackedElementsKind(array_kind)) { | 657 if (IsFastPackedElementsKind(array_kind)) { |
| 661 return *spread; | 658 return false; |
| 662 } | 659 } |
| 663 if (IsFastHoleyElementsKind(array_kind) && | 660 if (IsFastHoleyElementsKind(array_kind) && |
| 664 isolate->IsFastArrayConstructorPrototypeChainIntact()) { | 661 isolate->IsFastArrayConstructorPrototypeChainIntact()) { |
| 665 return *spread; | 662 return false; |
| 666 } | 663 } |
| 667 } | 664 } |
| 668 } | 665 } |
| 666 return true; |
| 667 } |
| 668 |
| 669 } // namespace |
| 670 |
| 671 RUNTIME_FUNCTION(Runtime_SpreadIterablePrepare) { |
| 672 HandleScope scope(isolate); |
| 673 DCHECK_EQ(1, args.length()); |
| 674 CONVERT_ARG_HANDLE_CHECKED(Object, spread, 0); |
| 675 |
| 676 if (!MustIterate(isolate, spread)) { |
| 677 return *spread; |
| 678 } |
| 669 | 679 |
| 670 Handle<JSFunction> spread_iterable_function = isolate->spread_iterable(); | 680 Handle<JSFunction> spread_iterable_function = isolate->spread_iterable(); |
| 671 | 681 |
| 672 Handle<Object> spreaded; | 682 Handle<Object> spreaded; |
| 673 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 683 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 674 isolate, spreaded, | 684 isolate, spreaded, |
| 675 Execution::Call(isolate, spread_iterable_function, | 685 Execution::Call(isolate, spread_iterable_function, |
| 676 isolate->factory()->undefined_value(), 1, &spread)); | 686 isolate->factory()->undefined_value(), 1, &spread)); |
| 677 | 687 |
| 678 return *spreaded; | 688 return *spreaded; |
| 679 } | 689 } |
| 680 | 690 |
| 691 RUNTIME_FUNCTION(Runtime_SpreadIterablePrepareVarargs) { |
| 692 HandleScope scope(isolate); |
| 693 DCHECK_LE(2, args.length()); |
| 694 CONVERT_ARG_HANDLE_CHECKED(Object, spread, args.length() - 1); |
| 695 |
| 696 Handle<FixedArray> result = isolate->factory()->NewFixedArray(args.length()); |
| 697 for (int i = 0; i < args.length() - 1; i++) { |
| 698 result->set(i, *args.at<Object>(i)); |
| 699 } |
| 700 // Iterate over the spread if we need to. |
| 701 if (MustIterate(isolate, spread)) { |
| 702 Handle<JSFunction> spread_iterable_function = isolate->spread_iterable(); |
| 703 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 704 isolate, spread, |
| 705 Execution::Call(isolate, spread_iterable_function, |
| 706 isolate->factory()->undefined_value(), 1, &spread)); |
| 707 } |
| 708 JSArray* spread_array = JSArray::cast(*spread); |
| 709 uint32_t length; |
| 710 CHECK(spread_array->length()->ToArrayIndex(&length)); |
| 711 for (uint32_t i = 0; i < length; i++) { |
| 712 LookupIterator it(isolate, spread, i); |
| 713 result = FixedArray::SetAndGrow(result, args.length() - 1 + i, |
| 714 spread_array->GetDataProperty(&it)); |
| 715 } |
| 716 |
| 717 Handle<JSArray> r = isolate->factory()->NewJSArrayWithElements( |
| 718 result, FAST_ELEMENTS, args.length() - 1 + length); |
| 719 return *r; |
| 720 } |
| 721 |
| 681 } // namespace internal | 722 } // namespace internal |
| 682 } // namespace v8 | 723 } // namespace v8 |
| OLD | NEW |