n-api: turn NAPI_CALL_INTO_MODULE into a function

These do not need to be macros.

PR-URL: https://github.com/nodejs/node/pull/26128
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Anna Henningsen 2019-02-15 11:57:19 +01:00
parent 441ef4d7f0
commit d2f652f12e
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
3 changed files with 37 additions and 31 deletions

View File

@ -303,11 +303,12 @@ class Reference : private Finalizer {
napi_env env = reference->_env; napi_env env = reference->_env;
if (reference->_finalize_callback != nullptr) { if (reference->_finalize_callback != nullptr) {
NAPI_CALL_INTO_MODULE_THROW(env, NapiCallIntoModuleThrow(env, [&]() {
reference->_finalize_callback( reference->_finalize_callback(
reference->_env, reference->_env,
reference->_finalize_data, reference->_finalize_data,
reference->_finalize_hint)); reference->_finalize_hint);
});
} }
// this is safe because if a request to delete the reference // this is safe because if a request to delete the reference
@ -448,7 +449,7 @@ class CallbackWrapperBase : public CallbackWrapper {
napi_callback cb = _bundle->*FunctionField; napi_callback cb = _bundle->*FunctionField;
napi_value result; napi_value result;
NAPI_CALL_INTO_MODULE_THROW(env, result = cb(env, cbinfo_wrapper)); NapiCallIntoModuleThrow(env, [&]() { result = cb(env, cbinfo_wrapper); });
if (result != nullptr) { if (result != nullptr) {
this->SetReturnValue(result); this->SetReturnValue(result);

View File

@ -113,23 +113,26 @@ napi_status napi_set_last_error(napi_env env, napi_status error_code,
} \ } \
} while (0) } while (0)
#define NAPI_CALL_INTO_MODULE(env, call, handle_exception) \ template <typename T, typename U>
do { \ void NapiCallIntoModule(napi_env env, T&& call, U&& handle_exception) {
int open_handle_scopes = (env)->open_handle_scopes; \ int open_handle_scopes = env->open_handle_scopes;
int open_callback_scopes = (env)->open_callback_scopes; \ int open_callback_scopes = env->open_callback_scopes;
napi_clear_last_error((env)); \ napi_clear_last_error(env);
call; \ call();
CHECK_EQ((env)->open_handle_scopes, open_handle_scopes); \ CHECK_EQ(env->open_handle_scopes, open_handle_scopes);
CHECK_EQ((env)->open_callback_scopes, open_callback_scopes); \ CHECK_EQ(env->open_callback_scopes, open_callback_scopes);
if (!(env)->last_exception.IsEmpty()) { \ if (!env->last_exception.IsEmpty()) {
handle_exception( \ handle_exception(env->last_exception.Get(env->isolate));
v8::Local<v8::Value>::New((env)->isolate, (env)->last_exception)); \ env->last_exception.Reset();
(env)->last_exception.Reset(); \ }
} \ }
} while (0)
#define NAPI_CALL_INTO_MODULE_THROW(env, call) \ template <typename T>
NAPI_CALL_INTO_MODULE((env), call, (env)->isolate->ThrowException) void NapiCallIntoModuleThrow(napi_env env, T&& call) {
NapiCallIntoModule(env, call, [&](v8::Local<v8::Value> value) {
env->isolate->ThrowException(value);
});
}
namespace v8impl { namespace v8impl {

View File

@ -34,11 +34,12 @@ class BufferFinalizer: private Finalizer {
static void FinalizeBufferCallback(char* data, void* hint) { static void FinalizeBufferCallback(char* data, void* hint) {
BufferFinalizer* finalizer = static_cast<BufferFinalizer*>(hint); BufferFinalizer* finalizer = static_cast<BufferFinalizer*>(hint);
if (finalizer->_finalize_callback != nullptr) { if (finalizer->_finalize_callback != nullptr) {
NAPI_CALL_INTO_MODULE_THROW(finalizer->_env, NapiCallIntoModuleThrow(finalizer->_env, [&]() {
finalizer->_finalize_callback( finalizer->_finalize_callback(
finalizer->_env, finalizer->_env,
data, data,
finalizer->_finalize_hint)); finalizer->_finalize_hint);
});
} }
Delete(finalizer); Delete(finalizer);
@ -465,8 +466,9 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
napi_env env = v8impl::GetEnv(context); napi_env env = v8impl::GetEnv(context);
napi_value _exports; napi_value _exports;
NAPI_CALL_INTO_MODULE_THROW(env, NapiCallIntoModuleThrow(env, [&]() {
_exports = init(env, v8impl::JsValueFromV8LocalValue(exports))); _exports = init(env, v8impl::JsValueFromV8LocalValue(exports));
});
// If register function returned a non-null exports object different from // If register function returned a non-null exports object different from
// the exports object we passed it, set that as the "exports" property of // 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 {
// stored. // stored.
napi_env env = _env; napi_env env = _env;
NAPI_CALL_INTO_MODULE(env, NapiCallIntoModule(env, [&]() {
_complete(_env, ConvertUVErrorCode(status), _data), _complete(_env, ConvertUVErrorCode(status), _data);
[env] (v8::Local<v8::Value> local_err) { }, [env](v8::Local<v8::Value> local_err) {
// If there was an unhandled exception in the complete callback, // If there was an unhandled exception in the complete callback,
// report it as a fatal exception. (There is no JavaScript on the // report it as a fatal exception. (There is no JavaScript on the
// callstack that can possibly handle it.) // callstack that can possibly handle it.)
v8impl::trigger_fatal_exception(env, local_err); v8impl::trigger_fatal_exception(env, local_err);
}); });
// Note: Don't access `work` after this point because it was // Note: Don't access `work` after this point because it was
// likely deleted by the complete callback. // likely deleted by the complete callback.