@@ -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.
@@ -275,10 +253,10 @@ class CallbackWrapper {
275
253
void * _data;
276
254
};
277
255
278
- template <typename T , int I >
256
+ template <typename Info , int InternalFieldIndex >
279
257
class CallbackWrapperBase : public CallbackWrapper {
280
258
public:
281
- CallbackWrapperBase (const T & cbinfo, const size_t args_length)
259
+ CallbackWrapperBase (const Info & cbinfo, const size_t args_length)
282
260
: CallbackWrapper(JsValueFromV8LocalValue(cbinfo.This()),
283
261
args_length,
284
262
nullptr ),
@@ -301,7 +279,8 @@ class CallbackWrapperBase : public CallbackWrapper {
301
279
napi_callback_info cbinfo_wrapper = reinterpret_cast <napi_callback_info>(
302
280
static_cast <CallbackWrapper*>(this ));
303
281
napi_callback cb = reinterpret_cast <napi_callback>(
304
- v8::Local<v8::External>::Cast (_cbdata->GetInternalField (I))->Value ());
282
+ v8::Local<v8::External>::Cast (
283
+ _cbdata->GetInternalField (InternalFieldIndex))->Value ());
305
284
v8::Isolate* isolate = _cbinfo.GetIsolate ();
306
285
cb (v8impl::JsEnvFromV8Isolate (isolate), cbinfo_wrapper);
307
286
@@ -312,7 +291,7 @@ class CallbackWrapperBase : public CallbackWrapper {
312
291
}
313
292
}
314
293
315
- const T & _cbinfo;
294
+ const Info & _cbinfo;
316
295
const v8::Local<v8::Object> _cbdata;
317
296
};
318
297
@@ -420,8 +399,8 @@ class SetterCallbackWrapper
420
399
421
400
/* virtual*/
422
401
void SetReturnValue (napi_value value) override {
423
- // Cannot set the return value of a setter.
424
- assert ( false );
402
+ node::FatalError ( " napi_set_return_value " ,
403
+ " Cannot return a value from a setter callback. " );
425
404
}
426
405
427
406
private:
@@ -597,7 +576,7 @@ void napi_module_register(napi_module* mod) {
597
576
: napi_set_last_error(napi_pending_exception))
598
577
599
578
// Static last error returned from napi_get_last_error_info
600
- napi_extended_error_info static_last_error = {};
579
+ napi_extended_error_info static_last_error = { nullptr , nullptr , 0 , napi_ok };
601
580
602
581
// Warning: Keep in-sync with napi_status enum
603
582
const char * error_messages[] = {nullptr ,
@@ -620,8 +599,7 @@ void napi_clear_last_error() {
620
599
}
621
600
622
601
const napi_extended_error_info* napi_get_last_error_info () {
623
- static_assert (
624
- sizeof (error_messages) / sizeof (*error_messages) == napi_status_last,
602
+ static_assert (node::arraysize (error_messages) == napi_status_last,
625
603
" Count of error messages must match count of error values" );
626
604
assert (static_last_error.error_code < napi_status_last);
627
605
@@ -651,7 +629,7 @@ napi_status napi_create_function(napi_env env,
651
629
NAPI_PREAMBLE (env);
652
630
CHECK_ARG (result);
653
631
654
- v8::Isolate * isolate = v8impl::V8IsolateFromJsEnv (env);
632
+ v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv (env);
655
633
v8::Local<v8::Function> return_value;
656
634
v8::EscapableHandleScope scope (isolate);
657
635
v8::Local<v8::Object> cbdata =
@@ -794,7 +772,7 @@ napi_status napi_get_propertynames(napi_env env,
794
772
NAPI_PREAMBLE (env);
795
773
CHECK_ARG (result);
796
774
797
- v8::Isolate * isolate = v8impl::V8IsolateFromJsEnv (env);
775
+ v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv (env);
798
776
v8::Local<v8::Context> context = isolate->GetCurrentContext ();
799
777
v8::Local<v8::Object> obj;
800
778
CHECK_TO_OBJECT (context, obj, object);
@@ -814,7 +792,7 @@ napi_status napi_set_property(napi_env env,
814
792
napi_value value) {
815
793
NAPI_PREAMBLE (env);
816
794
817
- v8::Isolate * isolate = v8impl::V8IsolateFromJsEnv (env);
795
+ v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv (env);
818
796
v8::Local<v8::Context> context = isolate->GetCurrentContext ();
819
797
v8::Local<v8::Object> obj;
820
798
@@ -880,7 +858,7 @@ napi_status napi_set_named_property(napi_env env,
880
858
napi_value value) {
881
859
NAPI_PREAMBLE (env);
882
860
883
- v8::Isolate * isolate = v8impl::V8IsolateFromJsEnv (env);
861
+ v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv (env);
884
862
v8::Local<v8::Context> context = isolate->GetCurrentContext ();
885
863
v8::Local<v8::Object> obj;
886
864
@@ -1448,17 +1426,20 @@ napi_status napi_call_function(napi_env env,
1448
1426
NAPI_PREAMBLE (env);
1449
1427
CHECK_ARG (result);
1450
1428
1451
- std::vector<v8::Handle <v8::Value>> args (argc);
1429
+ std::vector<v8::Local <v8::Value>> args (argc);
1452
1430
v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv (env);
1453
1431
v8::Local<v8::Context> context = isolate->GetCurrentContext ();
1454
1432
1455
- v8::Handle <v8::Value> v8recv = v8impl::V8LocalValueFromJsValue (recv);
1433
+ v8::Local <v8::Value> v8recv = v8impl::V8LocalValueFromJsValue (recv);
1456
1434
1457
1435
for (size_t i = 0 ; i < argc; i++) {
1458
1436
args[i] = v8impl::V8LocalValueFromJsValue (argv[i]);
1459
1437
}
1460
1438
1461
- v8::Local<v8::Function> v8func = v8impl::V8LocalFunctionFromJsValue (func);
1439
+ v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue (func);
1440
+ RETURN_STATUS_IF_FALSE (v8value->IsFunction (), napi_invalid_arg);
1441
+
1442
+ v8::Local<v8::Function> v8func = v8value.As <v8::Function>();
1462
1443
auto maybe = v8func->Call (context, v8recv, argc, args.data ());
1463
1444
1464
1445
if (try_catch.HasCaught ()) {
@@ -2007,13 +1988,15 @@ napi_status napi_new_instance(napi_env env,
2007
1988
v8::Isolate* isolate = v8impl::V8IsolateFromJsEnv (env);
2008
1989
v8::Local<v8::Context> context = isolate->GetCurrentContext ();
2009
1990
2010
- std::vector<v8::Handle <v8::Value>> args (argc);
1991
+ std::vector<v8::Local <v8::Value>> args (argc);
2011
1992
for (size_t i = 0 ; i < argc; i++) {
2012
1993
args[i] = v8impl::V8LocalValueFromJsValue (argv[i]);
2013
1994
}
2014
1995
2015
- v8::Local<v8::Function> ctor =
2016
- v8impl::V8LocalFunctionFromJsValue (constructor);
1996
+ v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue (constructor);
1997
+ RETURN_STATUS_IF_FALSE (v8value->IsFunction (), napi_invalid_arg);
1998
+
1999
+ v8::Local<v8::Function> ctor = v8value.As <v8::Function>();
2017
2000
2018
2001
auto maybe = ctor->NewInstance (context, argc, args.data ());
2019
2002
CHECK_MAYBE_EMPTY (maybe, napi_generic_failure);
@@ -2094,7 +2077,7 @@ napi_status napi_make_callback(napi_env env,
2094
2077
v8impl::V8LocalValueFromJsValue (recv).As <v8::Object>();
2095
2078
v8::Local<v8::Function> v8func =
2096
2079
v8impl::V8LocalValueFromJsValue (func).As <v8::Function>();
2097
- std::vector<v8::Handle <v8::Value>> args (argc);
2080
+ std::vector<v8::Local <v8::Value>> args (argc);
2098
2081
for (size_t i = 0 ; i < argc; i++) {
2099
2082
args[i] = v8impl::V8LocalValueFromJsValue (argv[i]);
2100
2083
}
0 commit comments