diff options
author | Vladimir Morozov <vmorozov@microsoft.com> | 2022-03-19 01:59:02 +0300 |
---|---|---|
committer | Michael Dawson <mdawson@devrus.com> | 2022-03-21 20:24:16 +0300 |
commit | 718be08686312d19645861b9715491a49ee3c9cb (patch) | |
tree | 1a0dca89f88f5141f44092df90556f585253b85e /src/node_api.cc | |
parent | 655926155647ad39236220b094c7d1f391ce02ed (diff) |
node-api: format Node-API related code
PR-URL: https://github.com/nodejs/node/pull/42396
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Diffstat (limited to 'src/node_api.cc')
-rw-r--r-- | src/node_api.cc | 319 |
1 files changed, 147 insertions, 172 deletions
diff --git a/src/node_api.cc b/src/node_api.cc index 60fbe96b8ef..aa4f8a6a240 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -64,18 +64,16 @@ class BufferFinalizer : private Finalizer { static_cast<node_napi_env>(finalizer->_env)->node_env(); node_env->SetImmediate( [finalizer = std::move(finalizer)](node::Environment* env) { - if (finalizer->_finalize_callback == nullptr) return; + if (finalizer->_finalize_callback == nullptr) return; - v8::HandleScope handle_scope(finalizer->_env->isolate); - v8::Context::Scope context_scope(finalizer->_env->context()); + v8::HandleScope handle_scope(finalizer->_env->isolate); + v8::Context::Scope context_scope(finalizer->_env->context()); - finalizer->_env->CallIntoModule([&](napi_env env) { - finalizer->_finalize_callback( - env, - finalizer->_finalize_data, - finalizer->_finalize_hint); - }); - }); + finalizer->_env->CallIntoModule([&](napi_env env) { + finalizer->_finalize_callback( + env, finalizer->_finalize_data, finalizer->_finalize_hint); + }); + }); } struct Deleter { @@ -85,8 +83,8 @@ class BufferFinalizer : private Finalizer { }; }; -static inline napi_env -NewEnv(v8::Local<v8::Context> context, const std::string& module_filename) { +static inline napi_env NewEnv(v8::Local<v8::Context> context, + const std::string& module_filename) { node_napi_env result; result = new node_napi_env__(context, module_filename); @@ -98,18 +96,16 @@ NewEnv(v8::Local<v8::Context> context, const std::string& module_filename) { // once all N-API addons using this napi_env are unloaded. // For now, a per-Environment cleanup hook is the best we can do. result->node_env()->AddCleanupHook( - [](void* arg) { - static_cast<napi_env>(arg)->Unref(); - }, + [](void* arg) { static_cast<napi_env>(arg)->Unref(); }, static_cast<void*>(result)); return result; } -static inline void trigger_fatal_exception( - napi_env env, v8::Local<v8::Value> local_err) { +static inline void trigger_fatal_exception(napi_env env, + v8::Local<v8::Value> local_err) { v8::Local<v8::Message> local_msg = - v8::Exception::CreateMessage(env->isolate, local_err); + v8::Exception::CreateMessage(env->isolate, local_err); node::errors::TriggerUncaughtException(env->isolate, local_err, local_msg); } @@ -124,20 +120,20 @@ class ThreadSafeFunction : public node::AsyncResource { node_napi_env env_, void* finalize_data_, napi_finalize finalize_cb_, - napi_threadsafe_function_call_js call_js_cb_): - AsyncResource(env_->isolate, - resource, - *v8::String::Utf8Value(env_->isolate, name)), - thread_count(thread_count_), - is_closing(false), - dispatch_state(kDispatchIdle), - context(context_), - max_queue_size(max_queue_size_), - env(env_), - finalize_data(finalize_data_), - finalize_cb(finalize_cb_), - call_js_cb(call_js_cb_ == nullptr ? CallJs : call_js_cb_), - handles_closing(false) { + napi_threadsafe_function_call_js call_js_cb_) + : AsyncResource(env_->isolate, + resource, + *v8::String::Utf8Value(env_->isolate, name)), + thread_count(thread_count_), + is_closing(false), + dispatch_state(kDispatchIdle), + context(context_), + max_queue_size(max_queue_size_), + env(env_), + finalize_data(finalize_data_), + finalize_cb(finalize_cb_), + call_js_cb(call_js_cb_ == nullptr ? CallJs : call_js_cb_), + handles_closing(false) { ref.Reset(env->isolate, func); node::AddEnvironmentCleanupHook(env->isolate, Cleanup, this); env->Ref(); @@ -153,9 +149,8 @@ class ThreadSafeFunction : public node::AsyncResource { napi_status Push(void* data, napi_threadsafe_function_call_mode mode) { node::Mutex::ScopedLock lock(this->mutex); - while (queue.size() >= max_queue_size && - max_queue_size > 0 && - !is_closing) { + while (queue.size() >= max_queue_size && max_queue_size > 0 && + !is_closing) { if (mode == napi_tsfn_nonblocking) { return napi_queue_full; } @@ -211,7 +206,7 @@ class ThreadSafeFunction : public node::AsyncResource { } void EmptyQueueAndDelete() { - for (; !queue.empty() ; queue.pop()) { + for (; !queue.empty(); queue.pop()) { call_js_cb(nullptr, nullptr, context, queue.front()); } delete this; @@ -262,9 +257,7 @@ class ThreadSafeFunction : public node::AsyncResource { return napi_ok; } - inline void* Context() { - return context; - } + inline void* Context() { return context; } protected: void Dispatch() { @@ -329,12 +322,11 @@ class ThreadSafeFunction : public node::AsyncResource { napi_value js_callback = nullptr; if (!ref.IsEmpty()) { v8::Local<v8::Function> js_cb = - v8::Local<v8::Function>::New(env->isolate, ref); + v8::Local<v8::Function>::New(env->isolate, ref); js_callback = v8impl::JsValueFromV8LocalValue(js_cb); } - env->CallIntoModule([&](napi_env env) { - call_js_cb(env, js_callback, context, data); - }); + env->CallIntoModule( + [&](napi_env env) { call_js_cb(env, js_callback, context, data); }); } return has_more; @@ -344,9 +336,8 @@ class ThreadSafeFunction : public node::AsyncResource { v8::HandleScope scope(env->isolate); if (finalize_cb) { CallbackScope cb_scope(this); - env->CallIntoModule([&](napi_env env) { - finalize_cb(env, finalize_data, context); - }); + env->CallIntoModule( + [&](napi_env env) { finalize_cb(env, finalize_data, context); }); } EmptyQueueAndDelete(); } @@ -393,15 +384,16 @@ class ThreadSafeFunction : public node::AsyncResource { status = napi_get_undefined(env, &recv); if (status != napi_ok) { - napi_throw_error(env, "ERR_NAPI_TSFN_GET_UNDEFINED", - "Failed to retrieve undefined value"); + napi_throw_error(env, + "ERR_NAPI_TSFN_GET_UNDEFINED", + "Failed to retrieve undefined value"); return; } status = napi_call_function(env, recv, cb, 0, nullptr, nullptr); if (status != napi_ok && status != napi_pending_exception) { - napi_throw_error(env, "ERR_NAPI_TSFN_CALL_JS", - "Failed to call JS callback"); + napi_throw_error( + env, "ERR_NAPI_TSFN_CALL_JS", "Failed to call JS callback"); return; } } @@ -414,8 +406,8 @@ class ThreadSafeFunction : public node::AsyncResource { } static void Cleanup(void* data) { - reinterpret_cast<ThreadSafeFunction*>(data) - ->CloseHandlesAndMaybeDelete(true); + reinterpret_cast<ThreadSafeFunction*>(data)->CloseHandlesAndMaybeDelete( + true); } private: @@ -564,7 +556,10 @@ static void napi_module_register_cb(v8::Local<v8::Object> exports, v8::Local<v8::Value> module, v8::Local<v8::Context> context, void* priv) { - napi_module_register_by_symbol(exports, module, context, + napi_module_register_by_symbol( + exports, + module, + context, static_cast<const napi_module*>(priv)->nm_register_func); } @@ -576,8 +571,7 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports, std::string module_filename = ""; if (init == nullptr) { CHECK_NOT_NULL(node_env); - node_env->ThrowError( - "Module has no declared entry point."); + node_env->ThrowError("Module has no declared entry point."); return; } @@ -620,23 +614,23 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports, namespace node { node_module napi_module_to_node_module(const napi_module* mod) { return { - -1, - mod->nm_flags | NM_F_DELETEME, - nullptr, - mod->nm_filename, - nullptr, - napi_module_register_cb, - mod->nm_modname, - const_cast<napi_module*>(mod), // priv - nullptr, + -1, + mod->nm_flags | NM_F_DELETEME, + nullptr, + mod->nm_filename, + nullptr, + napi_module_register_cb, + mod->nm_modname, + const_cast<napi_module*>(mod), // priv + nullptr, }; } } // namespace node // Registers a NAPI module. void napi_module_register(napi_module* mod) { - node::node_module* nm = new node::node_module( - node::napi_module_to_node_module(mod)); + node::node_module* nm = + new node::node_module(node::napi_module_to_node_module(mod)); node::node_module_register(nm); } @@ -665,23 +659,20 @@ napi_status napi_remove_env_cleanup_hook(napi_env env, struct napi_async_cleanup_hook_handle__ { napi_async_cleanup_hook_handle__(napi_env env, napi_async_cleanup_hook user_hook, - void* user_data): - env_(env), - user_hook_(user_hook), - user_data_(user_data) { + void* user_data) + : env_(env), user_hook_(user_hook), user_data_(user_data) { handle_ = node::AddEnvironmentCleanupHook(env->isolate, Hook, this); env->Ref(); } ~napi_async_cleanup_hook_handle__() { node::RemoveEnvironmentCleanupHook(std::move(handle_)); - if (done_cb_ != nullptr) - done_cb_(done_data_); + if (done_cb_ != nullptr) done_cb_(done_data_); // Release the `env` handle asynchronously since it would be surprising if // a call to a N-API function would destroy `env` synchronously. - static_cast<node_napi_env>(env_)->node_env() - ->SetImmediate([env = env_](node::Environment*) { env->Unref(); }); + static_cast<node_napi_env>(env_)->node_env()->SetImmediate( + [env = env_](node::Environment*) { env->Unref(); }); } static void Hook(void* data, void (*done_cb)(void*), void* done_data) { @@ -709,19 +700,16 @@ napi_status napi_add_async_cleanup_hook( CHECK_ARG(env, hook); napi_async_cleanup_hook_handle__* handle = - new napi_async_cleanup_hook_handle__(env, hook, arg); + new napi_async_cleanup_hook_handle__(env, hook, arg); - if (remove_handle != nullptr) - *remove_handle = handle; + if (remove_handle != nullptr) *remove_handle = handle; return napi_clear_last_error(env); } napi_status napi_remove_async_cleanup_hook( napi_async_cleanup_hook_handle remove_handle) { - - if (remove_handle == nullptr) - return napi_invalid_arg; + if (remove_handle == nullptr) return napi_invalid_arg; delete remove_handle; @@ -746,19 +734,15 @@ NAPI_NO_RETURN void napi_fatal_error(const char* location, std::string message_string; if (location_len != NAPI_AUTO_LENGTH) { - location_string.assign( - const_cast<char*>(location), location_len); + location_string.assign(const_cast<char*>(location), location_len); } else { - location_string.assign( - const_cast<char*>(location), strlen(location)); + location_string.assign(const_cast<char*>(location), strlen(location)); } if (message_len != NAPI_AUTO_LENGTH) { - message_string.assign( - const_cast<char*>(message), message_len); + message_string.assign(const_cast<char*>(message), message_len); } else { - message_string.assign( - const_cast<char*>(message), strlen(message)); + message_string.assign(const_cast<char*>(message), strlen(message)); } node::FatalError(location_string.c_str(), message_string.c_str()); @@ -831,8 +815,7 @@ napi_status napi_async_init(napi_env env, return napi_clear_last_error(env); } -napi_status napi_async_destroy(napi_env env, - napi_async_context async_context) { +napi_status napi_async_destroy(napi_env env, napi_async_context async_context) { CHECK_ENV(env); CHECK_ARG(env, async_context); @@ -890,8 +873,8 @@ napi_status napi_make_callback(napi_env env, } else { CHECK_MAYBE_EMPTY(env, callback_result, napi_generic_failure); if (result != nullptr) { - *result = v8impl::JsValueFromV8LocalValue( - callback_result.ToLocalChecked()); + *result = + v8impl::JsValueFromV8LocalValue(callback_result.ToLocalChecked()); } } @@ -932,16 +915,19 @@ napi_status napi_create_external_buffer(napi_env env, v8::Isolate* isolate = env->isolate; // The finalizer object will delete itself after invoking the callback. - v8impl::Finalizer* finalizer = v8impl::Finalizer::New( - env, finalize_cb, nullptr, finalize_hint, - v8impl::Finalizer::kKeepEnvReference); - - v8::MaybeLocal<v8::Object> maybe = node::Buffer::New( - isolate, - static_cast<char*>(data), - length, - v8impl::BufferFinalizer::FinalizeBufferCallback, - finalizer); + v8impl::Finalizer* finalizer = + v8impl::Finalizer::New(env, + finalize_cb, + nullptr, + finalize_hint, + v8impl::Finalizer::kKeepEnvReference); + + v8::MaybeLocal<v8::Object> maybe = + node::Buffer::New(isolate, + static_cast<char*>(data), + length, + v8impl::BufferFinalizer::FinalizeBufferCallback, + finalizer); CHECK_MAYBE_EMPTY(env, maybe, napi_generic_failure); @@ -961,9 +947,8 @@ napi_status napi_create_buffer_copy(napi_env env, NAPI_PREAMBLE(env); CHECK_ARG(env, result); - v8::MaybeLocal<v8::Object> maybe = node::Buffer::Copy( - env->isolate, - static_cast<const char*>(data), length); + v8::MaybeLocal<v8::Object> maybe = + node::Buffer::Copy(env->isolate, static_cast<const char*>(data), length); CHECK_MAYBE_EMPTY(env, maybe, napi_generic_failure); @@ -1010,11 +995,7 @@ napi_status napi_get_node_version(napi_env env, CHECK_ENV(env); CHECK_ARG(env, result); static const napi_node_version version = { - NODE_MAJOR_VERSION, - NODE_MINOR_VERSION, - NODE_PATCH_VERSION, - NODE_RELEASE - }; + NODE_MAJOR_VERSION, NODE_MINOR_VERSION, NODE_PATCH_VERSION, NODE_RELEASE}; *result = &version; return napi_clear_last_error(env); } @@ -1044,15 +1025,15 @@ class Work : public node::AsyncResource, public node::ThreadPoolWork { napi_async_execute_callback execute, napi_async_complete_callback complete = nullptr, void* data = nullptr) - : AsyncResource(env->isolate, - async_resource, - *v8::String::Utf8Value(env->isolate, async_resource_name)), - ThreadPoolWork(env->node_env()), - _env(env), - _data(data), - _execute(execute), - _complete(complete) { - } + : AsyncResource( + env->isolate, + async_resource, + *v8::String::Utf8Value(env->isolate, async_resource_name)), + ThreadPoolWork(env->node_env()), + _env(env), + _data(data), + _execute(execute), + _complete(complete) {} ~Work() override = default; @@ -1063,21 +1044,16 @@ class Work : public node::AsyncResource, public node::ThreadPoolWork { napi_async_execute_callback execute, napi_async_complete_callback complete, void* data) { - return new Work(env, async_resource, async_resource_name, - execute, complete, data); + return new Work( + env, async_resource, async_resource_name, execute, complete, data); } - static void Delete(Work* work) { - delete work; - } + static void Delete(Work* work) { delete work; } - void DoThreadPoolWork() override { - _execute(_env, _data); - } + void DoThreadPoolWork() override { _execute(_env, _data); } void AfterThreadPoolWork(int status) override { - if (_complete == nullptr) - return; + if (_complete == nullptr) return; // Establish a handle scope here so that every callback doesn't have to. // Also it is needed for the exception-handling below. @@ -1085,14 +1061,16 @@ class Work : public node::AsyncResource, public node::ThreadPoolWork { CallbackScope callback_scope(this); - _env->CallIntoModule([&](napi_env env) { - _complete(env, ConvertUVErrorCode(status), _data); - }, [](napi_env env, v8::Local<v8::Value> local_err) { - // If there was an unhandled exception in the complete callback, - // report it as a fatal exception. (There is no JavaScript on the - // callstack that can possibly handle it.) - v8impl::trigger_fatal_exception(env, local_err); - }); + _env->CallIntoModule( + [&](napi_env env) { + _complete(env, ConvertUVErrorCode(status), _data); + }, + [](napi_env env, v8::Local<v8::Value> local_err) { + // If there was an unhandled exception in the complete callback, + // report it as a fatal exception. (There is no JavaScript on the + // callstack that can possibly handle it.) + v8impl::trigger_fatal_exception(env, local_err); + }); // Note: Don't access `work` after this point because it was // likely deleted by the complete callback. @@ -1108,13 +1086,13 @@ class Work : public node::AsyncResource, public node::ThreadPoolWork { } // end of namespace uvimpl } // end of anonymous namespace -#define CALL_UV(env, condition) \ - do { \ - int result = (condition); \ - napi_status status = uvimpl::ConvertUVErrorCode(result); \ - if (status != napi_ok) { \ - return napi_set_last_error(env, status, result); \ - } \ +#define CALL_UV(env, condition) \ + do { \ + int result = (condition); \ + napi_status status = uvimpl::ConvertUVErrorCode(result); \ + if (status != napi_ok) { \ + return napi_set_last_error(env, status, result); \ + } \ } while (0) napi_status napi_create_async_work(napi_env env, @@ -1193,18 +1171,18 @@ napi_status napi_cancel_async_work(napi_env env, napi_async_work work) { return napi_clear_last_error(env); } -napi_status -napi_create_threadsafe_function(napi_env env, - napi_value func, - napi_value async_resource, - napi_value async_resource_name, - size_t max_queue_size, - size_t initial_thread_count, - void* thread_finalize_data, - napi_finalize thread_finalize_cb, - void* context, - napi_threadsafe_function_call_js call_js_cb, - napi_threadsafe_function* result) { +napi_status napi_create_threadsafe_function( + napi_env env, + napi_value func, + napi_value async_resource, + napi_value async_resource_name, + size_t max_queue_size, + size_t initial_thread_count, + void* thread_finalize_data, + napi_finalize thread_finalize_cb, + void* context, + napi_threadsafe_function_call_js call_js_cb, + napi_threadsafe_function* result) { CHECK_ENV(env); CHECK_ARG(env, async_resource_name); RETURN_STATUS_IF_FALSE(env, initial_thread_count > 0, napi_invalid_arg); @@ -1256,9 +1234,8 @@ napi_create_threadsafe_function(napi_env env, return napi_set_last_error(env, status); } -napi_status -napi_get_threadsafe_function_context(napi_threadsafe_function func, - void** result) { +napi_status napi_get_threadsafe_function_context(napi_threadsafe_function func, + void** result) { CHECK_NOT_NULL(func); CHECK_NOT_NULL(result); @@ -1266,36 +1243,34 @@ napi_get_threadsafe_function_context(napi_threadsafe_function func, return napi_ok; } -napi_status -napi_call_threadsafe_function(napi_threadsafe_function func, - void* data, - napi_threadsafe_function_call_mode is_blocking) { +napi_status napi_call_threadsafe_function( + napi_threadsafe_function func, + void* data, + napi_threadsafe_function_call_mode is_blocking) { CHECK_NOT_NULL(func); return reinterpret_cast<v8impl::ThreadSafeFunction*>(func)->Push(data, is_blocking); } -napi_status -napi_acquire_threadsafe_function(napi_threadsafe_function func) { +napi_status napi_acquire_threadsafe_function(napi_threadsafe_function func) { CHECK_NOT_NULL(func); return reinterpret_cast<v8impl::ThreadSafeFunction*>(func)->Acquire(); } -napi_status -napi_release_threadsafe_function(napi_threadsafe_function func, - napi_threadsafe_function_release_mode mode) { +napi_status napi_release_threadsafe_function( + napi_threadsafe_function func, napi_threadsafe_function_release_mode mode) { CHECK_NOT_NULL(func); return reinterpret_cast<v8impl::ThreadSafeFunction*>(func)->Release(mode); } -napi_status -napi_unref_threadsafe_function(napi_env env, napi_threadsafe_function func) { +napi_status napi_unref_threadsafe_function(napi_env env, + napi_threadsafe_function func) { CHECK_NOT_NULL(func); return reinterpret_cast<v8impl::ThreadSafeFunction*>(func)->Unref(); } -napi_status -napi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func) { +napi_status napi_ref_threadsafe_function(napi_env env, + napi_threadsafe_function func) { CHECK_NOT_NULL(func); return reinterpret_cast<v8impl::ThreadSafeFunction*>(func)->Ref(); } |