Skip to content

Commit c69acfb

Browse files
committed
Address some minor PR feedback
- Simplify conversion to/from napi_value - Better names for CallbackWrapper template params - Fatal error when trying to set setter return value - Use node::arraysize - Replace v8::Handle with v8::Local - Align the star after v8::Isolate - Remove some non-ascii whitespace chars that were causing some IDEs to auto-add a UTF-8 BOM
1 parent b3d806c commit c69acfb

File tree

2 files changed

+40
-57
lines changed

2 files changed

+40
-57
lines changed

src/node_api.cc

+38-55
Original file line numberDiff line numberDiff line change
@@ -74,41 +74,19 @@ V8EscapableHandleScopeFromJsEscapableHandleScope(
7474

7575
//=== Conversion between V8 Handles and napi_value ========================
7676

77-
// This is assuming v8::Local<> will always be implemented with a single
78-
// pointer field so that we can pass it around as a void* (maybe we should
79-
// use intptr_t instead of void*)
77+
// This asserts v8::Local<> will always be implemented with a single
78+
// pointer field so that we can pass it around as a void*.
79+
static_assert(sizeof(v8::Local<v8::Value>) == sizeof(napi_value),
80+
"Cannot convert between v8::Local<v8::Value> and napi_value");
8081

8182
napi_value JsValueFromV8LocalValue(v8::Local<v8::Value> local) {
82-
// This is awkward, better way? memcpy but don't want that dependency?
83-
union U {
84-
napi_value v;
85-
v8::Local<v8::Value> l;
86-
U(v8::Local<v8::Value> _l) : l(_l) {}
87-
} u(local);
88-
assert(sizeof(u.v) == sizeof(u.l));
89-
return u.v;
83+
return reinterpret_cast<napi_value>(*local);
9084
}
9185

9286
v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
93-
// Likewise awkward
94-
union U {
95-
napi_value v;
96-
v8::Local<v8::Value> l;
97-
U(napi_value _v) : v(_v) {}
98-
} u(v);
99-
assert(sizeof(u.v) == sizeof(u.l));
100-
return u.l;
101-
}
102-
103-
static v8::Local<v8::Function> V8LocalFunctionFromJsValue(napi_value v) {
104-
// Likewise awkward
105-
union U {
106-
napi_value v;
107-
v8::Local<v8::Function> f;
108-
U(napi_value _v) : v(_v) {}
109-
} u(v);
110-
assert(sizeof(u.v) == sizeof(u.f));
111-
return u.f;
87+
v8::Local<v8::Value> local;
88+
memcpy(&local, &v, sizeof(v));
89+
return local;
11290
}
11391

11492
// Wrapper around v8::Persistent that implements reference counting.
@@ -254,10 +232,10 @@ class CallbackWrapper {
254232
void* _data;
255233
};
256234

257-
template <typename T, int I>
235+
template <typename Info, int InternalFieldIndex>
258236
class CallbackWrapperBase : public CallbackWrapper {
259237
public:
260-
CallbackWrapperBase(const T& cbinfo, const int argsLength)
238+
CallbackWrapperBase(const Info& cbinfo, const int argsLength)
261239
: CallbackWrapper(JsValueFromV8LocalValue(cbinfo.This()),
262240
argsLength,
263241
nullptr),
@@ -280,7 +258,8 @@ class CallbackWrapperBase : public CallbackWrapper {
280258
napi_callback_info cbinfo_wrapper = reinterpret_cast<napi_callback_info>(
281259
static_cast<CallbackWrapper*>(this));
282260
napi_callback cb = reinterpret_cast<napi_callback>(
283-
v8::Local<v8::External>::Cast(_cbdata->GetInternalField(I))->Value());
261+
v8::Local<v8::External>::Cast(
262+
_cbdata->GetInternalField(InternalFieldIndex))->Value());
284263
v8::Isolate* isolate = _cbinfo.GetIsolate();
285264
cb(v8impl::JsEnvFromV8Isolate(isolate), cbinfo_wrapper);
286265

@@ -291,7 +270,7 @@ class CallbackWrapperBase : public CallbackWrapper {
291270
}
292271
}
293272

294-
const T& _cbinfo;
273+
const Info& _cbinfo;
295274
const v8::Local<v8::Object> _cbdata;
296275
};
297276

@@ -399,8 +378,8 @@ class SetterCallbackWrapper
399378

400379
/*virtual*/
401380
void SetReturnValue(napi_value v) override {
402-
// Cannot set the return value of a setter.
403-
assert(false);
381+
node::FatalError("napi_set_return_value",
382+
"Cannot return a value from a setter callback.");
404383
}
405384

406385
private:
@@ -576,7 +555,7 @@ void napi_module_register(napi_module* mod) {
576555
: napi_set_last_error(napi_pending_exception))
577556

578557
// Static last error returned from napi_get_last_error_info
579-
napi_extended_error_info static_last_error = {};
558+
napi_extended_error_info static_last_error = { nullptr, nullptr, 0, napi_ok };
580559

581560
// Warning: Keep in-sync with napi_status enum
582561
const char* error_messages[] = {nullptr,
@@ -598,8 +577,7 @@ void napi_clear_last_error() {
598577
}
599578

600579
const napi_extended_error_info* napi_get_last_error_info() {
601-
static_assert(
602-
sizeof(error_messages) / sizeof(*error_messages) == napi_status_last,
580+
static_assert(node::arraysize(error_messages) == napi_status_last,
603581
"Count of error messages must match count of error values");
604582
assert(static_last_error.error_code < napi_status_last);
605583

@@ -629,7 +607,7 @@ napi_status napi_create_function(napi_env e,
629607
NAPI_PREAMBLE(e);
630608
CHECK_ARG(result);
631609

632-
v8::Isolate *isolate = v8impl::V8IsolateFromJsEnv(e);
610+
v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv(e);
633611
v8::Local<v8::Function> retval;
634612

635613
v8::EscapableHandleScope scope(isolate);
@@ -774,7 +752,7 @@ napi_status napi_get_propertynames(napi_env e,
774752
NAPI_PREAMBLE(e);
775753
CHECK_ARG(result);
776754

777-
v8::Isolate *isolate = v8impl::V8IsolateFromJsEnv(e);
755+
v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv(e);
778756
v8::Local<v8::Context> context = isolate->GetCurrentContext();
779757
v8::Local<v8::Object> obj;
780758
CHECK_TO_OBJECT(context, obj, object);
@@ -794,7 +772,7 @@ napi_status napi_set_property(napi_env e,
794772
napi_value value) {
795773
NAPI_PREAMBLE(e);
796774

797-
v8::Isolate *isolate = v8impl::V8IsolateFromJsEnv(e);
775+
v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv(e);
798776
v8::Local<v8::Context> context = isolate->GetCurrentContext();
799777
v8::Local<v8::Object> obj;
800778

@@ -860,7 +838,7 @@ napi_status napi_set_named_property(napi_env e,
860838
napi_value value) {
861839
NAPI_PREAMBLE(e);
862840

863-
v8::Isolate *isolate = v8impl::V8IsolateFromJsEnv(e);
841+
v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv(e);
864842
v8::Local<v8::Context> context = isolate->GetCurrentContext();
865843
v8::Local<v8::Object> obj;
866844

@@ -1433,17 +1411,20 @@ napi_status napi_call_function(napi_env e,
14331411
NAPI_PREAMBLE(e);
14341412
CHECK_ARG(result);
14351413

1436-
std::vector<v8::Handle<v8::Value>> args(argc);
1414+
std::vector<v8::Local<v8::Value>> args(argc);
14371415
v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv(e);
14381416
v8::Local<v8::Context> context = isolate->GetCurrentContext();
14391417

1440-
v8::Handle<v8::Value> v8recv = v8impl::V8LocalValueFromJsValue(recv);
1418+
v8::Local<v8::Value> v8recv = v8impl::V8LocalValueFromJsValue(recv);
14411419

14421420
for (int i = 0; i < argc; i++) {
14431421
args[i] = v8impl::V8LocalValueFromJsValue(argv[i]);
14441422
}
14451423

1446-
v8::Local<v8::Function> v8func = v8impl::V8LocalFunctionFromJsValue(func);
1424+
v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue(func);
1425+
RETURN_STATUS_IF_FALSE(v8value->IsFunction(), napi_invalid_arg);
1426+
1427+
v8::Local<v8::Function> v8func = v8value.As<v8::Function>();
14471428
auto maybe = v8func->Call(context, v8recv, argc, args.data());
14481429

14491430
if (tryCatch.HasCaught()) {
@@ -1898,8 +1879,8 @@ napi_status napi_delete_reference(napi_env e, napi_ref ref) {
18981879
// After this call the
18991880
// reference will be a strong reference because its refcount is >0, and the
19001881
// referenced object is
1901-
// effectively "pinned". Calling this when the refcount is 0 and the object
1902-
// is unavailable
1882+
// effectively "pinned". Calling this when the refcount is 0 and the object
1883+
// is unavailable
19031884
// results in an error.
19041885
napi_status napi_reference_addref(napi_env e, napi_ref ref, int* result) {
19051886
NAPI_PREAMBLE(e);
@@ -1919,7 +1900,7 @@ napi_status napi_reference_addref(napi_env e, napi_ref ref, int* result) {
19191900
// the result is
19201901
// 0 the reference is now weak and the object may be GC'd at any time if there
19211902
// are no other
1922-
// references. Calling this when the refcount is already 0 results in an error.
1903+
// references. Calling this when the refcount is already 0 results in an error.
19231904
napi_status napi_reference_release(napi_env e, napi_ref ref, int* result) {
19241905
NAPI_PREAMBLE(e);
19251906
CHECK_ARG(ref);
@@ -1939,7 +1920,7 @@ napi_status napi_reference_release(napi_env e, napi_ref ref, int* result) {
19391920

19401921
// Attempts to get a referenced value. If the reference is weak, the value might
19411922
// no longer be
1942-
// available, in that case the call is still successful but the result is NULL.
1923+
// available, in that case the call is still successful but the result is NULL.
19431924
napi_status napi_get_reference_value(napi_env e,
19441925
napi_ref ref,
19451926
napi_value* result) {
@@ -2017,13 +1998,15 @@ napi_status napi_new_instance(napi_env e,
20171998
v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv(e);
20181999
v8::Local<v8::Context> context = isolate->GetCurrentContext();
20192000

2020-
std::vector<v8::Handle<v8::Value>> args(argc);
2001+
std::vector<v8::Local<v8::Value>> args(argc);
20212002
for (int i = 0; i < argc; i++) {
20222003
args[i] = v8impl::V8LocalValueFromJsValue(argv[i]);
20232004
}
20242005

2025-
v8::Local<v8::Function> v8cons =
2026-
v8impl::V8LocalFunctionFromJsValue(constructor);
2006+
v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue(constructor);
2007+
RETURN_STATUS_IF_FALSE(v8value->IsFunction(), napi_invalid_arg);
2008+
2009+
v8::Local<v8::Function> v8cons = v8value.As<v8::Function>();
20272010

20282011
auto maybe = v8cons->NewInstance(context, argc, args.data());
20292012
CHECK_MAYBE_EMPTY(maybe, napi_generic_failure);
@@ -2102,12 +2085,12 @@ napi_status napi_make_callback(napi_env e,
21022085
v8impl::V8LocalValueFromJsValue(recv).As<v8::Object>();
21032086
v8::Local<v8::Function> v8func =
21042087
v8impl::V8LocalValueFromJsValue(func).As<v8::Function>();
2105-
std::vector<v8::Handle<v8::Value>> args(argc);
2088+
std::vector<v8::Local<v8::Value>> args(argc);
21062089
for (int i = 0; i < argc; i++) {
21072090
args[i] = v8impl::V8LocalValueFromJsValue(argv[i]);
21082091
}
21092092

2110-
v8::Handle<v8::Value> retval =
2093+
v8::Local<v8::Value> retval =
21112094
node::MakeCallback(isolate, v8recv, v8func, argc, args.data());
21122095
*result = v8impl::JsValueFromV8LocalValue(retval);
21132096

src/node_api.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,14 @@ NAPI_EXTERN napi_status napi_reference_addref(napi_env env,
378378
// Decrements the reference count, optionally returning the resulting count.
379379
// If the result is 0 the reference is now weak and the object may be GC'd
380380
// at any time if there are no other references. Calling this when the
381-
// refcount is already 0 results in an error.
381+
// refcount is already 0 results in an error.
382382
NAPI_EXTERN napi_status napi_reference_release(napi_env env,
383383
napi_ref ref,
384384
int* result);
385385

386386
// Attempts to get a referenced value. If the reference is weak,
387387
// the value might no longer be available, in that case the call
388-
// is still successful but the result is NULL.
388+
// is still successful but the result is NULL.
389389
NAPI_EXTERN napi_status napi_get_reference_value(napi_env env,
390390
napi_ref ref,
391391
napi_value* result);

0 commit comments

Comments
 (0)