Skip to content

Commit f1bdbd1

Browse files
mhdawsonMylesBorins
authored andcommitted
n-api: break dep between v8 and napi attributes
The v8 n-api implementation had been depending on a one-to-one relationship between v8 and n-api v8 property attributes. Remove this dependency and fix coverity scan issue 165845. Backport-PR-URL: #19447 PR-URL: #12191 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
1 parent a9562fe commit f1bdbd1

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/node_api.cc

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ napi_env JsEnvFromV8Isolate(v8::Isolate* isolate) {
2626
return reinterpret_cast<napi_env>(isolate);
2727
}
2828

29+
// convert from n-api property attributes to v8::PropertyAttribute
30+
static inline v8::PropertyAttribute V8PropertyAttributesFromAttributes(
31+
napi_property_attributes attributes) {
32+
unsigned int attribute_flags = v8::None;
33+
if (attributes & napi_read_only) {
34+
attribute_flags |= v8::ReadOnly;
35+
}
36+
if (attributes & napi_dont_enum) {
37+
attribute_flags |= v8::DontEnum;
38+
}
39+
if (attributes & napi_dont_delete) {
40+
attribute_flags |= v8::DontDelete;
41+
}
42+
return static_cast<v8::PropertyAttribute>(attribute_flags);
43+
}
44+
2945
v8::Isolate* V8IsolateFromJsEnv(napi_env e) {
3046
return reinterpret_cast<v8::Isolate*>(e);
3147
}
@@ -741,9 +757,8 @@ napi_status napi_define_class(napi_env env,
741757

742758
v8::Local<v8::String> property_name;
743759
CHECK_NEW_FROM_UTF8(isolate, property_name, p->utf8name);
744-
745760
v8::PropertyAttribute attributes =
746-
static_cast<v8::PropertyAttribute>(p->attributes);
761+
v8impl::V8PropertyAttributesFromAttributes(p->attributes);
747762

748763
// This code is similar to that in napi_define_property(); the
749764
// difference is it applies to a template instead of an object.
@@ -1052,8 +1067,9 @@ napi_status napi_define_properties(napi_env env,
10521067
v8::Local<v8::Name> name;
10531068
CHECK_NEW_FROM_UTF8(isolate, name, p->utf8name);
10541069

1055-
v8::PropertyAttribute attributes = static_cast<v8::PropertyAttribute>(
1056-
p->attributes & ~napi_static_property);
1070+
v8::PropertyAttribute attributes =
1071+
v8impl::V8PropertyAttributesFromAttributes(
1072+
(napi_property_attributes)(p->attributes & ~napi_static_property));
10571073

10581074
if (p->method) {
10591075
v8::Local<v8::Object> cbdata =

0 commit comments

Comments
 (0)