Commit acf695e3 authored by ishell@chromium.org's avatar ishell@chromium.org

Handlification of ArrayConstructorCommon().

R=yangguo@chromium.org

Review URL: https://codereview.chromium.org/201303009

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@20024 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent be0bc25b
......@@ -1910,78 +1910,64 @@ MUST_USE_RESULT MaybeObject* ElementsAccessorBase<ElementsAccessorSubclass,
}
// TODO(ishell): Temporary wrapper until handlified.
Handle<Object> ArrayConstructInitializeElements(Handle<JSArray> array,
Arguments* args) {
CALL_HEAP_FUNCTION(array->GetIsolate(),
ArrayConstructInitializeElements(*array, args),
Object);
}
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];
Handle<Object> obj = args->at<Object>(0);
if (obj->IsSmi()) {
int len = Smi::cast(obj)->value();
int len = Handle<Smi>::cast(obj)->value();
if (len > 0 && len < JSObject::kInitialMaxFastElementArray) {
ElementsKind elements_kind = array->GetElementsKind();
MaybeObject* maybe_array = array->Initialize(len, len);
if (maybe_array->IsFailure()) return maybe_array;
JSArray::Initialize(array, len, len);
if (!IsFastHoleyElementsKind(elements_kind)) {
elements_kind = GetHoleyElementsKind(elements_kind);
maybe_array = array->TransitionElementsKind(elements_kind);
if (maybe_array->IsFailure()) return maybe_array;
JSObject::TransitionElementsKind(array, elements_kind);
}
return array;
} else if (len == 0) {
return array->Initialize(JSArray::kPreallocatedArrayElements);
JSArray::Initialize(array, JSArray::kPreallocatedArrayElements);
return array;
}
}
// Take the argument as the length.
MaybeObject* maybe_obj = array->Initialize(0);
if (!maybe_obj->To(&obj)) return maybe_obj;
JSArray::Initialize(array, 0);
return array->SetElementsLength((*args)[0]);
return JSArray::SetElementsLength(array, obj);
}
// Optimize the case where there are no parameters passed.
if (args->length() == 0) {
return array->Initialize(JSArray::kPreallocatedArrayElements);
JSArray::Initialize(array, JSArray::kPreallocatedArrayElements);
return array;
}
Factory* factory = array->GetIsolate()->factory();
// 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;
JSObject::EnsureCanContainElements(
array, args, 0, number_of_elements, ALLOW_CONVERTED_DOUBLE_ELEMENTS);
// Allocate an appropriately typed elements array.
MaybeObject* maybe_elms;
ElementsKind elements_kind = array->GetElementsKind();
Handle<FixedArrayBase> elms;
if (IsFastDoubleElementsKind(elements_kind)) {
maybe_elms = heap->AllocateUninitializedFixedDoubleArray(
number_of_elements);
elms = Handle<FixedArrayBase>::cast(
factory->NewFixedDoubleArray(number_of_elements));
} else {
maybe_elms = heap->AllocateFixedArrayWithHoles(number_of_elements);
elms = Handle<FixedArrayBase>::cast(
factory->NewFixedArrayWithHoles(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);
Handle<FixedArray> smi_elms = Handle<FixedArray>::cast(elms);
for (int index = 0; index < number_of_elements; index++) {
smi_elms->set(index, (*args)[index], SKIP_WRITE_BARRIER);
}
......@@ -1991,7 +1977,7 @@ MUST_USE_RESULT MaybeObject* ArrayConstructInitializeElements(
case FAST_ELEMENTS: {
DisallowHeapAllocation no_gc;
WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
FixedArray* object_elms = FixedArray::cast(elms);
Handle<FixedArray> object_elms = Handle<FixedArray>::cast(elms);
for (int index = 0; index < number_of_elements; index++) {
object_elms->set(index, (*args)[index], mode);
}
......@@ -1999,7 +1985,8 @@ MUST_USE_RESULT MaybeObject* ArrayConstructInitializeElements(
}
case FAST_HOLEY_DOUBLE_ELEMENTS:
case FAST_DOUBLE_ELEMENTS: {
FixedDoubleArray* double_elms = FixedDoubleArray::cast(elms);
Handle<FixedDoubleArray> double_elms =
Handle<FixedDoubleArray>::cast(elms);
for (int index = 0; index < number_of_elements; index++) {
double_elms->set(index, (*args)[index]->Number());
}
......@@ -2010,7 +1997,7 @@ MUST_USE_RESULT MaybeObject* ArrayConstructInitializeElements(
break;
}
array->set_elements(elms);
array->set_elements(*elms);
array->set_length(Smi::FromInt(number_of_elements));
return array;
}
......
......@@ -203,9 +203,6 @@ void CheckArrayAbuse(JSObject* obj, const char* op, uint32_t key,
Handle<Object> ArrayConstructInitializeElements(Handle<JSArray> array,
Arguments* args);
MUST_USE_RESULT MaybeObject* ArrayConstructInitializeElements(
JSArray* array, Arguments* args);
} } // namespace v8::internal
#endif // V8_ELEMENTS_H_
......@@ -11348,10 +11348,11 @@ MaybeObject* JSObject::SetFastDoubleElementsCapacityAndLength(
}
MaybeObject* JSArray::Initialize(int capacity, int length) {
// static
void JSArray::Initialize(Handle<JSArray> array, int capacity, int length) {
ASSERT(capacity >= 0);
return GetHeap()->AllocateJSArrayStorage(this, length, capacity,
INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
array->GetIsolate()->factory()->NewJSArrayStorage(
array, length, capacity, INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
}
......@@ -11430,6 +11431,16 @@ static void EndPerformSplice(Handle<JSArray> object) {
}
// TODO(ishell): Temporary wrapper until handlified.
// static
Handle<Object> JSArray::SetElementsLength(Handle<JSArray> array,
Handle<Object> length) {
CALL_HEAP_FUNCTION(array->GetIsolate(),
array->SetElementsLength(*length),
Object);
}
MaybeObject* JSArray::SetElementsLength(Object* len) {
// We should never end in here with a pixel or external array.
ASSERT(AllowsSetElementsLength());
......@@ -11893,6 +11904,21 @@ Handle<Object> JSObject::SetPrototype(Handle<JSObject> object,
}
// TODO(ishell): temporary wrapper until handilfied.
// static
void JSObject::EnsureCanContainElements(Handle<JSObject> object,
Arguments* args,
uint32_t first_arg,
uint32_t arg_count,
EnsureElementsMode mode) {
CALL_HEAP_FUNCTION_VOID(object->GetIsolate(),
object->EnsureCanContainElements(args,
first_arg,
arg_count,
mode));
}
MaybeObject* JSObject::EnsureCanContainElements(Arguments* args,
uint32_t first_arg,
uint32_t arg_count,
......
......@@ -2422,6 +2422,12 @@ class JSObject: public JSReceiver {
FixedArrayBase* elements,
uint32_t length,
EnsureElementsMode mode);
static void EnsureCanContainElements(
Handle<JSObject> object,
Arguments* arguments,
uint32_t first_arg,
uint32_t arg_count,
EnsureElementsMode mode);
MUST_USE_RESULT MaybeObject* EnsureCanContainElements(
Arguments* arguments,
uint32_t first_arg,
......@@ -10011,11 +10017,13 @@ class JSArray: public JSObject {
// Initialize the array with the given capacity. The function may
// fail due to out-of-memory situations, but only if the requested
// capacity is non-zero.
MUST_USE_RESULT MaybeObject* Initialize(int capacity, int length = 0);
static void Initialize(Handle<JSArray> array, int capacity, int length = 0);
// Initializes the array to a certain length.
inline bool AllowsSetElementsLength();
// Can cause GC.
static Handle<Object> SetElementsLength(Handle<JSArray> array,
Handle<Object> length);
MUST_USE_RESULT MaybeObject* SetElementsLength(Object* length);
// Set the content of the array to the content of storage.
......
......@@ -756,7 +756,7 @@ TEST(JSArray) {
Handle<JSObject> object = factory->NewJSObject(function);
Handle<JSArray> array = Handle<JSArray>::cast(object);
// We just initialized the VM, no heap allocation failure yet.
array->Initialize(0)->ToObjectChecked();
JSArray::Initialize(array, 0);
// Set array length to 0.
array->SetElementsLength(Smi::FromInt(0))->ToObjectChecked();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment