Skip to content

Commit 26c5fa7

Browse files
authored
Address PR feedback (nodejs#201)
- Consistent documentation for --napi-modules - Add underscore to napi_get_property_names - Avoid using V8 APIs marked as pending deprecation - Avoid unnecessary copying of args arrays - Other miscellaneous cleanup
1 parent d713ae3 commit 26c5fa7

5 files changed

Lines changed: 44 additions & 60 deletions

File tree

‎doc/node.1‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ Silence all process warnings (including deprecations).
121121

122122
.TP
123123
.BR \-\-napi\-modules
124-
Load N-API modules (experimental, opt-in by adding this flag).
124+
Enable loading native modules compiled with the ABI-stable Node.js API (N-API)
125+
(experimental).
125126

126127
.TP
127128
.BR \-\-trace\-warnings

‎src/node_api.cc‎

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class Reference {
186186
}
187187

188188
if (delete_self) {
189-
delete reference;
189+
Delete(reference);
190190
}
191191
}
192192

@@ -237,7 +237,6 @@ class CallbackWrapper {
237237
CallbackWrapper(napi_value this_arg, size_t args_length, void* data)
238238
: _this(this_arg), _args_length(args_length), _data(data) {}
239239

240-
virtual napi_value Holder() = 0;
241240
virtual bool IsConstructCall() = 0;
242241
virtual void Args(napi_value* buffer, size_t bufferlength) = 0;
243242
virtual void SetReturnValue(napi_value value) = 0;
@@ -254,7 +253,7 @@ class CallbackWrapper {
254253
void* _data;
255254
};
256255

257-
template <typename Info, int InternalFieldIndex>
256+
template <typename Info, int kInternalFieldIndex>
258257
class CallbackWrapperBase : public CallbackWrapper {
259258
public:
260259
CallbackWrapperBase(const Info& cbinfo, const size_t args_length)
@@ -267,11 +266,6 @@ class CallbackWrapperBase : public CallbackWrapper {
267266
->Value();
268267
}
269268

270-
/*virtual*/
271-
napi_value Holder() override {
272-
return JsValueFromV8LocalValue(_cbinfo.Holder());
273-
}
274-
275269
/*virtual*/
276270
bool IsConstructCall() override { return false; }
277271

@@ -281,7 +275,7 @@ class CallbackWrapperBase : public CallbackWrapper {
281275
static_cast<CallbackWrapper*>(this));
282276
napi_callback cb = reinterpret_cast<napi_callback>(
283277
v8::Local<v8::External>::Cast(
284-
_cbdata->GetInternalField(InternalFieldIndex))->Value());
278+
_cbdata->GetInternalField(kInternalFieldIndex))->Value());
285279
v8::Isolate* isolate = _cbinfo.GetIsolate();
286280
cb(v8impl::JsEnvFromV8Isolate(isolate), cbinfo_wrapper);
287281

@@ -634,7 +628,11 @@ napi_status napi_create_function(napi_env env,
634628
v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(
635629
isolate, v8impl::FunctionCallbackWrapper::Invoke, cbdata);
636630

637-
return_value = scope.Escape(tpl->GetFunction());
631+
v8::Local<v8::Context> context = isolate->GetCurrentContext();
632+
v8::MaybeLocal<v8::Function> maybe_function = tpl->GetFunction(context);
633+
CHECK_MAYBE_EMPTY(maybe_function, napi_generic_failure);
634+
635+
return_value = scope.Escape(maybe_function.ToLocalChecked());
638636

639637
if (utf8name != nullptr) {
640638
v8::Local<v8::String> name_string;
@@ -713,7 +711,7 @@ napi_status napi_define_class(napi_env env,
713711

714712
tpl->PrototypeTemplate()->SetAccessor(
715713
property_name,
716-
v8impl::GetterCallbackWrapper::Invoke,
714+
p->getter ? v8impl::GetterCallbackWrapper::Invoke : nullptr,
717715
p->setter ? v8impl::SetterCallbackWrapper::Invoke : nullptr,
718716
cbdata,
719717
v8::AccessControl::DEFAULT,
@@ -740,7 +738,7 @@ napi_status napi_define_class(napi_env env,
740738
napi_status status =
741739
napi_define_properties(env,
742740
*result,
743-
static_cast<int>(static_descriptors.size()),
741+
static_descriptors.size(),
744742
static_descriptors.data());
745743
if (status != napi_ok) return status;
746744
}
@@ -760,9 +758,9 @@ napi_status napi_set_return_value(napi_env env,
760758
return GET_RETURN_STATUS();
761759
}
762760

763-
napi_status napi_get_propertynames(napi_env env,
764-
napi_value object,
765-
napi_value* result) {
761+
napi_status napi_get_property_names(napi_env env,
762+
napi_value object,
763+
napi_value* result) {
766764
NAPI_PREAMBLE(env);
767765
CHECK_ARG(result);
768766

@@ -1028,27 +1026,23 @@ napi_status napi_define_properties(napi_env env,
10281026
auto set_maybe = obj->SetAccessor(
10291027
context,
10301028
name,
1031-
v8impl::GetterCallbackWrapper::Invoke,
1029+
p->getter ? v8impl::GetterCallbackWrapper::Invoke : nullptr,
10321030
p->setter ? v8impl::SetterCallbackWrapper::Invoke : nullptr,
10331031
cbdata,
10341032
v8::AccessControl::DEFAULT,
10351033
attributes);
10361034

1037-
// IsNothing seems like a serious failure,
1038-
// should we return a different error code if the set failed?
1039-
if (set_maybe.IsNothing() || !set_maybe.FromMaybe(false)) {
1040-
return napi_set_last_error(napi_generic_failure);
1035+
if (!set_maybe.FromMaybe(false)) {
1036+
return napi_set_last_error(napi_invalid_arg);
10411037
}
10421038
} else {
10431039
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(p->value);
10441040

10451041
auto define_maybe =
10461042
obj->DefineOwnProperty(context, name, value, attributes);
10471043

1048-
// IsNothing seems like a serious failure,
1049-
// should we return a different error code if the define failed?
1050-
if (define_maybe.IsNothing() || !define_maybe.FromMaybe(false)) {
1051-
return napi_set_last_error(napi_generic_failure);
1044+
if (!define_maybe.FromMaybe(false)) {
1045+
return napi_set_last_error(napi_invalid_arg);
10521046
}
10531047
}
10541048
}
@@ -1430,21 +1424,17 @@ napi_status napi_call_function(napi_env env,
14301424
NAPI_PREAMBLE(env);
14311425
CHECK_ARG(result);
14321426

1433-
std::vector<v8::Local<v8::Value>> args(argc);
14341427
v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv(env);
14351428
v8::Local<v8::Context> context = isolate->GetCurrentContext();
14361429

14371430
v8::Local<v8::Value> v8recv = v8impl::V8LocalValueFromJsValue(recv);
14381431

1439-
for (size_t i = 0; i < argc; i++) {
1440-
args[i] = v8impl::V8LocalValueFromJsValue(argv[i]);
1441-
}
1442-
14431432
v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue(func);
14441433
RETURN_STATUS_IF_FALSE(v8value->IsFunction(), napi_invalid_arg);
14451434

14461435
v8::Local<v8::Function> v8func = v8value.As<v8::Function>();
1447-
auto maybe = v8func->Call(context, v8recv, argc, args.data());
1436+
auto maybe = v8func->Call(context, v8recv, argc,
1437+
reinterpret_cast<v8::Local<v8::Value>*>(const_cast<napi_value*>(argv)));
14481438

14491439
if (try_catch.HasCaught()) {
14501440
return napi_set_last_error(napi_pending_exception);
@@ -1809,8 +1799,9 @@ napi_status napi_unwrap(napi_env env, napi_value js_object, void** result) {
18091799
CHECK_ARG(js_object);
18101800
CHECK_ARG(result);
18111801

1812-
v8::Local<v8::Object> obj =
1813-
v8impl::V8LocalValueFromJsValue(js_object).As<v8::Object>();
1802+
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(js_object);
1803+
RETURN_STATUS_IF_FALSE(value->IsObject(), napi_invalid_arg);
1804+
v8::Local<v8::Object> obj = value.As<v8::Object>();
18141805

18151806
// Only objects that were created from a NAPI constructor's prototype
18161807
// via napi_define_class() can be (un)wrapped.
@@ -2014,17 +2005,14 @@ napi_status napi_new_instance(napi_env env,
20142005
v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv(env);
20152006
v8::Local<v8::Context> context = isolate->GetCurrentContext();
20162007

2017-
std::vector<v8::Local<v8::Value>> args(argc);
2018-
for (size_t i = 0; i < argc; i++) {
2019-
args[i] = v8impl::V8LocalValueFromJsValue(argv[i]);
2020-
}
2021-
20222008
v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue(constructor);
20232009
RETURN_STATUS_IF_FALSE(v8value->IsFunction(), napi_invalid_arg);
20242010

20252011
v8::Local<v8::Function> ctor = v8value.As<v8::Function>();
20262012

2027-
auto maybe = ctor->NewInstance(context, argc, args.data());
2013+
auto maybe = ctor->NewInstance(context, argc,
2014+
reinterpret_cast<v8::Local<v8::Value>*>(const_cast<napi_value*>(argv)));
2015+
20282016
CHECK_MAYBE_EMPTY(maybe, napi_generic_failure);
20292017

20302018
*result = v8impl::JsValueFromV8LocalValue(maybe.ToLocalChecked());
@@ -2095,19 +2083,19 @@ napi_status napi_instanceof(napi_env env,
20952083
v8::Local<v8::String> prototype_string;
20962084
CHECK_NEW_FROM_UTF8(isolate, prototype_string, "prototype");
20972085

2098-
auto maybe = ctor->Get(context, prototype_string);
2099-
2100-
CHECK_MAYBE_EMPTY(maybe, napi_generic_failure);
2101-
2102-
v8::Local<v8::Value> prototype_property = maybe.ToLocalChecked();
2086+
auto maybe_prototype = ctor->Get(context, prototype_string);
2087+
CHECK_MAYBE_EMPTY(maybe_prototype, napi_generic_failure);
21032088

2089+
v8::Local<v8::Value> prototype_property = maybe_prototype.ToLocalChecked();
21042090
if (!prototype_property->IsObject()) {
2105-
napi_throw_type_error(env, "constructor prototype must be an object");
2091+
napi_throw_type_error(env, "constructor.prototype must be an object");
21062092

21072093
return napi_set_last_error(napi_object_expected);
21082094
}
21092095

2110-
ctor = prototype_property->ToObject();
2096+
auto maybe_ctor = prototype_property->ToObject(context);
2097+
CHECK_MAYBE_EMPTY(maybe_ctor, napi_generic_failure);
2098+
ctor = maybe_ctor.ToLocalChecked();
21112099

21122100
v8::Local<v8::Value> current_obj = v8impl::V8LocalValueFromJsValue(object);
21132101
if (!current_obj->StrictEquals(ctor)) {
@@ -2143,13 +2131,10 @@ napi_status napi_make_callback(napi_env env,
21432131
v8impl::V8LocalValueFromJsValue(recv).As<v8::Object>();
21442132
v8::Local<v8::Function> v8func =
21452133
v8impl::V8LocalValueFromJsValue(func).As<v8::Function>();
2146-
std::vector<v8::Local<v8::Value>> args(argc);
2147-
for (size_t i = 0; i < argc; i++) {
2148-
args[i] = v8impl::V8LocalValueFromJsValue(argv[i]);
2149-
}
21502134

21512135
*result = v8impl::JsValueFromV8LocalValue(
2152-
node::MakeCallback(isolate, v8recv, v8func, argc, args.data()));
2136+
node::MakeCallback(isolate, v8recv, v8func, argc,
2137+
reinterpret_cast<v8::Local<v8::Value>*>(const_cast<napi_value*>(argv))));
21532138

21542139
return GET_RETURN_STATUS();
21552140
}

‎src/node_api.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ typedef struct {
7171
#ifdef __cplusplus
7272
#define EXTERN_C_START extern "C" {
7373
#define EXTERN_C_END }
74-
#else /* ndef __cplusplus */
74+
#else
7575
#define EXTERN_C_START
7676
#define EXTERN_C_END
77-
#endif /* def __cplusplus */
77+
#endif
7878

7979
#define NAPI_MODULE_X(modname, regfunc, priv, flags) \
8080
EXTERN_C_START \
@@ -185,7 +185,7 @@ NAPI_EXTERN napi_status napi_get_value_string_utf16(napi_env env,
185185
size_t* result);
186186

187187
// Methods to coerce values
188-
// These APIs may execute user script
188+
// These APIs may execute user scripts
189189
NAPI_EXTERN napi_status napi_coerce_to_bool(napi_env env,
190190
napi_value value,
191191
napi_value* result);
@@ -203,9 +203,9 @@ NAPI_EXTERN napi_status napi_coerce_to_string(napi_env env,
203203
NAPI_EXTERN napi_status napi_get_prototype(napi_env env,
204204
napi_value object,
205205
napi_value* result);
206-
NAPI_EXTERN napi_status napi_get_propertynames(napi_env env,
207-
napi_value object,
208-
napi_value* result);
206+
NAPI_EXTERN napi_status napi_get_property_names(napi_env env,
207+
napi_value object,
208+
napi_value* result);
209209
NAPI_EXTERN napi_status napi_set_property(napi_env env,
210210
napi_value object,
211211
napi_value key,

‎src/node_api_types.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ typedef struct {
4343
void* data;
4444
} napi_property_descriptor;
4545

46-
#define DEFAULT_ATTR 0, 0, 0, napi_default, 0
47-
4846
typedef enum {
4947
// ES6 types (corresponds to typeof)
5048
napi_undefined,

‎test/addons-napi/test_object/test_object.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ void Inflate(napi_env env, napi_callback_info info) {
195195
napi_value obj = args[0];
196196

197197
napi_value propertynames;
198-
status = napi_get_propertynames(env, obj, &propertynames);
198+
status = napi_get_property_names(env, obj, &propertynames);
199199
if (status != napi_ok) return;
200200

201201
uint32_t i, length;

0 commit comments

Comments
 (0)