From f2315a9def44d68b6b3380ed191292d20531442a Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Mon, 3 Apr 2017 11:30:00 -0400 Subject: [PATCH 1/3] 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. --- src/node_api.cc | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/node_api.cc b/src/node_api.cc index a94ee6af4fd2a9..681c4e0bbc4a6f 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -741,8 +741,19 @@ napi_status napi_define_class(napi_env env, v8::Local property_name; CHECK_NEW_FROM_UTF8(isolate, property_name, p->utf8name); + // convert the properties from NAPI to v8 format + unsigned int attribute_flags = v8::None; + if ((p->attributes & napi_read_only)) { + attribute_flags |= v8::ReadOnly; + } + if ((p->attributes & napi_dont_enum)) { + attribute_flags |= v8::DontEnum; + } + if ((p->attributes & napi_dont_delete)) { + attribute_flags |= v8::DontDelete; + } v8::PropertyAttribute attributes = - static_cast(p->attributes); + static_cast(attribute_flags); // This code is similar to that in napi_define_property(); the // difference is it applies to a template instead of an object. From 444b519308a9cc3d8a3831ed32617cc5cc0f0ead Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 4 Apr 2017 14:32:23 -0400 Subject: [PATCH 2/3] squash: address comments --- src/node_api.cc | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index 681c4e0bbc4a6f..6068418ff1b0ac 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -25,6 +25,22 @@ napi_env JsEnvFromV8Isolate(v8::Isolate* isolate) { return reinterpret_cast(isolate); } +// convert from n-api property attributes to v8::PropertyAttribute +v8::PropertyAttribute V8PropertyAttributesFromAttributes( + napi_property_attributes attributes) { + unsigned int attribute_flags = v8::None; + if (attributes & napi_read_only) { + attribute_flags |= v8::ReadOnly; + } + if (attributes & napi_dont_enum) { + attribute_flags |= v8::DontEnum; + } + if (attributes & napi_dont_delete) { + attribute_flags |= v8::DontDelete; + } + return static_cast(attribute_flags); +} + v8::Isolate* V8IsolateFromJsEnv(napi_env e) { return reinterpret_cast(e); } @@ -740,20 +756,8 @@ napi_status napi_define_class(napi_env env, v8::Local property_name; CHECK_NEW_FROM_UTF8(isolate, property_name, p->utf8name); - - // convert the properties from NAPI to v8 format - unsigned int attribute_flags = v8::None; - if ((p->attributes & napi_read_only)) { - attribute_flags |= v8::ReadOnly; - } - if ((p->attributes & napi_dont_enum)) { - attribute_flags |= v8::DontEnum; - } - if ((p->attributes & napi_dont_delete)) { - attribute_flags |= v8::DontDelete; - } v8::PropertyAttribute attributes = - static_cast(attribute_flags); + v8impl::V8PropertyAttributesFromAttributes(p->attributes); // This code is similar to that in napi_define_property(); the // difference is it applies to a template instead of an object. @@ -1062,8 +1066,9 @@ napi_status napi_define_properties(napi_env env, v8::Local name; CHECK_NEW_FROM_UTF8(isolate, name, p->utf8name); - v8::PropertyAttribute attributes = static_cast( - p->attributes & ~napi_static_property); + v8::PropertyAttribute attributes = + v8impl::V8PropertyAttributesFromAttributes( + (napi_property_attributes)(p->attributes & ~napi_static_property)); if (p->method) { v8::Local cbdata = From fbcc3c6053dd52ea6213e0501ad64735aa0d4099 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Wed, 5 Apr 2017 12:03:15 -0400 Subject: [PATCH 3/3] squash: address suggestion --- src/node_api.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_api.cc b/src/node_api.cc index 6068418ff1b0ac..b61964cf96fede 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -26,7 +26,7 @@ napi_env JsEnvFromV8Isolate(v8::Isolate* isolate) { } // convert from n-api property attributes to v8::PropertyAttribute -v8::PropertyAttribute V8PropertyAttributesFromAttributes( +static inline v8::PropertyAttribute V8PropertyAttributesFromAttributes( napi_property_attributes attributes) { unsigned int attribute_flags = v8::None; if (attributes & napi_read_only) {