Convert array.length to API-style accessor.

BUG=
R=yangguo@chromium.org

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@21023 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
ulan@chromium.org 2014-04-28 14:59:29 +00:00
parent ee0cd292d7
commit 17f88aafc3
3 changed files with 66 additions and 42 deletions

View File

@ -174,15 +174,6 @@ bool Accessors::IsJSObjectFieldAccessor<HeapType>(Handle<HeapType> type,
//
Object* Accessors::ArrayGetLength(Isolate* isolate,
Object* object,
void*) {
// Traverse the prototype chain until we reach an array.
JSArray* holder = FindInstanceOf<JSArray>(isolate, object);
return holder == NULL ? Smi::FromInt(0) : holder->length();
}
// The helper function will 'flatten' Number objects.
Handle<Object> Accessors::FlattenNumber(Isolate* isolate,
Handle<Object> value) {
@ -199,55 +190,84 @@ Handle<Object> Accessors::FlattenNumber(Isolate* isolate,
}
Object* Accessors::ArraySetLength(Isolate* isolate,
JSObject* object_raw,
Object* value_raw,
void*) {
void Accessors::ArrayLengthGetter(
v8::Local<v8::String> name,
const v8::PropertyCallbackInfo<v8::Value>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
DisallowHeapAllocation no_allocation;
HandleScope scope(isolate);
Handle<JSObject> object(object_raw, isolate);
Handle<Object> value(value_raw, isolate);
Object* object = *Utils::OpenHandle(*info.This());
// Traverse the prototype chain until we reach an array.
JSArray* holder = FindInstanceOf<JSArray>(isolate, object);
Object* result;
if (holder != NULL) {
result = holder->length();
} else {
result = Smi::FromInt(0);
}
info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(result, isolate)));
}
void Accessors::ArrayLengthSetter(
v8::Local<v8::String> name,
v8::Local<v8::Value> val,
const v8::PropertyCallbackInfo<void>& info) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
HandleScope scope(isolate);
Handle<JSObject> object = Handle<JSObject>::cast(
Utils::OpenHandle(*info.This()));
Handle<Object> value = Utils::OpenHandle(*val);
// This means one of the object's prototypes is a JSArray and the
// object does not have a 'length' property. Calling SetProperty
// causes an infinite loop.
if (!object->IsJSArray()) {
Handle<Object> result;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, result,
MaybeHandle<Object> maybe_result =
JSObject::SetLocalPropertyIgnoreAttributes(
object, isolate->factory()->length_string(), value, NONE));
return *result;
object, isolate->factory()->length_string(), value, NONE);
maybe_result.ToHandleChecked();
return;
}
value = FlattenNumber(isolate, value);
Handle<JSArray> array_handle = Handle<JSArray>::cast(object);
MaybeHandle<Object> maybe;
Handle<Object> uint32_v;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, uint32_v, Execution::ToUint32(isolate, value));
maybe = Execution::ToUint32(isolate, value);
if (!maybe.ToHandle(&uint32_v)) {
isolate->OptionalRescheduleException(false);
return;
}
Handle<Object> number_v;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, number_v, Execution::ToNumber(isolate, value));
maybe = Execution::ToNumber(isolate, value);
if (!maybe.ToHandle(&number_v)) {
isolate->OptionalRescheduleException(false);
return;
}
if (uint32_v->Number() == number_v->Number()) {
Handle<Object> result;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, result,
JSArray::SetElementsLength(array_handle, uint32_v));
return *result;
MaybeHandle<Object> result;
result = JSArray::SetElementsLength(array_handle, uint32_v);
USE(result);
return;
}
return isolate->Throw(
isolate->ScheduleThrow(
*isolate->factory()->NewRangeError("invalid_array_length",
HandleVector<Object>(NULL, 0)));
}
const AccessorDescriptor Accessors::ArrayLength = {
ArrayGetLength,
ArraySetLength,
0
};
Handle<AccessorInfo> Accessors::ArrayLengthInfo(
Isolate* isolate, PropertyAttributes attributes) {
return MakeAccessor(isolate,
isolate->factory()->length_string(),
&ArrayLengthGetter,
&ArrayLengthSetter,
attributes);
}
//

View File

@ -37,9 +37,10 @@ namespace internal {
// The list of accessor descriptors. This is a second-order macro
// taking a macro to be applied to all accessor descriptor names.
#define ACCESSOR_DESCRIPTOR_LIST(V) \
V(ArrayLength)
#define ACCESSOR_INFO_LIST(V) \
V(ArrayLength) \
V(FunctionArguments) \
V(FunctionCaller) \
V(FunctionName) \

View File

@ -863,12 +863,15 @@ void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
ASSERT(initial_map->elements_kind() == GetInitialFastElementsKind());
Map::EnsureDescriptorSlack(initial_map, 1);
Handle<Foreign> array_length(factory->NewForeign(&Accessors::ArrayLength));
PropertyAttributes attribs = static_cast<PropertyAttributes>(
DONT_ENUM | DONT_DELETE);
Handle<AccessorInfo> array_length =
Accessors::ArrayLengthInfo(isolate, attribs);
{ // Add length.
CallbacksDescriptor d(factory->length_string(), array_length, attribs);
CallbacksDescriptor d(
Handle<Name>(Name::cast(array_length->name())),
array_length, attribs);
array_function->initial_map()->AppendDescriptor(&d);
}
@ -1614,14 +1617,14 @@ Handle<JSFunction> Genesis::InstallInternalArray(
// Make "length" magic on instances.
Map::EnsureDescriptorSlack(initial_map, 1);
Handle<Foreign> array_length(factory()->NewForeign(
&Accessors::ArrayLength));
PropertyAttributes attribs = static_cast<PropertyAttributes>(
DONT_ENUM | DONT_DELETE);
Handle<AccessorInfo> array_length =
Accessors::ArrayLengthInfo(isolate(), attribs);
{ // Add length.
CallbacksDescriptor d(
factory()->length_string(), array_length, attribs);
Handle<Name>(Name::cast(array_length->name())), array_length, attribs);
array_function->initial_map()->AppendDescriptor(&d);
}