From 6a1df3b5afc92f438cf84621abd8cda79d9b1b17 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Tue, 26 May 2020 08:32:26 -0700 Subject: [PATCH] n-api: remove `napi_env::CallIntoModuleThrow` Give `napi_env::CallIntoModule` the thrower used by `CallIntoModuleThrow` as its default second argument. That way we do not need two different methods on `napi_env` for calling into the addon. PR-URL: https://github.com/nodejs/node/pull/33570 Signed-off-by: Gabriel Schulhof Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Chengzhong Wu Reviewed-By: Michael Dawson --- src/js_native_api_v8.cc | 4 ++-- src/js_native_api_v8.h | 16 +++++++--------- src/node_api.cc | 8 ++++---- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index a09969b9adf28f..db53a104d6b8ef 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -267,7 +267,7 @@ class RefBase : protected Finalizer, RefTracker { protected: inline void Finalize(bool is_env_teardown = false) override { if (_finalize_callback != nullptr) { - _env->CallIntoModuleThrow([&](napi_env env) { + _env->CallIntoModule([&](napi_env env) { _finalize_callback( env, _finalize_data, @@ -475,7 +475,7 @@ class CallbackWrapperBase : public CallbackWrapper { napi_callback cb = _bundle->*FunctionField; napi_value result; - env->CallIntoModuleThrow([&](napi_env env) { + env->CallIntoModule([&](napi_env env) { result = cb(env, cbinfo_wrapper); }); diff --git a/src/js_native_api_v8.h b/src/js_native_api_v8.h index 32fc16f155a7fe..9c737f3c9cc9fc 100644 --- a/src/js_native_api_v8.h +++ b/src/js_native_api_v8.h @@ -82,8 +82,13 @@ struct napi_env__ { return v8::Just(true); } - template - void CallIntoModule(T&& call, U&& handle_exception) { + static inline void + HandleThrow(napi_env env, v8::Local value) { + env->isolate->ThrowException(value); + } + + template + inline void CallIntoModule(T&& call, U&& handle_exception = HandleThrow) { int open_handle_scopes_before = open_handle_scopes; int open_callback_scopes_before = open_callback_scopes; napi_clear_last_error(this); @@ -96,13 +101,6 @@ struct napi_env__ { } } - template - void CallIntoModuleThrow(T&& call) { - CallIntoModule(call, [&](napi_env env, v8::Local value) { - env->isolate->ThrowException(value); - }); - } - v8impl::Persistent last_exception; // We store references in two different lists, depending on whether they have diff --git a/src/node_api.cc b/src/node_api.cc index b87690752b3d9a..fe24eca1b8e2d8 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -57,7 +57,7 @@ class BufferFinalizer : private Finalizer { v8::HandleScope handle_scope(finalizer->_env->isolate); v8::Context::Scope context_scope(finalizer->_env->context()); - finalizer->_env->CallIntoModuleThrow([&](napi_env env) { + finalizer->_env->CallIntoModule([&](napi_env env) { finalizer->_finalize_callback( env, finalizer->_finalize_data, @@ -308,7 +308,7 @@ class ThreadSafeFunction : public node::AsyncResource { v8::Local::New(env->isolate, ref); js_callback = v8impl::JsValueFromV8LocalValue(js_cb); } - env->CallIntoModuleThrow([&](napi_env env) { + env->CallIntoModule([&](napi_env env) { call_js_cb(env, js_callback, context, data); }); } @@ -318,7 +318,7 @@ class ThreadSafeFunction : public node::AsyncResource { v8::HandleScope scope(env->isolate); if (finalize_cb) { CallbackScope cb_scope(this); - env->CallIntoModuleThrow([&](napi_env env) { + env->CallIntoModule([&](napi_env env) { finalize_cb(env, finalize_data, context); }); } @@ -455,7 +455,7 @@ void napi_module_register_by_symbol(v8::Local exports, napi_env env = v8impl::NewEnv(context); napi_value _exports; - env->CallIntoModuleThrow([&](napi_env env) { + env->CallIntoModule([&](napi_env env) { _exports = init(env, v8impl::JsValueFromV8LocalValue(exports)); });