Skip to content

Commit

Permalink
n-api: remove napi_env::CallIntoModuleThrow
Browse files Browse the repository at this point in the history
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: #33570
Signed-off-by: Gabriel Schulhof <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
Gabriel Schulhof committed May 28, 2020
1 parent fb33b67 commit 6a1df3b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
});

Expand Down
16 changes: 7 additions & 9 deletions src/js_native_api_v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,13 @@ struct napi_env__ {
return v8::Just(true);
}

template <typename T, typename U>
void CallIntoModule(T&& call, U&& handle_exception) {
static inline void
HandleThrow(napi_env env, v8::Local<v8::Value> value) {
env->isolate->ThrowException(value);
}

template <typename T, typename U = decltype(HandleThrow)>
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);
Expand All @@ -96,13 +101,6 @@ struct napi_env__ {
}
}

template <typename T>
void CallIntoModuleThrow(T&& call) {
CallIntoModule(call, [&](napi_env env, v8::Local<v8::Value> value) {
env->isolate->ThrowException(value);
});
}

v8impl::Persistent<v8::Value> last_exception;

// We store references in two different lists, depending on whether they have
Expand Down
8 changes: 4 additions & 4 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -308,7 +308,7 @@ class ThreadSafeFunction : public node::AsyncResource {
v8::Local<v8::Function>::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);
});
}
Expand All @@ -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);
});
}
Expand Down Expand Up @@ -455,7 +455,7 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> 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));
});

Expand Down

0 comments on commit 6a1df3b

Please sign in to comment.