Skip to content

Commit 2335bcd

Browse files
addaleaxrvagg
authored andcommitted
n-api: turn NAPI_CALL_INTO_MODULE into a function
These do not need to be macros. PR-URL: #26128 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 1ce5e63 commit 2335bcd

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

src/js_native_api_v8.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,12 @@ class Reference : private Finalizer {
303303
napi_env env = reference->_env;
304304

305305
if (reference->_finalize_callback != nullptr) {
306-
NAPI_CALL_INTO_MODULE_THROW(env,
306+
NapiCallIntoModuleThrow(env, [&]() {
307307
reference->_finalize_callback(
308308
reference->_env,
309309
reference->_finalize_data,
310-
reference->_finalize_hint));
310+
reference->_finalize_hint);
311+
});
311312
}
312313

313314
// this is safe because if a request to delete the reference
@@ -448,7 +449,7 @@ class CallbackWrapperBase : public CallbackWrapper {
448449
napi_callback cb = _bundle->*FunctionField;
449450

450451
napi_value result;
451-
NAPI_CALL_INTO_MODULE_THROW(env, result = cb(env, cbinfo_wrapper));
452+
NapiCallIntoModuleThrow(env, [&]() { result = cb(env, cbinfo_wrapper); });
452453

453454
if (result != nullptr) {
454455
this->SetReturnValue(result);

src/js_native_api_v8.h

+19-16
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,26 @@ napi_status napi_set_last_error(napi_env env, napi_status error_code,
113113
} \
114114
} while (0)
115115

116-
#define NAPI_CALL_INTO_MODULE(env, call, handle_exception) \
117-
do { \
118-
int open_handle_scopes = (env)->open_handle_scopes; \
119-
int open_callback_scopes = (env)->open_callback_scopes; \
120-
napi_clear_last_error((env)); \
121-
call; \
122-
CHECK_EQ((env)->open_handle_scopes, open_handle_scopes); \
123-
CHECK_EQ((env)->open_callback_scopes, open_callback_scopes); \
124-
if (!(env)->last_exception.IsEmpty()) { \
125-
handle_exception( \
126-
v8::Local<v8::Value>::New((env)->isolate, (env)->last_exception)); \
127-
(env)->last_exception.Reset(); \
128-
} \
129-
} while (0)
116+
template <typename T, typename U>
117+
void NapiCallIntoModule(napi_env env, T&& call, U&& handle_exception) {
118+
int open_handle_scopes = env->open_handle_scopes;
119+
int open_callback_scopes = env->open_callback_scopes;
120+
napi_clear_last_error(env);
121+
call();
122+
CHECK_EQ(env->open_handle_scopes, open_handle_scopes);
123+
CHECK_EQ(env->open_callback_scopes, open_callback_scopes);
124+
if (!env->last_exception.IsEmpty()) {
125+
handle_exception(env->last_exception.Get(env->isolate));
126+
env->last_exception.Reset();
127+
}
128+
}
130129

131-
#define NAPI_CALL_INTO_MODULE_THROW(env, call) \
132-
NAPI_CALL_INTO_MODULE((env), call, (env)->isolate->ThrowException)
130+
template <typename T>
131+
void NapiCallIntoModuleThrow(napi_env env, T&& call) {
132+
NapiCallIntoModule(env, call, [&](v8::Local<v8::Value> value) {
133+
env->isolate->ThrowException(value);
134+
});
135+
}
133136

134137
namespace v8impl {
135138

src/node_api.cc

+14-12
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ class BufferFinalizer: private Finalizer {
3434
static void FinalizeBufferCallback(char* data, void* hint) {
3535
BufferFinalizer* finalizer = static_cast<BufferFinalizer*>(hint);
3636
if (finalizer->_finalize_callback != nullptr) {
37-
NAPI_CALL_INTO_MODULE_THROW(finalizer->_env,
37+
NapiCallIntoModuleThrow(finalizer->_env, [&]() {
3838
finalizer->_finalize_callback(
3939
finalizer->_env,
4040
data,
41-
finalizer->_finalize_hint));
41+
finalizer->_finalize_hint);
42+
});
4243
}
4344

4445
Delete(finalizer);
@@ -465,8 +466,9 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
465466
napi_env env = v8impl::GetEnv(context);
466467

467468
napi_value _exports;
468-
NAPI_CALL_INTO_MODULE_THROW(env,
469-
_exports = init(env, v8impl::JsValueFromV8LocalValue(exports)));
469+
NapiCallIntoModuleThrow(env, [&]() {
470+
_exports = init(env, v8impl::JsValueFromV8LocalValue(exports));
471+
});
470472

471473
// If register function returned a non-null exports object different from
472474
// the exports object we passed it, set that as the "exports" property of
@@ -874,14 +876,14 @@ class Work : public node::AsyncResource, public node::ThreadPoolWork {
874876
// stored.
875877
napi_env env = _env;
876878

877-
NAPI_CALL_INTO_MODULE(env,
878-
_complete(_env, ConvertUVErrorCode(status), _data),
879-
[env] (v8::Local<v8::Value> local_err) {
880-
// If there was an unhandled exception in the complete callback,
881-
// report it as a fatal exception. (There is no JavaScript on the
882-
// callstack that can possibly handle it.)
883-
v8impl::trigger_fatal_exception(env, local_err);
884-
});
879+
NapiCallIntoModule(env, [&]() {
880+
_complete(_env, ConvertUVErrorCode(status), _data);
881+
}, [env](v8::Local<v8::Value> local_err) {
882+
// If there was an unhandled exception in the complete callback,
883+
// report it as a fatal exception. (There is no JavaScript on the
884+
// callstack that can possibly handle it.)
885+
v8impl::trigger_fatal_exception(env, local_err);
886+
});
885887

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

0 commit comments

Comments
 (0)