@@ -74,41 +74,19 @@ V8EscapableHandleScopeFromJsEscapableHandleScope(
74
74
75
75
// === Conversion between V8 Handles and napi_value ========================
76
76
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" );
80
81
81
82
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);
90
84
}
91
85
92
86
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;
112
90
}
113
91
114
92
// Wrapper around v8::Persistent that implements reference counting.
@@ -254,10 +232,10 @@ class CallbackWrapper {
254
232
void * _data;
255
233
};
256
234
257
- template <typename T , int I >
235
+ template <typename Info , int InternalFieldIndex >
258
236
class CallbackWrapperBase : public CallbackWrapper {
259
237
public:
260
- CallbackWrapperBase (const T & cbinfo, const int argsLength)
238
+ CallbackWrapperBase (const Info & cbinfo, const int argsLength)
261
239
: CallbackWrapper(JsValueFromV8LocalValue(cbinfo.This()),
262
240
argsLength,
263
241
nullptr ),
@@ -280,7 +258,8 @@ class CallbackWrapperBase : public CallbackWrapper {
280
258
napi_callback_info cbinfo_wrapper = reinterpret_cast <napi_callback_info>(
281
259
static_cast <CallbackWrapper*>(this ));
282
260
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 ());
284
263
v8::Isolate* isolate = _cbinfo.GetIsolate ();
285
264
cb (v8impl::JsEnvFromV8Isolate (isolate), cbinfo_wrapper);
286
265
@@ -291,7 +270,7 @@ class CallbackWrapperBase : public CallbackWrapper {
291
270
}
292
271
}
293
272
294
- const T & _cbinfo;
273
+ const Info & _cbinfo;
295
274
const v8::Local<v8::Object> _cbdata;
296
275
};
297
276
@@ -399,8 +378,8 @@ class SetterCallbackWrapper
399
378
400
379
/* virtual*/
401
380
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. " );
404
383
}
405
384
406
385
private:
@@ -576,7 +555,7 @@ void napi_module_register(napi_module* mod) {
576
555
: napi_set_last_error(napi_pending_exception))
577
556
578
557
// 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 };
580
559
581
560
// Warning: Keep in-sync with napi_status enum
582
561
const char * error_messages[] = {nullptr ,
@@ -598,8 +577,7 @@ void napi_clear_last_error() {
598
577
}
599
578
600
579
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,
603
581
" Count of error messages must match count of error values" );
604
582
assert (static_last_error.error_code < napi_status_last);
605
583
@@ -629,7 +607,7 @@ napi_status napi_create_function(napi_env e,
629
607
NAPI_PREAMBLE (e);
630
608
CHECK_ARG (result);
631
609
632
- v8::Isolate * isolate = v8impl::V8IsolateFromJsEnv (e);
610
+ v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv (e);
633
611
v8::Local<v8::Function> retval;
634
612
635
613
v8::EscapableHandleScope scope (isolate);
@@ -774,7 +752,7 @@ napi_status napi_get_propertynames(napi_env e,
774
752
NAPI_PREAMBLE (e);
775
753
CHECK_ARG (result);
776
754
777
- v8::Isolate * isolate = v8impl::V8IsolateFromJsEnv (e);
755
+ v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv (e);
778
756
v8::Local<v8::Context> context = isolate->GetCurrentContext ();
779
757
v8::Local<v8::Object> obj;
780
758
CHECK_TO_OBJECT (context, obj, object);
@@ -794,7 +772,7 @@ napi_status napi_set_property(napi_env e,
794
772
napi_value value) {
795
773
NAPI_PREAMBLE (e);
796
774
797
- v8::Isolate * isolate = v8impl::V8IsolateFromJsEnv (e);
775
+ v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv (e);
798
776
v8::Local<v8::Context> context = isolate->GetCurrentContext ();
799
777
v8::Local<v8::Object> obj;
800
778
@@ -860,7 +838,7 @@ napi_status napi_set_named_property(napi_env e,
860
838
napi_value value) {
861
839
NAPI_PREAMBLE (e);
862
840
863
- v8::Isolate * isolate = v8impl::V8IsolateFromJsEnv (e);
841
+ v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv (e);
864
842
v8::Local<v8::Context> context = isolate->GetCurrentContext ();
865
843
v8::Local<v8::Object> obj;
866
844
@@ -1433,17 +1411,20 @@ napi_status napi_call_function(napi_env e,
1433
1411
NAPI_PREAMBLE (e);
1434
1412
CHECK_ARG (result);
1435
1413
1436
- std::vector<v8::Handle <v8::Value>> args (argc);
1414
+ std::vector<v8::Local <v8::Value>> args (argc);
1437
1415
v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv (e);
1438
1416
v8::Local<v8::Context> context = isolate->GetCurrentContext ();
1439
1417
1440
- v8::Handle <v8::Value> v8recv = v8impl::V8LocalValueFromJsValue (recv);
1418
+ v8::Local <v8::Value> v8recv = v8impl::V8LocalValueFromJsValue (recv);
1441
1419
1442
1420
for (int i = 0 ; i < argc; i++) {
1443
1421
args[i] = v8impl::V8LocalValueFromJsValue (argv[i]);
1444
1422
}
1445
1423
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>();
1447
1428
auto maybe = v8func->Call (context, v8recv, argc, args.data ());
1448
1429
1449
1430
if (tryCatch.HasCaught ()) {
@@ -1898,8 +1879,8 @@ napi_status napi_delete_reference(napi_env e, napi_ref ref) {
1898
1879
// After this call the
1899
1880
// reference will be a strong reference because its refcount is >0, and the
1900
1881
// 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
1903
1884
// results in an error.
1904
1885
napi_status napi_reference_addref (napi_env e, napi_ref ref, int * result) {
1905
1886
NAPI_PREAMBLE (e);
@@ -1919,7 +1900,7 @@ napi_status napi_reference_addref(napi_env e, napi_ref ref, int* result) {
1919
1900
// the result is
1920
1901
// 0 the reference is now weak and the object may be GC'd at any time if there
1921
1902
// 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.
1923
1904
napi_status napi_reference_release (napi_env e, napi_ref ref, int * result) {
1924
1905
NAPI_PREAMBLE (e);
1925
1906
CHECK_ARG (ref);
@@ -1939,7 +1920,7 @@ napi_status napi_reference_release(napi_env e, napi_ref ref, int* result) {
1939
1920
1940
1921
// Attempts to get a referenced value. If the reference is weak, the value might
1941
1922
// 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.
1943
1924
napi_status napi_get_reference_value (napi_env e,
1944
1925
napi_ref ref,
1945
1926
napi_value* result) {
@@ -2017,13 +1998,15 @@ napi_status napi_new_instance(napi_env e,
2017
1998
v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv (e);
2018
1999
v8::Local<v8::Context> context = isolate->GetCurrentContext ();
2019
2000
2020
- std::vector<v8::Handle <v8::Value>> args (argc);
2001
+ std::vector<v8::Local <v8::Value>> args (argc);
2021
2002
for (int i = 0 ; i < argc; i++) {
2022
2003
args[i] = v8impl::V8LocalValueFromJsValue (argv[i]);
2023
2004
}
2024
2005
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>();
2027
2010
2028
2011
auto maybe = v8cons->NewInstance (context, argc, args.data ());
2029
2012
CHECK_MAYBE_EMPTY (maybe, napi_generic_failure);
@@ -2102,12 +2085,12 @@ napi_status napi_make_callback(napi_env e,
2102
2085
v8impl::V8LocalValueFromJsValue (recv).As <v8::Object>();
2103
2086
v8::Local<v8::Function> v8func =
2104
2087
v8impl::V8LocalValueFromJsValue (func).As <v8::Function>();
2105
- std::vector<v8::Handle <v8::Value>> args (argc);
2088
+ std::vector<v8::Local <v8::Value>> args (argc);
2106
2089
for (int i = 0 ; i < argc; i++) {
2107
2090
args[i] = v8impl::V8LocalValueFromJsValue (argv[i]);
2108
2091
}
2109
2092
2110
- v8::Handle <v8::Value> retval =
2093
+ v8::Local <v8::Value> retval =
2111
2094
node::MakeCallback (isolate, v8recv, v8func, argc, args.data ());
2112
2095
*result = v8impl::JsValueFromV8LocalValue (retval);
2113
2096
0 commit comments