@@ -35,18 +35,26 @@ 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_read_only) {
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
+ } else if ((descriptor->attributes & napi_writable) == 0 ) {
48
+ attribute_flags |= v8::PropertyAttribute::ReadOnly;
43
49
}
44
- if (attributes & napi_dont_enum) {
45
- attribute_flags |= v8::DontEnum;
50
+
51
+ if ((descriptor->attributes & napi_enumerable) == 0 ) {
52
+ attribute_flags |= v8::PropertyAttribute::DontEnum;
46
53
}
47
- if (attributes & napi_dont_delete ) {
48
- attribute_flags |= v8::DontDelete;
54
+ if ((descriptor-> attributes & napi_configurable) == 0 ) {
55
+ attribute_flags |= v8::PropertyAttribute:: DontDelete;
49
56
}
57
+
50
58
return static_cast <v8::PropertyAttribute>(attribute_flags);
51
59
}
52
60
@@ -775,7 +783,7 @@ napi_status napi_define_class(napi_env env,
775
783
for (size_t i = 0 ; i < property_count; i++) {
776
784
const napi_property_descriptor* p = properties + i;
777
785
778
- if ((p->attributes & napi_static_property ) != 0 ) {
786
+ if ((p->attributes & napi_static ) != 0 ) {
779
787
// Static properties are handled separately below.
780
788
static_property_count++;
781
789
continue ;
@@ -784,25 +792,11 @@ napi_status napi_define_class(napi_env env,
784
792
v8::Local<v8::String> property_name;
785
793
CHECK_NEW_FROM_UTF8 (env, property_name, p->utf8name );
786
794
v8::PropertyAttribute attributes =
787
- v8impl::V8PropertyAttributesFromAttributes (p-> attributes );
795
+ v8impl::V8PropertyAttributesFromDescriptor (p );
788
796
789
- // This code is similar to that in napi_define_property (); the
797
+ // This code is similar to that in napi_define_properties (); the
790
798
// difference is it applies to a template instead of an object.
791
- if (p->method ) {
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 || p->setter ) {
799
+ if (p->getter != nullptr || p->setter != nullptr ) {
806
800
v8::Local<v8::Object> cbdata = v8impl::CreateAccessorCallbackData (
807
801
env, p->getter , p->setter , p->data );
808
802
@@ -813,6 +807,20 @@ napi_status napi_define_class(napi_env env,
813
807
cbdata,
814
808
v8::AccessControl::DEFAULT,
815
809
attributes);
810
+ } else if (p->method != nullptr ) {
811
+ v8::Local<v8::Object> cbdata =
812
+ v8impl::CreateFunctionCallbackData (env, p->method , p->data );
813
+
814
+ RETURN_STATUS_IF_FALSE (env, !cbdata.IsEmpty (), napi_generic_failure);
815
+
816
+ v8::Local<v8::FunctionTemplate> t =
817
+ v8::FunctionTemplate::New (isolate,
818
+ v8impl::FunctionCallbackWrapper::Invoke,
819
+ cbdata,
820
+ v8::Signature::New (isolate, tpl));
821
+ t->SetClassName (property_name);
822
+
823
+ tpl->PrototypeTemplate ()->Set (property_name, t, attributes);
816
824
} else {
817
825
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue (p->value );
818
826
tpl->PrototypeTemplate ()->Set (property_name, value, attributes);
@@ -827,7 +835,7 @@ napi_status napi_define_class(napi_env env,
827
835
828
836
for (size_t i = 0 ; i < property_count; i++) {
829
837
const napi_property_descriptor* p = properties + i;
830
- if ((p->attributes & napi_static_property ) != 0 ) {
838
+ if ((p->attributes & napi_static ) != 0 ) {
831
839
static_descriptors.push_back (*p);
832
840
}
833
841
}
@@ -1094,10 +1102,28 @@ napi_status napi_define_properties(napi_env env,
1094
1102
CHECK_NEW_FROM_UTF8 (env, name, p->utf8name );
1095
1103
1096
1104
v8::PropertyAttribute attributes =
1097
- v8impl::V8PropertyAttributesFromAttributes (
1098
- (napi_property_attributes)(p->attributes & ~napi_static_property));
1105
+ v8impl::V8PropertyAttributesFromDescriptor (p);
1106
+
1107
+ if (p->getter != nullptr || p->setter != nullptr ) {
1108
+ v8::Local<v8::Object> cbdata = v8impl::CreateAccessorCallbackData (
1109
+ env,
1110
+ p->getter ,
1111
+ p->setter ,
1112
+ p->data );
1099
1113
1100
- if (p->method ) {
1114
+ auto set_maybe = obj->SetAccessor (
1115
+ context,
1116
+ name,
1117
+ p->getter ? v8impl::GetterCallbackWrapper::Invoke : nullptr ,
1118
+ p->setter ? v8impl::SetterCallbackWrapper::Invoke : nullptr ,
1119
+ cbdata,
1120
+ v8::AccessControl::DEFAULT,
1121
+ attributes);
1122
+
1123
+ if (!set_maybe.FromMaybe (false )) {
1124
+ return napi_set_last_error (env, napi_invalid_arg);
1125
+ }
1126
+ } else if (p->method != nullptr ) {
1101
1127
v8::Local<v8::Object> cbdata =
1102
1128
v8impl::CreateFunctionCallbackData (env, p->method , p->data );
1103
1129
@@ -1112,25 +1138,6 @@ napi_status napi_define_properties(napi_env env,
1112
1138
if (!define_maybe.FromMaybe (false )) {
1113
1139
return napi_set_last_error (env, napi_generic_failure);
1114
1140
}
1115
- } else if (p->getter || p->setter ) {
1116
- v8::Local<v8::Object> cbdata = v8impl::CreateAccessorCallbackData (
1117
- env,
1118
- p->getter ,
1119
- p->setter ,
1120
- p->data );
1121
-
1122
- auto set_maybe = obj->SetAccessor (
1123
- context,
1124
- name,
1125
- p->getter ? v8impl::GetterCallbackWrapper::Invoke : nullptr ,
1126
- p->setter ? v8impl::SetterCallbackWrapper::Invoke : nullptr ,
1127
- cbdata,
1128
- v8::AccessControl::DEFAULT,
1129
- attributes);
1130
-
1131
- if (!set_maybe.FromMaybe (false )) {
1132
- return napi_set_last_error (env, napi_invalid_arg);
1133
- }
1134
1141
} else {
1135
1142
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue (p->value );
1136
1143
0 commit comments