1
- /* *****************************************************************************
1
+ /* *****************************************************************************
2
2
* Experimental prototype for demonstrating VM agnostic and ABI stable API
3
3
* for native modules to use instead of using Nan and V8 APIs directly.
4
4
*
@@ -35,18 +35,27 @@ class napi_env__ {
35
35
namespace v8impl {
36
36
37
37
// convert from n-api property attributes to v8::PropertyAttribute
38
- static inline v8::PropertyAttribute V8PropertyAttributesFromAttributes (
39
- napi_property_attributes attributes) {
40
- unsigned int attribute_flags = v8::None;
41
- if ((attributes & napi_writable) == 0 ) {
42
- attribute_flags |= v8::ReadOnly;
38
+ static inline v8::PropertyAttribute V8PropertyAttributesFromDescriptor (
39
+ const napi_property_descriptor* descriptor) {
40
+ unsigned int attribute_flags = v8::PropertyAttribute::None;
41
+
42
+ if (descriptor->getter != nullptr || descriptor->setter != nullptr ) {
43
+ // The napi_writable attribute is ignored for accessor descriptors, but
44
+ // V8 requires the ReadOnly attribute to match nonexistence of a setter.
45
+ attribute_flags |= (descriptor->setter == nullptr ?
46
+ v8::PropertyAttribute::ReadOnly : v8::PropertyAttribute::None);
47
+ }
48
+ else if ((descriptor->attributes & napi_writable) == 0 ) {
49
+ attribute_flags |= v8::PropertyAttribute::ReadOnly;
43
50
}
44
- if ((attributes & napi_enumerable) == 0 ) {
45
- attribute_flags |= v8::DontEnum;
51
+
52
+ if ((descriptor->attributes & napi_enumerable) == 0 ) {
53
+ attribute_flags |= v8::PropertyAttribute::DontEnum;
46
54
}
47
- if ((attributes & napi_configurable) == 0 ) {
48
- attribute_flags |= v8::DontDelete;
55
+ if ((descriptor-> attributes & napi_configurable) == 0 ) {
56
+ attribute_flags |= v8::PropertyAttribute:: DontDelete;
49
57
}
58
+
50
59
return static_cast <v8::PropertyAttribute>(attribute_flags);
51
60
}
52
61
@@ -784,41 +793,35 @@ napi_status napi_define_class(napi_env env,
784
793
v8::Local<v8::String> property_name;
785
794
CHECK_NEW_FROM_UTF8 (env, property_name, p->utf8name );
786
795
v8::PropertyAttribute attributes =
787
- v8impl::V8PropertyAttributesFromAttributes (p-> attributes );
796
+ v8impl::V8PropertyAttributesFromDescriptor (p );
788
797
789
- // This code is similar to that in napi_define_property (); the
798
+ // This code is similar to that in napi_define_properties (); the
790
799
// difference is it applies to a template instead of an object.
791
- if (p->method != nullptr ) {
792
- v8::Local<v8::Object> cbdata =
793
- v8impl::CreateFunctionCallbackData (env, p->method , p->data );
794
-
795
- RETURN_STATUS_IF_FALSE (env, !cbdata.IsEmpty (), napi_generic_failure);
796
-
797
- v8::Local<v8::FunctionTemplate> t =
798
- v8::FunctionTemplate::New (isolate,
799
- v8impl::FunctionCallbackWrapper::Invoke,
800
- cbdata,
801
- v8::Signature::New (isolate, tpl));
802
- t->SetClassName (property_name);
803
-
804
- tpl->PrototypeTemplate ()->Set (property_name, t, attributes);
805
- } else if (p->getter != nullptr || p->setter != nullptr ) {
800
+ if (p->getter != nullptr || p->setter != nullptr ) {
806
801
v8::Local<v8::Object> cbdata = v8impl::CreateAccessorCallbackData (
807
802
env, p->getter , p->setter , p->data );
808
803
809
- // The napi_writable attribute is ignored for accessor descriptors,
810
- // but V8 requires the ReadOnly attribute to match existence of a setter.
811
- attributes = static_cast <v8::PropertyAttribute>(p->setter != nullptr
812
- ? attributes & ~v8::PropertyAttribute::ReadOnly
813
- : attributes | v8::PropertyAttribute::ReadOnly);
814
-
815
804
tpl->PrototypeTemplate ()->SetAccessor (
816
805
property_name,
817
806
p->getter ? v8impl::GetterCallbackWrapper::Invoke : nullptr ,
818
807
p->setter ? v8impl::SetterCallbackWrapper::Invoke : nullptr ,
819
808
cbdata,
820
809
v8::AccessControl::DEFAULT,
821
810
attributes);
811
+ } else if (p->method != nullptr ) {
812
+ v8::Local<v8::Object> cbdata =
813
+ v8impl::CreateFunctionCallbackData (env, p->method , p->data );
814
+
815
+ RETURN_STATUS_IF_FALSE (env, !cbdata.IsEmpty (), napi_generic_failure);
816
+
817
+ v8::Local<v8::FunctionTemplate> t =
818
+ v8::FunctionTemplate::New (isolate,
819
+ v8impl::FunctionCallbackWrapper::Invoke,
820
+ cbdata,
821
+ v8::Signature::New (isolate, tpl));
822
+ t->SetClassName (property_name);
823
+
824
+ tpl->PrototypeTemplate ()->Set (property_name, t, attributes);
822
825
} else {
823
826
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue (p->value );
824
827
tpl->PrototypeTemplate ()->Set (property_name, value, attributes);
@@ -1100,9 +1103,28 @@ napi_status napi_define_properties(napi_env env,
1100
1103
CHECK_NEW_FROM_UTF8 (env, name, p->utf8name );
1101
1104
1102
1105
v8::PropertyAttribute attributes =
1103
- v8impl::V8PropertyAttributesFromAttributes (p-> attributes );
1106
+ v8impl::V8PropertyAttributesFromDescriptor (p );
1104
1107
1105
- if (p->method != nullptr ) {
1108
+ if (p->getter != nullptr || p->setter != nullptr ) {
1109
+ v8::Local<v8::Object> cbdata = v8impl::CreateAccessorCallbackData (
1110
+ env,
1111
+ p->getter ,
1112
+ p->setter ,
1113
+ p->data );
1114
+
1115
+ auto set_maybe = obj->SetAccessor (
1116
+ context,
1117
+ name,
1118
+ p->getter ? v8impl::GetterCallbackWrapper::Invoke : nullptr ,
1119
+ p->setter ? v8impl::SetterCallbackWrapper::Invoke : nullptr ,
1120
+ cbdata,
1121
+ v8::AccessControl::DEFAULT,
1122
+ attributes);
1123
+
1124
+ if (!set_maybe.FromMaybe (false )) {
1125
+ return napi_set_last_error (env, napi_invalid_arg);
1126
+ }
1127
+ } else if (p->method != nullptr ) {
1106
1128
v8::Local<v8::Object> cbdata =
1107
1129
v8impl::CreateFunctionCallbackData (env, p->method , p->data );
1108
1130
@@ -1117,31 +1139,6 @@ napi_status napi_define_properties(napi_env env,
1117
1139
if (!define_maybe.FromMaybe (false )) {
1118
1140
return napi_set_last_error (env, napi_generic_failure);
1119
1141
}
1120
- } else if (p->getter != nullptr || p->setter != nullptr ) {
1121
- v8::Local<v8::Object> cbdata = v8impl::CreateAccessorCallbackData (
1122
- env,
1123
- p->getter ,
1124
- p->setter ,
1125
- p->data );
1126
-
1127
- // The napi_writable attribute is ignored for accessor descriptors,
1128
- // but V8 requires the ReadOnly attribute to match existence of a setter.
1129
- attributes = static_cast <v8::PropertyAttribute>(p->setter != nullptr
1130
- ? attributes & ~v8::PropertyAttribute::ReadOnly
1131
- : attributes | v8::PropertyAttribute::ReadOnly);
1132
-
1133
- auto set_maybe = obj->SetAccessor (
1134
- context,
1135
- name,
1136
- p->getter ? v8impl::GetterCallbackWrapper::Invoke : nullptr ,
1137
- p->setter ? v8impl::SetterCallbackWrapper::Invoke : nullptr ,
1138
- cbdata,
1139
- v8::AccessControl::DEFAULT,
1140
- attributes);
1141
-
1142
- if (!set_maybe.FromMaybe (false )) {
1143
- return napi_set_last_error (env, napi_invalid_arg);
1144
- }
1145
1142
} else {
1146
1143
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue (p->value );
1147
1144
0 commit comments