From e64810b5a5672f0b3a140d54986d2e02d86bb2a8 Mon Sep 17 00:00:00 2001 From: dcarney Date: Fri, 6 Feb 2015 01:44:49 -0800 Subject: [PATCH] cleanup api-natives a bit R=verwaest@chromium.org BUG= Review URL: https://codereview.chromium.org/901923002 Cr-Commit-Position: refs/heads/master@{#26481} --- src/api-natives.cc | 169 ++++++++++++++++++++++++++++++++++++----------------- src/api-natives.h | 15 +++++ src/api.cc | 73 ++++++----------------- 3 files changed, 146 insertions(+), 111 deletions(-) diff --git a/src/api-natives.cc b/src/api-natives.cc index 8233b8a..ed434ca 100644 --- a/src/api-natives.cc +++ b/src/api-natives.cc @@ -10,43 +10,71 @@ namespace internal { namespace { -// Transform getter or setter into something DefineAccessor can handle. -Handle InstantiateAccessorComponent(Isolate* isolate, - Handle component) { - if (component->IsUndefined()) return isolate->factory()->undefined_value(); - Handle info = - Handle::cast(component); - // TODO(dcarney): instantiate directly. - return Utils::OpenHandle(*Utils::ToLocal(info)->GetFunction()); +MaybeHandle InstantiateObject(Isolate* isolate, + Handle data); + + +MaybeHandle InstantiateFunction(Isolate* isolate, + Handle data, + Handle name = Handle()); + + +MaybeHandle Instantiate(Isolate* isolate, Handle data, + Handle name = Handle()) { + if (data->IsFunctionTemplateInfo()) { + return InstantiateFunction(isolate, + Handle::cast(data), name); + } else if (data->IsObjectTemplateInfo()) { + return InstantiateObject(isolate, Handle::cast(data)); + } else { + return data; + } } -MaybeHandle DefineApiAccessorProperty( +MaybeHandle DefineAccessorProperty( Isolate* isolate, Handle object, Handle name, - Handle getter, Handle setter, Smi* attribute) { + Handle getter, Handle setter, Smi* attributes) { DCHECK(PropertyDetails::AttributesField::is_valid( - static_cast(attribute->value()))); - RETURN_ON_EXCEPTION( - isolate, JSObject::DefineAccessor( - object, name, InstantiateAccessorComponent(isolate, getter), - InstantiateAccessorComponent(isolate, setter), - static_cast(attribute->value())), - Object); + static_cast(attributes->value()))); + if (!getter->IsUndefined()) { + ASSIGN_RETURN_ON_EXCEPTION( + isolate, getter, + InstantiateFunction(isolate, + Handle::cast(getter)), + Object); + } + if (!setter->IsUndefined()) { + ASSIGN_RETURN_ON_EXCEPTION( + isolate, setter, + InstantiateFunction(isolate, + Handle::cast(setter)), + Object); + } + RETURN_ON_EXCEPTION(isolate, + JSObject::DefineAccessor( + object, name, getter, setter, + static_cast(attributes->value())), + Object); return object; } -MaybeHandle AddPropertyForTemplate(Isolate* isolate, - Handle object, - Handle key, - Handle value, - Smi* unchecked_attributes) { +MaybeHandle DefineDataProperty(Isolate* isolate, + Handle object, + Handle key, + Handle prop_data, + Smi* unchecked_attributes) { DCHECK((unchecked_attributes->value() & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); // Compute attributes. PropertyAttributes attributes = static_cast(unchecked_attributes->value()); + Handle value; + ASSIGN_RETURN_ON_EXCEPTION(isolate, value, + Instantiate(isolate, prop_data, key), Object); + #ifdef DEBUG bool duplicate; if (key->IsName()) { @@ -95,29 +123,6 @@ void EnableAccessChecks(Isolate* isolate, Handle object) { } -MaybeHandle InstantiateObject(Isolate* isolate, - Handle data); - - -MaybeHandle InstantiateFunction(Isolate* isolate, - Handle data, - Handle name = Handle()); - - -MaybeHandle Instantiate(Isolate* isolate, Handle data, - Handle name = Handle()) { - if (data->IsFunctionTemplateInfo()) { - return InstantiateFunction(isolate, - Handle::cast(data), name); - } else if (data->IsObjectTemplateInfo()) { - return InstantiateObject(isolate, Handle::cast(data)); - } else { - // TODO(dcarney): CHECK data is JSObject or Primitive. - return data; - } -} - - class AccessCheckDisableScope { public: AccessCheckDisableScope(Isolate* isolate, Handle obj) @@ -157,23 +162,18 @@ MaybeHandle ConfigureInstance(Isolate* isolate, Handle obj, auto name = handle(Name::cast(properties.get(i + 1)), isolate); auto prop_data = handle(properties.get(i + 2), isolate); auto attributes = Smi::cast(properties.get(i + 3)); - Handle value; - ASSIGN_RETURN_ON_EXCEPTION( - isolate, value, Instantiate(isolate, prop_data, name), JSObject); - RETURN_ON_EXCEPTION(isolate, AddPropertyForTemplate(isolate, obj, name, - value, attributes), + RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name, + prop_data, attributes), JSObject); } else { - DCHECK(length == 4 || length == 5); - // TODO(verwaest): The 5th value used to be access_control. Remove once - // the bindings are updated. + DCHECK(length == 4); auto name = handle(Name::cast(properties.get(i + 1)), isolate); auto getter = handle(properties.get(i + 2), isolate); auto setter = handle(properties.get(i + 3), isolate); auto attributes = Smi::cast(properties.get(i + 4)); RETURN_ON_EXCEPTION(isolate, - DefineApiAccessorProperty(isolate, obj, name, getter, - setter, attributes), + DefineAccessorProperty(isolate, obj, name, getter, + setter, attributes), JSObject); } i += length + 1; @@ -313,6 +313,25 @@ class InvokeScope { SaveContext save_context_; }; + +void AddPropertyToPropertyList(Isolate* isolate, Handle templ, + int length, Handle* data) { + auto list = handle(templ->property_list(), isolate); + if (list->IsUndefined()) { + list = NeanderArray(isolate).value(); + templ->set_property_list(*list); + } + NeanderArray array(list); + array.add(isolate, isolate->factory()->NewNumberFromInt(length)); + for (int i = 0; i < length; i++) { + Handle value = + data[i].is_null() + ? Handle::cast(isolate->factory()->undefined_value()) + : data[i]; + array.add(isolate, value); + } +} + } // namespace @@ -348,6 +367,46 @@ MaybeHandle ApiNatives::ConfigureInstance( } +void ApiNatives::AddDataProperty(Isolate* isolate, Handle info, + Handle name, Handle value, + PropertyAttributes attributes) { + const int kSize = 3; + DCHECK(Smi::IsValid(static_cast(attributes))); + auto attribute_handle = + handle(Smi::FromInt(static_cast(attributes)), isolate); + Handle data[kSize] = {name, value, attribute_handle}; + AddPropertyToPropertyList(isolate, info, kSize, data); +} + + +void ApiNatives::AddAccessorProperty(Isolate* isolate, + Handle info, + Handle name, + Handle getter, + Handle setter, + PropertyAttributes attributes) { + const int kSize = 4; + DCHECK(Smi::IsValid(static_cast(attributes))); + auto attribute_handle = + handle(Smi::FromInt(static_cast(attributes)), isolate); + Handle data[kSize] = {name, getter, setter, attribute_handle}; + AddPropertyToPropertyList(isolate, info, kSize, data); +} + + +void ApiNatives::AddNativeDataProperty(Isolate* isolate, + Handle info, + Handle property) { + auto list = handle(info->property_accessors(), isolate); + if (list->IsUndefined()) { + list = NeanderArray(isolate).value(); + info->set_property_accessors(*list); + } + NeanderArray array(list); + array.add(isolate, property); +} + + Handle ApiNatives::CreateApiFunction( Isolate* isolate, Handle obj, Handle prototype, ApiInstanceType instance_type) { diff --git a/src/api-natives.h b/src/api-natives.h index 8f93d64..9f97b5d 100644 --- a/src/api-natives.h +++ b/src/api-natives.h @@ -14,8 +14,10 @@ class ApiNatives { public: MUST_USE_RESULT static MaybeHandle InstantiateFunction( Handle data); + MUST_USE_RESULT static MaybeHandle InstantiateObject( Handle data); + MUST_USE_RESULT static MaybeHandle ConfigureInstance( Isolate* isolate, Handle instance, Handle data); @@ -30,6 +32,19 @@ class ApiNatives { Handle obj, Handle prototype, ApiInstanceType instance_type); + + static void AddDataProperty(Isolate* isolate, Handle info, + Handle name, Handle value, + PropertyAttributes attributes); + + static void AddAccessorProperty(Isolate* isolate, Handle info, + Handle name, + Handle getter, + Handle setter, + PropertyAttributes attributes); + + static void AddNativeDataProperty(Isolate* isolate, Handle info, + Handle property); }; } // namespace internal diff --git a/src/api.cc b/src/api.cc index 0393693..28f8747 100644 --- a/src/api.cc +++ b/src/api.cc @@ -766,39 +766,17 @@ static void InitializeTemplate(i::Handle that, int type) { } -static void TemplateSet(i::Isolate* isolate, - v8::Template* templ, - int length, - v8::Handle* data) { - i::Handle list(Utils::OpenHandle(templ)->property_list(), isolate); - if (list->IsUndefined()) { - list = NeanderArray(isolate).value(); - Utils::OpenHandle(templ)->set_property_list(*list); - } - NeanderArray array(list); - array.add(isolate, isolate->factory()->NewNumberFromInt(length)); - for (int i = 0; i < length; i++) { - i::Handle value = data[i].IsEmpty() ? - i::Handle(isolate->factory()->undefined_value()) : - Utils::OpenHandle(*data[i]); - array.add(isolate, value); - } -} - - void Template::Set(v8::Handle name, v8::Handle value, v8::PropertyAttribute attribute) { - i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); + auto templ = Utils::OpenHandle(this); + i::Isolate* isolate = templ->GetIsolate(); ENTER_V8(isolate); i::HandleScope scope(isolate); - const int kSize = 3; - v8::Isolate* v8_isolate = reinterpret_cast(isolate); - v8::Handle data[kSize] = { - name, - value, - v8::Integer::New(v8_isolate, attribute)}; - TemplateSet(isolate, this, kSize, data); + // TODO(dcarney): split api to allow values of v8::Value or v8::TemplateInfo. + i::ApiNatives::AddDataProperty(isolate, templ, Utils::OpenHandle(*name), + Utils::OpenHandle(*value), + static_cast(attribute)); } @@ -810,19 +788,16 @@ void Template::SetAccessorProperty( v8::AccessControl access_control) { // TODO(verwaest): Remove |access_control|. DCHECK_EQ(v8::DEFAULT, access_control); - i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); + auto templ = Utils::OpenHandle(this); + auto isolate = templ->GetIsolate(); ENTER_V8(isolate); DCHECK(!name.IsEmpty()); DCHECK(!getter.IsEmpty() || !setter.IsEmpty()); i::HandleScope scope(isolate); - const int kSize = 5; - v8::Isolate* v8_isolate = reinterpret_cast(isolate); - v8::Handle data[kSize] = { - name, - getter, - setter, - v8::Integer::New(v8_isolate, attribute)}; - TemplateSet(isolate, this, kSize, data); + i::ApiNatives::AddAccessorProperty( + isolate, templ, Utils::OpenHandle(*name), + Utils::OpenHandle(*getter, true), Utils::OpenHandle(*setter, true), + static_cast(attribute)); } @@ -1144,20 +1119,6 @@ static i::Handle EnsureConstructor( } -static inline void AddPropertyToTemplate( - i::Handle info, - i::Handle obj) { - i::Isolate* isolate = info->GetIsolate(); - i::Handle list(info->property_accessors(), isolate); - if (list->IsUndefined()) { - list = NeanderArray(isolate).value(); - info->set_property_accessors(*list); - } - NeanderArray array(list); - array.add(isolate, obj); -} - - static inline i::Handle GetTemplateInfo( i::Isolate* isolate, Template* template_obj) { @@ -1184,14 +1145,14 @@ static bool TemplateSetAccessor( AccessControl settings, PropertyAttribute attribute, v8::Local signature) { - i::Isolate* isolate = Utils::OpenHandle(template_obj)->GetIsolate(); + auto isolate = Utils::OpenHandle(template_obj)->GetIsolate(); ENTER_V8(isolate); i::HandleScope scope(isolate); - i::Handle obj = MakeAccessorInfo( - name, getter, setter, data, settings, attribute, signature); + auto obj = MakeAccessorInfo(name, getter, setter, data, settings, attribute, + signature); if (obj.is_null()) return false; - i::Handle info = GetTemplateInfo(isolate, template_obj); - AddPropertyToTemplate(info, obj); + auto info = GetTemplateInfo(isolate, template_obj); + i::ApiNatives::AddNativeDataProperty(isolate, info, obj); return true; } -- 2.7.4