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 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
618 } | 618 } |
619 | 619 |
620 // Set the length. | 620 // Set the length. |
621 array->set_length(Smi::FromInt(len - 1)); | 621 array->set_length(Smi::FromInt(len - 1)); |
622 | 622 |
623 return *first; | 623 return *first; |
624 } | 624 } |
625 | 625 |
626 | 626 |
627 BUILTIN(ArrayUnshift) { | 627 BUILTIN(ArrayUnshift) { |
| 628 HandleScope scope(isolate); |
628 Heap* heap = isolate->heap(); | 629 Heap* heap = isolate->heap(); |
629 Object* receiver = *args.receiver(); | 630 Handle<Object> receiver = args.receiver(); |
630 FixedArrayBase* elms_obj; | 631 Handle<Object> elms_or_null = |
631 MaybeObject* maybe_elms_obj = | 632 EnsureJSArrayWithWritableFastElementsWrapper(isolate, receiver, NULL, 0); |
632 EnsureJSArrayWithWritableFastElements(heap, receiver, NULL, 0); | 633 RETURN_IF_EMPTY_HANDLE(isolate, elms_or_null); |
633 if (maybe_elms_obj == NULL) | 634 if ((*elms_or_null == NULL) || |
634 return CallJsBuiltin(isolate, "ArrayUnshift", args); | 635 !IsJSArrayFastElementMovingAllowed(heap, |
635 if (!maybe_elms_obj->To(&elms_obj)) return maybe_elms_obj; | 636 *Handle<JSArray>::cast(receiver))) { |
636 | |
637 if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) { | |
638 return CallJsBuiltin(isolate, "ArrayUnshift", args); | 637 return CallJsBuiltin(isolate, "ArrayUnshift", args); |
639 } | 638 } |
640 JSArray* array = JSArray::cast(receiver); | 639 Handle<FixedArrayBase> elms_obj = Handle<FixedArrayBase>::cast(elms_or_null); |
| 640 Handle<JSArray> array = Handle<JSArray>::cast(receiver); |
641 ASSERT(!array->map()->is_observed()); | 641 ASSERT(!array->map()->is_observed()); |
642 if (!array->HasFastSmiOrObjectElements()) { | 642 if (!array->HasFastSmiOrObjectElements()) { |
643 return CallJsBuiltin(isolate, "ArrayUnshift", args); | 643 return CallJsBuiltin(isolate, "ArrayUnshift", args); |
644 } | 644 } |
645 FixedArray* elms = FixedArray::cast(elms_obj); | 645 Handle<FixedArray> elms = Handle<FixedArray>::cast(elms_obj); |
646 | 646 |
647 int len = Smi::cast(array->length())->value(); | 647 int len = Smi::cast(array->length())->value(); |
648 int to_add = args.length() - 1; | 648 int to_add = args.length() - 1; |
649 int new_length = len + to_add; | 649 int new_length = len + to_add; |
650 // Currently fixed arrays cannot grow too big, so | 650 // Currently fixed arrays cannot grow too big, so |
651 // we should never hit this case. | 651 // we should never hit this case. |
652 ASSERT(to_add <= (Smi::kMaxValue - len)); | 652 ASSERT(to_add <= (Smi::kMaxValue - len)); |
653 | 653 |
654 MaybeObject* maybe_object = | 654 JSObject::EnsureCanContainElements(array, &args, 1, to_add, |
655 array->EnsureCanContainElements(&args, 1, to_add, | 655 DONT_ALLOW_DOUBLE_ELEMENTS); |
656 DONT_ALLOW_DOUBLE_ELEMENTS); | |
657 if (maybe_object->IsFailure()) return maybe_object; | |
658 | 656 |
659 if (new_length > elms->length()) { | 657 if (new_length > elms->length()) { |
660 // New backing storage is needed. | 658 // New backing storage is needed. |
661 int capacity = new_length + (new_length >> 1) + 16; | 659 int capacity = new_length + (new_length >> 1) + 16; |
662 FixedArray* new_elms; | 660 Handle<FixedArray> new_elms = |
663 MaybeObject* maybe_elms = heap->AllocateUninitializedFixedArray(capacity); | 661 isolate->factory()->NewUninitializedFixedArray(capacity); |
664 if (!maybe_elms->To(&new_elms)) return maybe_elms; | |
665 | 662 |
666 ElementsKind kind = array->GetElementsKind(); | 663 ElementsKind kind = array->GetElementsKind(); |
667 ElementsAccessor* accessor = array->GetElementsAccessor(); | 664 ElementsAccessor* accessor = array->GetElementsAccessor(); |
668 MaybeObject* maybe_failure = accessor->CopyElements( | 665 accessor->CopyElements( |
669 NULL, 0, kind, new_elms, to_add, | 666 Handle<JSObject>::null(), 0, kind, new_elms, to_add, |
670 ElementsAccessor::kCopyToEndAndInitializeToHole, elms); | 667 ElementsAccessor::kCopyToEndAndInitializeToHole, elms); |
671 ASSERT(!maybe_failure->IsFailure()); | |
672 USE(maybe_failure); | |
673 | 668 |
674 elms = new_elms; | 669 elms = new_elms; |
675 array->set_elements(elms); | 670 array->set_elements(*elms); |
676 } else { | 671 } else { |
677 DisallowHeapAllocation no_gc; | 672 DisallowHeapAllocation no_gc; |
678 heap->MoveElements(elms, to_add, 0, len); | 673 heap->MoveElements(*elms, to_add, 0, len); |
679 } | 674 } |
680 | 675 |
681 // Add the provided values. | 676 // Add the provided values. |
682 DisallowHeapAllocation no_gc; | 677 DisallowHeapAllocation no_gc; |
683 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); | 678 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); |
684 for (int i = 0; i < to_add; i++) { | 679 for (int i = 0; i < to_add; i++) { |
685 elms->set(i, args[i + 1], mode); | 680 elms->set(i, args[i + 1], mode); |
686 } | 681 } |
687 | 682 |
688 // Set the length. | 683 // Set the length. |
(...skipping 1084 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1773 } | 1768 } |
1774 BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C) | 1769 BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C) |
1775 BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A) | 1770 BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A) |
1776 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) | 1771 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) |
1777 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) | 1772 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) |
1778 #undef DEFINE_BUILTIN_ACCESSOR_C | 1773 #undef DEFINE_BUILTIN_ACCESSOR_C |
1779 #undef DEFINE_BUILTIN_ACCESSOR_A | 1774 #undef DEFINE_BUILTIN_ACCESSOR_A |
1780 | 1775 |
1781 | 1776 |
1782 } } // namespace v8::internal | 1777 } } // namespace v8::internal |
OLD | NEW |