-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
n-api: Update property attrs enum to match JS spec #12240
Closed
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/****************************************************************************** | ||
/****************************************************************************** | ||
* Experimental prototype for demonstrating VM agnostic and ABI stable API | ||
* for native modules to use instead of using Nan and V8 APIs directly. | ||
* | ||
|
@@ -35,18 +35,27 @@ class napi_env__ { | |
namespace v8impl { | ||
|
||
// convert from n-api property attributes to v8::PropertyAttribute | ||
static inline v8::PropertyAttribute V8PropertyAttributesFromAttributes( | ||
napi_property_attributes attributes) { | ||
unsigned int attribute_flags = v8::None; | ||
if (attributes & napi_read_only) { | ||
attribute_flags |= v8::ReadOnly; | ||
static inline v8::PropertyAttribute V8PropertyAttributesFromDescriptor( | ||
const napi_property_descriptor* descriptor) { | ||
unsigned int attribute_flags = v8::PropertyAttribute::None; | ||
|
||
if (descriptor->getter != nullptr || descriptor->setter != nullptr) { | ||
// The napi_writable attribute is ignored for accessor descriptors, but | ||
// V8 requires the ReadOnly attribute to match nonexistence of a setter. | ||
attribute_flags |= (descriptor->setter == nullptr ? | ||
v8::PropertyAttribute::ReadOnly : v8::PropertyAttribute::None); | ||
} | ||
else if ((descriptor->attributes & napi_writable) == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: coalesce the right brace with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
attribute_flags |= v8::PropertyAttribute::ReadOnly; | ||
} | ||
if (attributes & napi_dont_enum) { | ||
attribute_flags |= v8::DontEnum; | ||
|
||
if ((descriptor->attributes & napi_enumerable) == 0) { | ||
attribute_flags |= v8::PropertyAttribute::DontEnum; | ||
} | ||
if (attributes & napi_dont_delete) { | ||
attribute_flags |= v8::DontDelete; | ||
if ((descriptor->attributes & napi_configurable) == 0) { | ||
attribute_flags |= v8::PropertyAttribute::DontDelete; | ||
} | ||
|
||
return static_cast<v8::PropertyAttribute>(attribute_flags); | ||
} | ||
|
||
|
@@ -775,7 +784,7 @@ napi_status napi_define_class(napi_env env, | |
for (size_t i = 0; i < property_count; i++) { | ||
const napi_property_descriptor* p = properties + i; | ||
|
||
if ((p->attributes & napi_static_property) != 0) { | ||
if ((p->attributes & napi_static) != 0) { | ||
// Static properties are handled separately below. | ||
static_property_count++; | ||
continue; | ||
|
@@ -784,25 +793,11 @@ napi_status napi_define_class(napi_env env, | |
v8::Local<v8::String> property_name; | ||
CHECK_NEW_FROM_UTF8(env, property_name, p->utf8name); | ||
v8::PropertyAttribute attributes = | ||
v8impl::V8PropertyAttributesFromAttributes(p->attributes); | ||
v8impl::V8PropertyAttributesFromDescriptor(p); | ||
|
||
// This code is similar to that in napi_define_property(); the | ||
// This code is similar to that in napi_define_properties(); the | ||
// difference is it applies to a template instead of an object. | ||
if (p->method) { | ||
v8::Local<v8::Object> cbdata = | ||
v8impl::CreateFunctionCallbackData(env, p->method, p->data); | ||
|
||
RETURN_STATUS_IF_FALSE(env, !cbdata.IsEmpty(), napi_generic_failure); | ||
|
||
v8::Local<v8::FunctionTemplate> t = | ||
v8::FunctionTemplate::New(isolate, | ||
v8impl::FunctionCallbackWrapper::Invoke, | ||
cbdata, | ||
v8::Signature::New(isolate, tpl)); | ||
t->SetClassName(property_name); | ||
|
||
tpl->PrototypeTemplate()->Set(property_name, t, attributes); | ||
} else if (p->getter || p->setter) { | ||
if (p->getter != nullptr || p->setter != nullptr) { | ||
v8::Local<v8::Object> cbdata = v8impl::CreateAccessorCallbackData( | ||
env, p->getter, p->setter, p->data); | ||
|
||
|
@@ -813,6 +808,20 @@ napi_status napi_define_class(napi_env env, | |
cbdata, | ||
v8::AccessControl::DEFAULT, | ||
attributes); | ||
} else if (p->method != nullptr) { | ||
v8::Local<v8::Object> cbdata = | ||
v8impl::CreateFunctionCallbackData(env, p->method, p->data); | ||
|
||
RETURN_STATUS_IF_FALSE(env, !cbdata.IsEmpty(), napi_generic_failure); | ||
|
||
v8::Local<v8::FunctionTemplate> t = | ||
v8::FunctionTemplate::New(isolate, | ||
v8impl::FunctionCallbackWrapper::Invoke, | ||
cbdata, | ||
v8::Signature::New(isolate, tpl)); | ||
t->SetClassName(property_name); | ||
|
||
tpl->PrototypeTemplate()->Set(property_name, t, attributes); | ||
} else { | ||
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(p->value); | ||
tpl->PrototypeTemplate()->Set(property_name, value, attributes); | ||
|
@@ -827,7 +836,7 @@ napi_status napi_define_class(napi_env env, | |
|
||
for (size_t i = 0; i < property_count; i++) { | ||
const napi_property_descriptor* p = properties + i; | ||
if ((p->attributes & napi_static_property) != 0) { | ||
if ((p->attributes & napi_static) != 0) { | ||
static_descriptors.push_back(*p); | ||
} | ||
} | ||
|
@@ -1094,10 +1103,28 @@ napi_status napi_define_properties(napi_env env, | |
CHECK_NEW_FROM_UTF8(env, name, p->utf8name); | ||
|
||
v8::PropertyAttribute attributes = | ||
v8impl::V8PropertyAttributesFromAttributes( | ||
(napi_property_attributes)(p->attributes & ~napi_static_property)); | ||
v8impl::V8PropertyAttributesFromDescriptor(p); | ||
|
||
if (p->method) { | ||
if (p->getter != nullptr || p->setter != nullptr) { | ||
v8::Local<v8::Object> cbdata = v8impl::CreateAccessorCallbackData( | ||
env, | ||
p->getter, | ||
p->setter, | ||
p->data); | ||
|
||
auto set_maybe = obj->SetAccessor( | ||
context, | ||
name, | ||
p->getter ? v8impl::GetterCallbackWrapper::Invoke : nullptr, | ||
p->setter ? v8impl::SetterCallbackWrapper::Invoke : nullptr, | ||
cbdata, | ||
v8::AccessControl::DEFAULT, | ||
attributes); | ||
|
||
if (!set_maybe.FromMaybe(false)) { | ||
return napi_set_last_error(env, napi_invalid_arg); | ||
} | ||
} else if (p->method != nullptr) { | ||
v8::Local<v8::Object> cbdata = | ||
v8impl::CreateFunctionCallbackData(env, p->method, p->data); | ||
|
||
|
@@ -1112,25 +1139,6 @@ napi_status napi_define_properties(napi_env env, | |
if (!define_maybe.FromMaybe(false)) { | ||
return napi_set_last_error(env, napi_generic_failure); | ||
} | ||
} else if (p->getter || p->setter) { | ||
v8::Local<v8::Object> cbdata = v8impl::CreateAccessorCallbackData( | ||
env, | ||
p->getter, | ||
p->setter, | ||
p->data); | ||
|
||
auto set_maybe = obj->SetAccessor( | ||
context, | ||
name, | ||
p->getter ? v8impl::GetterCallbackWrapper::Invoke : nullptr, | ||
p->setter ? v8impl::SetterCallbackWrapper::Invoke : nullptr, | ||
cbdata, | ||
v8::AccessControl::DEFAULT, | ||
attributes); | ||
|
||
if (!set_maybe.FromMaybe(false)) { | ||
return napi_set_last_error(env, napi_invalid_arg); | ||
} | ||
} else { | ||
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(p->value); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have an idea what this is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a UTF-8 BOM. VS 2017 likes to insert those for some reason. Fixed.