@@ -158,23 +158,26 @@ struct napi_env__ {
158
158
(out) = v8::type::New ((buffer), (byte_offset), (length)); \
159
159
} while (0 )
160
160
161
- #define NAPI_CALL_INTO_MODULE (env, call, handle_exception ) \
162
- do { \
163
- int open_handle_scopes = (env)->open_handle_scopes ; \
164
- int open_callback_scopes = (env)->open_callback_scopes ; \
165
- napi_clear_last_error ((env)); \
166
- call; \
167
- CHECK_EQ ((env)->open_handle_scopes , open_handle_scopes); \
168
- CHECK_EQ ((env)->open_callback_scopes , open_callback_scopes); \
169
- if (!(env)->last_exception .IsEmpty ()) { \
170
- handle_exception ( \
171
- v8::Local<v8::Value>::New ((env)->isolate , (env)->last_exception )); \
172
- (env)->last_exception .Reset (); \
173
- } \
174
- } while (0 )
161
+ template <typename T, typename U>
162
+ void NapiCallIntoModule (napi_env env, T&& call, U&& handle_exception) {
163
+ int open_handle_scopes = env->open_handle_scopes ;
164
+ int open_callback_scopes = env->open_callback_scopes ;
165
+ napi_clear_last_error (env);
166
+ call ();
167
+ CHECK_EQ (env->open_handle_scopes , open_handle_scopes);
168
+ CHECK_EQ (env->open_callback_scopes , open_callback_scopes);
169
+ if (!env->last_exception .IsEmpty ()) {
170
+ handle_exception (env->last_exception .Get (env->isolate ));
171
+ env->last_exception .Reset ();
172
+ }
173
+ }
175
174
176
- #define NAPI_CALL_INTO_MODULE_THROW (env, call ) \
177
- NAPI_CALL_INTO_MODULE ((env), call, (env)->isolate->ThrowException)
175
+ template <typename T>
176
+ void NapiCallIntoModuleThrow (napi_env env, T&& call) {
177
+ NapiCallIntoModule (env, call, [&](v8::Local<v8::Value> value) {
178
+ env->isolate ->ThrowException (value);
179
+ });
180
+ }
178
181
179
182
namespace {
180
183
namespace v8impl {
@@ -354,11 +357,12 @@ class Finalizer {
354
357
static void FinalizeBufferCallback (char * data, void * hint) {
355
358
Finalizer* finalizer = static_cast <Finalizer*>(hint);
356
359
if (finalizer->_finalize_callback != nullptr ) {
357
- NAPI_CALL_INTO_MODULE_THROW (finalizer->_env ,
360
+ NapiCallIntoModuleThrow (finalizer->_env , [&]() {
358
361
finalizer->_finalize_callback (
359
362
finalizer->_env ,
360
363
data,
361
- finalizer->_finalize_hint ));
364
+ finalizer->_finalize_hint );
365
+ });
362
366
}
363
367
364
368
Delete (finalizer);
@@ -493,11 +497,12 @@ class Reference : private Finalizer {
493
497
napi_env env = reference->_env ;
494
498
495
499
if (reference->_finalize_callback != nullptr ) {
496
- NAPI_CALL_INTO_MODULE_THROW (env,
500
+ NapiCallIntoModuleThrow (env, [&]() {
497
501
reference->_finalize_callback (
498
502
reference->_env ,
499
503
reference->_finalize_data ,
500
- reference->_finalize_hint ));
504
+ reference->_finalize_hint );
505
+ });
501
506
}
502
507
503
508
// this is safe because if a request to delete the reference
@@ -612,7 +617,7 @@ class CallbackWrapperBase : public CallbackWrapper {
612
617
napi_callback cb = _bundle->*FunctionField;
613
618
614
619
napi_value result;
615
- NAPI_CALL_INTO_MODULE_THROW (env, result = cb (env, cbinfo_wrapper));
620
+ NapiCallIntoModuleThrow (env, [&]() { result = cb (env, cbinfo_wrapper); } );
616
621
617
622
if (result != nullptr ) {
618
623
this ->SetReturnValue (result);
@@ -1309,8 +1314,9 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
1309
1314
napi_env env = v8impl::GetEnv (context);
1310
1315
1311
1316
napi_value _exports;
1312
- NAPI_CALL_INTO_MODULE_THROW (env,
1313
- _exports = init (env, v8impl::JsValueFromV8LocalValue (exports)));
1317
+ NapiCallIntoModuleThrow (env, [&]() {
1318
+ _exports = init (env, v8impl::JsValueFromV8LocalValue (exports));
1319
+ });
1314
1320
1315
1321
// If register function returned a non-null exports object different from
1316
1322
// the exports object we passed it, set that as the "exports" property of
@@ -3941,14 +3947,14 @@ class Work : public node::AsyncResource, public node::ThreadPoolWork {
3941
3947
// stored.
3942
3948
napi_env env = _env;
3943
3949
3944
- NAPI_CALL_INTO_MODULE (env,
3945
- _complete (_env, ConvertUVErrorCode (status), _data),
3946
- [env] (v8::Local<v8::Value> local_err) {
3947
- // If there was an unhandled exception in the complete callback,
3948
- // report it as a fatal exception. (There is no JavaScript on the
3949
- // callstack that can possibly handle it.)
3950
- v8impl::trigger_fatal_exception (env, local_err);
3951
- });
3950
+ NapiCallIntoModule (env, [&]() {
3951
+ _complete (_env, ConvertUVErrorCode (status), _data);
3952
+ }, [env](v8::Local<v8::Value> local_err) {
3953
+ // If there was an unhandled exception in the complete callback,
3954
+ // report it as a fatal exception. (There is no JavaScript on the
3955
+ // callstack that can possibly handle it.)
3956
+ v8impl::trigger_fatal_exception (env, local_err);
3957
+ });
3952
3958
3953
3959
// Note: Don't access `work` after this point because it was
3954
3960
// likely deleted by the complete callback.
0 commit comments