Chromium Code Reviews| Index: src/elements.cc |
| diff --git a/src/elements.cc b/src/elements.cc |
| index d0b6c3b348579a1ed51c9def1d5810b28115c104..681a1feca53462327db9d50fb38e66999c2c61ee 100644 |
| --- a/src/elements.cc |
| +++ b/src/elements.cc |
| @@ -27,6 +27,7 @@ |
| #include "v8.h" |
| +#include "arguments.h" |
| #include "objects.h" |
| #include "elements.h" |
| #include "utils.h" |
| @@ -1973,4 +1974,110 @@ MUST_USE_RESULT MaybeObject* ElementsAccessorBase<ElementsAccessorSubclass, |
| } |
| +MUST_USE_RESULT MaybeObject* ArrayConstructInitializeElements( |
| + JSArray* array, Arguments* args) { |
| + Heap* heap = array->GetIsolate()->heap(); |
| + |
| + // Optimize the case where there is one argument and the argument is a |
| + // small smi. |
| + if (args->length() == 1) { |
| + Object* obj = (*args)[0]; |
| + if (obj->IsSmi()) { |
| + int len = Smi::cast(obj)->value(); |
| + if (len > 0 && len < JSObject::kInitialMaxFastElementArray) { |
| + FixedArrayBase* fixed_array; |
| + ElementsKind elements_kind = array->GetElementsKind(); |
| + { |
|
Toon Verwaest
2013/02/13 15:14:51
Remove { ... }.
mvstanton
2013/02/19 11:04:08
Done.
|
| + MaybeObject* maybe_obj; |
| + if (IsFastDoubleElementsKind(elements_kind)) { |
| + maybe_obj = heap->AllocateFixedDoubleArrayWithHoles(len); |
| + } else { |
| + maybe_obj = heap->AllocateFixedArrayWithHoles(len); |
| + } |
| + if (!maybe_obj->To(&fixed_array)) return maybe_obj; |
|
Toon Verwaest
2013/02/13 15:14:51
It seems like this method should be merged with Al
mvstanton
2013/02/19 11:04:08
I did this, going through helper JSArray::Initiali
|
| + } |
| + if (!IsFastHoleyElementsKind(elements_kind)) { |
| + elements_kind = GetHoleyElementsKind(elements_kind); |
| + MaybeObject* maybe_array = |
| + array->TransitionElementsKind(elements_kind); |
| + if (maybe_array->IsFailure()) return maybe_array; |
| + } |
| + // We do not use SetContent to skip the unnecessary elements type check. |
| + array->set_elements(fixed_array); |
| + array->set_length(Smi::cast(obj)); |
| + return array; |
| + } else if (len == 0) { |
| + return array->Initialize(JSArray::kPreallocatedArrayElements); |
| + } |
| + } |
| + // Take the argument as the length. |
| + { MaybeObject* maybe_obj = array->Initialize(0); |
|
Toon Verwaest
2013/02/13 15:14:51
You don't need the { .. }; and To(& rather than To
mvstanton
2013/02/19 11:04:08
Done.
|
| + if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| + } |
| + return array->SetElementsLength((*args)[0]); |
| + } |
| + |
| + // Optimize the case where there are no parameters passed. |
| + if (args->length() == 0) { |
| + return array->Initialize(JSArray::kPreallocatedArrayElements); |
| + } |
|
Toon Verwaest
2013/02/13 15:14:51
nit: I'd move this case above the == 1 case.
mvstanton
2013/02/19 11:04:08
Done.
|
| + |
| + // Set length and elements on the array. |
| + int number_of_elements = args->length(); |
| + MaybeObject* maybe_object = |
| + array->EnsureCanContainElements(args, 0, number_of_elements, |
| + ALLOW_CONVERTED_DOUBLE_ELEMENTS); |
| + if (maybe_object->IsFailure()) return maybe_object; |
| + |
| + |
| + // Allocate an appropriately typed elements array. |
| + MaybeObject* maybe_elms; |
| + ElementsKind elements_kind = array->GetElementsKind(); |
| + if (IsFastDoubleElementsKind(elements_kind)) { |
| + maybe_elms = heap->AllocateUninitializedFixedDoubleArray( |
| + number_of_elements); |
| + } else { |
| + maybe_elms = heap->AllocateFixedArrayWithHoles(number_of_elements); |
| + } |
| + FixedArrayBase* elms; |
| + if (!maybe_elms->To(&elms)) return maybe_elms; |
| + |
| + // Fill in the content |
| + switch (array->GetElementsKind()) { |
| + case FAST_HOLEY_SMI_ELEMENTS: |
| + case FAST_SMI_ELEMENTS: { |
| + FixedArray* smi_elms = FixedArray::cast(elms); |
| + for (int index = 0; index < number_of_elements; index++) { |
| + smi_elms->set(index, (*args)[index], SKIP_WRITE_BARRIER); |
| + } |
| + break; |
| + } |
| + case FAST_HOLEY_ELEMENTS: |
| + case FAST_ELEMENTS: { |
| + AssertNoAllocation no_gc; |
| + WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); |
| + FixedArray* object_elms = FixedArray::cast(elms); |
| + for (int index = 0; index < number_of_elements; index++) { |
| + object_elms->set(index, (*args)[index], mode); |
| + } |
| + break; |
| + } |
| + case FAST_HOLEY_DOUBLE_ELEMENTS: |
| + case FAST_DOUBLE_ELEMENTS: { |
| + FixedDoubleArray* double_elms = FixedDoubleArray::cast(elms); |
| + for (int index = 0; index < number_of_elements; index++) { |
| + double_elms->set(index, (*args)[index]->Number()); |
| + } |
| + break; |
| + } |
| + default: |
| + UNREACHABLE(); |
| + break; |
| + } |
| + |
| + array->set_elements(elms); |
| + array->set_length(Smi::FromInt(number_of_elements)); |
| + return array; |
| +} |
| + |
| } } // namespace v8::internal |