Skip to content

Commit 20177b9

Browse files
addaleaxBethGriggs
authored andcommittedFeb 25, 2020
n-api: turn NAPI_CALL_INTO_MODULE into a function
These do not need to be macros. PR-URL: #26128 Backport-PR-URL: #30537 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 3f9cec3 commit 20177b9

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed
 

‎src/node_api.cc

+37-31
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,26 @@ struct napi_env__ {
158158
(out) = v8::type::New((buffer), (byte_offset), (length)); \
159159
} while (0)
160160

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+
}
175174

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+
}
178181

179182
namespace {
180183
namespace v8impl {
@@ -354,11 +357,12 @@ class Finalizer {
354357
static void FinalizeBufferCallback(char* data, void* hint) {
355358
Finalizer* finalizer = static_cast<Finalizer*>(hint);
356359
if (finalizer->_finalize_callback != nullptr) {
357-
NAPI_CALL_INTO_MODULE_THROW(finalizer->_env,
360+
NapiCallIntoModuleThrow(finalizer->_env, [&]() {
358361
finalizer->_finalize_callback(
359362
finalizer->_env,
360363
data,
361-
finalizer->_finalize_hint));
364+
finalizer->_finalize_hint);
365+
});
362366
}
363367

364368
Delete(finalizer);
@@ -493,11 +497,12 @@ class Reference : private Finalizer {
493497
napi_env env = reference->_env;
494498

495499
if (reference->_finalize_callback != nullptr) {
496-
NAPI_CALL_INTO_MODULE_THROW(env,
500+
NapiCallIntoModuleThrow(env, [&]() {
497501
reference->_finalize_callback(
498502
reference->_env,
499503
reference->_finalize_data,
500-
reference->_finalize_hint));
504+
reference->_finalize_hint);
505+
});
501506
}
502507

503508
// this is safe because if a request to delete the reference
@@ -612,7 +617,7 @@ class CallbackWrapperBase : public CallbackWrapper {
612617
napi_callback cb = _bundle->*FunctionField;
613618

614619
napi_value result;
615-
NAPI_CALL_INTO_MODULE_THROW(env, result = cb(env, cbinfo_wrapper));
620+
NapiCallIntoModuleThrow(env, [&]() { result = cb(env, cbinfo_wrapper); });
616621

617622
if (result != nullptr) {
618623
this->SetReturnValue(result);
@@ -1309,8 +1314,9 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
13091314
napi_env env = v8impl::GetEnv(context);
13101315

13111316
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+
});
13141320

13151321
// If register function returned a non-null exports object different from
13161322
// 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 {
39413947
// stored.
39423948
napi_env env = _env;
39433949

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+
});
39523958

39533959
// Note: Don't access `work` after this point because it was
39543960
// likely deleted by the complete callback.

0 commit comments

Comments
 (0)
Please sign in to comment.