Commit e64810b5 authored by dcarney's avatar dcarney Committed by Commit bot

cleanup api-natives a bit

R=verwaest@chromium.org

BUG=

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

Cr-Commit-Position: refs/heads/master@{#26481}
parent 6efd03c4
...@@ -10,43 +10,71 @@ namespace internal { ...@@ -10,43 +10,71 @@ namespace internal {
namespace { namespace {
// Transform getter or setter into something DefineAccessor can handle. MaybeHandle<JSObject> InstantiateObject(Isolate* isolate,
Handle<Object> InstantiateAccessorComponent(Isolate* isolate, Handle<ObjectTemplateInfo> data);
Handle<Object> component) {
if (component->IsUndefined()) return isolate->factory()->undefined_value();
Handle<FunctionTemplateInfo> info = MaybeHandle<JSFunction> InstantiateFunction(Isolate* isolate,
Handle<FunctionTemplateInfo>::cast(component); Handle<FunctionTemplateInfo> data,
// TODO(dcarney): instantiate directly. Handle<Name> name = Handle<Name>());
return Utils::OpenHandle(*Utils::ToLocal(info)->GetFunction());
MaybeHandle<Object> Instantiate(Isolate* isolate, Handle<Object> data,
Handle<Name> name = Handle<Name>()) {
if (data->IsFunctionTemplateInfo()) {
return InstantiateFunction(isolate,
Handle<FunctionTemplateInfo>::cast(data), name);
} else if (data->IsObjectTemplateInfo()) {
return InstantiateObject(isolate, Handle<ObjectTemplateInfo>::cast(data));
} else {
return data;
}
} }
MaybeHandle<Object> DefineApiAccessorProperty( MaybeHandle<Object> DefineAccessorProperty(
Isolate* isolate, Handle<JSObject> object, Handle<Name> name, Isolate* isolate, Handle<JSObject> object, Handle<Name> name,
Handle<Object> getter, Handle<Object> setter, Smi* attribute) { Handle<Object> getter, Handle<Object> setter, Smi* attributes) {
DCHECK(PropertyDetails::AttributesField::is_valid( DCHECK(PropertyDetails::AttributesField::is_valid(
static_cast<PropertyAttributes>(attribute->value()))); static_cast<PropertyAttributes>(attributes->value())));
RETURN_ON_EXCEPTION( if (!getter->IsUndefined()) {
isolate, JSObject::DefineAccessor( ASSIGN_RETURN_ON_EXCEPTION(
object, name, InstantiateAccessorComponent(isolate, getter), isolate, getter,
InstantiateAccessorComponent(isolate, setter), InstantiateFunction(isolate,
static_cast<PropertyAttributes>(attribute->value())), Handle<FunctionTemplateInfo>::cast(getter)),
Object); Object);
}
if (!setter->IsUndefined()) {
ASSIGN_RETURN_ON_EXCEPTION(
isolate, setter,
InstantiateFunction(isolate,
Handle<FunctionTemplateInfo>::cast(setter)),
Object);
}
RETURN_ON_EXCEPTION(isolate,
JSObject::DefineAccessor(
object, name, getter, setter,
static_cast<PropertyAttributes>(attributes->value())),
Object);
return object; return object;
} }
MaybeHandle<Object> AddPropertyForTemplate(Isolate* isolate, MaybeHandle<Object> DefineDataProperty(Isolate* isolate,
Handle<JSObject> object, Handle<JSObject> object,
Handle<Object> key, Handle<Name> key,
Handle<Object> value, Handle<Object> prop_data,
Smi* unchecked_attributes) { Smi* unchecked_attributes) {
DCHECK((unchecked_attributes->value() & DCHECK((unchecked_attributes->value() &
~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0);
// Compute attributes. // Compute attributes.
PropertyAttributes attributes = PropertyAttributes attributes =
static_cast<PropertyAttributes>(unchecked_attributes->value()); static_cast<PropertyAttributes>(unchecked_attributes->value());
Handle<Object> value;
ASSIGN_RETURN_ON_EXCEPTION(isolate, value,
Instantiate(isolate, prop_data, key), Object);
#ifdef DEBUG #ifdef DEBUG
bool duplicate; bool duplicate;
if (key->IsName()) { if (key->IsName()) {
...@@ -95,29 +123,6 @@ void EnableAccessChecks(Isolate* isolate, Handle<JSObject> object) { ...@@ -95,29 +123,6 @@ void EnableAccessChecks(Isolate* isolate, Handle<JSObject> object) {
} }
MaybeHandle<JSObject> InstantiateObject(Isolate* isolate,
Handle<ObjectTemplateInfo> data);
MaybeHandle<JSFunction> InstantiateFunction(Isolate* isolate,
Handle<FunctionTemplateInfo> data,
Handle<Name> name = Handle<Name>());
MaybeHandle<Object> Instantiate(Isolate* isolate, Handle<Object> data,
Handle<Name> name = Handle<Name>()) {
if (data->IsFunctionTemplateInfo()) {
return InstantiateFunction(isolate,
Handle<FunctionTemplateInfo>::cast(data), name);
} else if (data->IsObjectTemplateInfo()) {
return InstantiateObject(isolate, Handle<ObjectTemplateInfo>::cast(data));
} else {
// TODO(dcarney): CHECK data is JSObject or Primitive.
return data;
}
}
class AccessCheckDisableScope { class AccessCheckDisableScope {
public: public:
AccessCheckDisableScope(Isolate* isolate, Handle<JSObject> obj) AccessCheckDisableScope(Isolate* isolate, Handle<JSObject> obj)
...@@ -157,23 +162,18 @@ MaybeHandle<JSObject> ConfigureInstance(Isolate* isolate, Handle<JSObject> obj, ...@@ -157,23 +162,18 @@ MaybeHandle<JSObject> ConfigureInstance(Isolate* isolate, Handle<JSObject> obj,
auto name = handle(Name::cast(properties.get(i + 1)), isolate); auto name = handle(Name::cast(properties.get(i + 1)), isolate);
auto prop_data = handle(properties.get(i + 2), isolate); auto prop_data = handle(properties.get(i + 2), isolate);
auto attributes = Smi::cast(properties.get(i + 3)); auto attributes = Smi::cast(properties.get(i + 3));
Handle<Object> value; RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name,
ASSIGN_RETURN_ON_EXCEPTION( prop_data, attributes),
isolate, value, Instantiate(isolate, prop_data, name), JSObject);
RETURN_ON_EXCEPTION(isolate, AddPropertyForTemplate(isolate, obj, name,
value, attributes),
JSObject); JSObject);
} else { } else {
DCHECK(length == 4 || length == 5); DCHECK(length == 4);
// TODO(verwaest): The 5th value used to be access_control. Remove once
// the bindings are updated.
auto name = handle(Name::cast(properties.get(i + 1)), isolate); auto name = handle(Name::cast(properties.get(i + 1)), isolate);
auto getter = handle(properties.get(i + 2), isolate); auto getter = handle(properties.get(i + 2), isolate);
auto setter = handle(properties.get(i + 3), isolate); auto setter = handle(properties.get(i + 3), isolate);
auto attributes = Smi::cast(properties.get(i + 4)); auto attributes = Smi::cast(properties.get(i + 4));
RETURN_ON_EXCEPTION(isolate, RETURN_ON_EXCEPTION(isolate,
DefineApiAccessorProperty(isolate, obj, name, getter, DefineAccessorProperty(isolate, obj, name, getter,
setter, attributes), setter, attributes),
JSObject); JSObject);
} }
i += length + 1; i += length + 1;
...@@ -313,6 +313,25 @@ class InvokeScope { ...@@ -313,6 +313,25 @@ class InvokeScope {
SaveContext save_context_; SaveContext save_context_;
}; };
void AddPropertyToPropertyList(Isolate* isolate, Handle<TemplateInfo> templ,
int length, Handle<Object>* 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<Object> value =
data[i].is_null()
? Handle<Object>::cast(isolate->factory()->undefined_value())
: data[i];
array.add(isolate, value);
}
}
} // namespace } // namespace
...@@ -348,6 +367,46 @@ MaybeHandle<FunctionTemplateInfo> ApiNatives::ConfigureInstance( ...@@ -348,6 +367,46 @@ MaybeHandle<FunctionTemplateInfo> ApiNatives::ConfigureInstance(
} }
void ApiNatives::AddDataProperty(Isolate* isolate, Handle<TemplateInfo> info,
Handle<Name> name, Handle<Object> value,
PropertyAttributes attributes) {
const int kSize = 3;
DCHECK(Smi::IsValid(static_cast<int>(attributes)));
auto attribute_handle =
handle(Smi::FromInt(static_cast<int>(attributes)), isolate);
Handle<Object> data[kSize] = {name, value, attribute_handle};
AddPropertyToPropertyList(isolate, info, kSize, data);
}
void ApiNatives::AddAccessorProperty(Isolate* isolate,
Handle<TemplateInfo> info,
Handle<Name> name,
Handle<FunctionTemplateInfo> getter,
Handle<FunctionTemplateInfo> setter,
PropertyAttributes attributes) {
const int kSize = 4;
DCHECK(Smi::IsValid(static_cast<int>(attributes)));
auto attribute_handle =
handle(Smi::FromInt(static_cast<int>(attributes)), isolate);
Handle<Object> data[kSize] = {name, getter, setter, attribute_handle};
AddPropertyToPropertyList(isolate, info, kSize, data);
}
void ApiNatives::AddNativeDataProperty(Isolate* isolate,
Handle<TemplateInfo> info,
Handle<AccessorInfo> 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<JSFunction> ApiNatives::CreateApiFunction( Handle<JSFunction> ApiNatives::CreateApiFunction(
Isolate* isolate, Handle<FunctionTemplateInfo> obj, Isolate* isolate, Handle<FunctionTemplateInfo> obj,
Handle<Object> prototype, ApiInstanceType instance_type) { Handle<Object> prototype, ApiInstanceType instance_type) {
......
...@@ -14,8 +14,10 @@ class ApiNatives { ...@@ -14,8 +14,10 @@ class ApiNatives {
public: public:
MUST_USE_RESULT static MaybeHandle<JSFunction> InstantiateFunction( MUST_USE_RESULT static MaybeHandle<JSFunction> InstantiateFunction(
Handle<FunctionTemplateInfo> data); Handle<FunctionTemplateInfo> data);
MUST_USE_RESULT static MaybeHandle<JSObject> InstantiateObject( MUST_USE_RESULT static MaybeHandle<JSObject> InstantiateObject(
Handle<ObjectTemplateInfo> data); Handle<ObjectTemplateInfo> data);
MUST_USE_RESULT static MaybeHandle<FunctionTemplateInfo> ConfigureInstance( MUST_USE_RESULT static MaybeHandle<FunctionTemplateInfo> ConfigureInstance(
Isolate* isolate, Handle<FunctionTemplateInfo> instance, Isolate* isolate, Handle<FunctionTemplateInfo> instance,
Handle<JSObject> data); Handle<JSObject> data);
...@@ -30,6 +32,19 @@ class ApiNatives { ...@@ -30,6 +32,19 @@ class ApiNatives {
Handle<FunctionTemplateInfo> obj, Handle<FunctionTemplateInfo> obj,
Handle<Object> prototype, Handle<Object> prototype,
ApiInstanceType instance_type); ApiInstanceType instance_type);
static void AddDataProperty(Isolate* isolate, Handle<TemplateInfo> info,
Handle<Name> name, Handle<Object> value,
PropertyAttributes attributes);
static void AddAccessorProperty(Isolate* isolate, Handle<TemplateInfo> info,
Handle<Name> name,
Handle<FunctionTemplateInfo> getter,
Handle<FunctionTemplateInfo> setter,
PropertyAttributes attributes);
static void AddNativeDataProperty(Isolate* isolate, Handle<TemplateInfo> info,
Handle<AccessorInfo> property);
}; };
} // namespace internal } // namespace internal
......
...@@ -766,39 +766,17 @@ static void InitializeTemplate(i::Handle<i::TemplateInfo> that, int type) { ...@@ -766,39 +766,17 @@ static void InitializeTemplate(i::Handle<i::TemplateInfo> that, int type) {
} }
static void TemplateSet(i::Isolate* isolate,
v8::Template* templ,
int length,
v8::Handle<v8::Data>* data) {
i::Handle<i::Object> 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<i::Object> value = data[i].IsEmpty() ?
i::Handle<i::Object>(isolate->factory()->undefined_value()) :
Utils::OpenHandle(*data[i]);
array.add(isolate, value);
}
}
void Template::Set(v8::Handle<Name> name, void Template::Set(v8::Handle<Name> name,
v8::Handle<Data> value, v8::Handle<Data> value,
v8::PropertyAttribute attribute) { v8::PropertyAttribute attribute) {
i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); auto templ = Utils::OpenHandle(this);
i::Isolate* isolate = templ->GetIsolate();
ENTER_V8(isolate); ENTER_V8(isolate);
i::HandleScope scope(isolate); i::HandleScope scope(isolate);
const int kSize = 3; // TODO(dcarney): split api to allow values of v8::Value or v8::TemplateInfo.
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate); i::ApiNatives::AddDataProperty(isolate, templ, Utils::OpenHandle(*name),
v8::Handle<v8::Data> data[kSize] = { Utils::OpenHandle(*value),
name, static_cast<PropertyAttributes>(attribute));
value,
v8::Integer::New(v8_isolate, attribute)};
TemplateSet(isolate, this, kSize, data);
} }
...@@ -810,19 +788,16 @@ void Template::SetAccessorProperty( ...@@ -810,19 +788,16 @@ void Template::SetAccessorProperty(
v8::AccessControl access_control) { v8::AccessControl access_control) {
// TODO(verwaest): Remove |access_control|. // TODO(verwaest): Remove |access_control|.
DCHECK_EQ(v8::DEFAULT, 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); ENTER_V8(isolate);
DCHECK(!name.IsEmpty()); DCHECK(!name.IsEmpty());
DCHECK(!getter.IsEmpty() || !setter.IsEmpty()); DCHECK(!getter.IsEmpty() || !setter.IsEmpty());
i::HandleScope scope(isolate); i::HandleScope scope(isolate);
const int kSize = 5; i::ApiNatives::AddAccessorProperty(
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate); isolate, templ, Utils::OpenHandle(*name),
v8::Handle<v8::Data> data[kSize] = { Utils::OpenHandle(*getter, true), Utils::OpenHandle(*setter, true),
name, static_cast<PropertyAttributes>(attribute));
getter,
setter,
v8::Integer::New(v8_isolate, attribute)};
TemplateSet(isolate, this, kSize, data);
} }
...@@ -1144,20 +1119,6 @@ static i::Handle<i::FunctionTemplateInfo> EnsureConstructor( ...@@ -1144,20 +1119,6 @@ static i::Handle<i::FunctionTemplateInfo> EnsureConstructor(
} }
static inline void AddPropertyToTemplate(
i::Handle<i::TemplateInfo> info,
i::Handle<i::AccessorInfo> obj) {
i::Isolate* isolate = info->GetIsolate();
i::Handle<i::Object> 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<i::TemplateInfo> GetTemplateInfo( static inline i::Handle<i::TemplateInfo> GetTemplateInfo(
i::Isolate* isolate, i::Isolate* isolate,
Template* template_obj) { Template* template_obj) {
...@@ -1184,14 +1145,14 @@ static bool TemplateSetAccessor( ...@@ -1184,14 +1145,14 @@ static bool TemplateSetAccessor(
AccessControl settings, AccessControl settings,
PropertyAttribute attribute, PropertyAttribute attribute,
v8::Local<AccessorSignature> signature) { v8::Local<AccessorSignature> signature) {
i::Isolate* isolate = Utils::OpenHandle(template_obj)->GetIsolate(); auto isolate = Utils::OpenHandle(template_obj)->GetIsolate();
ENTER_V8(isolate); ENTER_V8(isolate);
i::HandleScope scope(isolate); i::HandleScope scope(isolate);
i::Handle<i::AccessorInfo> obj = MakeAccessorInfo( auto obj = MakeAccessorInfo(name, getter, setter, data, settings, attribute,
name, getter, setter, data, settings, attribute, signature); signature);
if (obj.is_null()) return false; if (obj.is_null()) return false;
i::Handle<i::TemplateInfo> info = GetTemplateInfo(isolate, template_obj); auto info = GetTemplateInfo(isolate, template_obj);
AddPropertyToTemplate(info, obj); i::ApiNatives::AddNativeDataProperty(isolate, info, obj);
return true; return true;
} }
......
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