Skip to content

node-api: convert NewEnv to node_napi_env__::New #57834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 48 additions & 45 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,52 @@
#include <cstring>
#include <memory>

namespace v8impl {
static void ThrowNodeApiVersionError(node::Environment* node_env,
const char* module_name,
int32_t module_api_version) {
std::string error_message;
error_message += module_name;
error_message += " requires Node-API version ";
error_message += std::to_string(module_api_version);
error_message += ", but this version of Node.js only supports version ";
error_message += NODE_STRINGIFY(NODE_API_SUPPORTED_VERSION_MAX) " add-ons.";
node_env->ThrowError(error_message.c_str());
}
} // namespace v8impl

/*static*/ napi_env node_napi_env__::New(v8::Local<v8::Context> context,
const std::string& module_filename,
int32_t module_api_version) {
node_napi_env result;

// Validate module_api_version.
if (module_api_version < NODE_API_DEFAULT_MODULE_API_VERSION) {
module_api_version = NODE_API_DEFAULT_MODULE_API_VERSION;
} else if (module_api_version > NODE_API_SUPPORTED_VERSION_MAX &&
module_api_version != NAPI_VERSION_EXPERIMENTAL) {
node::Environment* node_env = node::Environment::GetCurrent(context);
CHECK_NOT_NULL(node_env);
v8impl::ThrowNodeApiVersionError(
node_env, module_filename.c_str(), module_api_version);
return nullptr;
}

result = new node_napi_env__(context, module_filename, module_api_version);
// TODO(addaleax): There was previously code that tried to delete the
// napi_env when its v8::Context was garbage collected;
// However, as long as N-API addons using this napi_env are in place,
// the Context needs to be accessible and alive.
// Ideally, we'd want an on-addon-unload hook that takes care of this
// 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(); },
static_cast<void*>(result));

return result;
}

node_napi_env__::node_napi_env__(v8::Local<v8::Context> context,
const std::string& module_filename,
int32_t module_api_version)
Expand Down Expand Up @@ -152,50 +198,6 @@ class BufferFinalizer : private Finalizer {
~BufferFinalizer() { env()->Unref(); }
};

void ThrowNodeApiVersionError(node::Environment* node_env,
const char* module_name,
int32_t module_api_version) {
std::string error_message;
error_message += module_name;
error_message += " requires Node-API version ";
error_message += std::to_string(module_api_version);
error_message += ", but this version of Node.js only supports version ";
error_message += NODE_STRINGIFY(NODE_API_SUPPORTED_VERSION_MAX) " add-ons.";
node_env->ThrowError(error_message.c_str());
}

inline napi_env NewEnv(v8::Local<v8::Context> context,
const std::string& module_filename,
int32_t module_api_version) {
node_napi_env result;

// Validate module_api_version.
if (module_api_version < NODE_API_DEFAULT_MODULE_API_VERSION) {
module_api_version = NODE_API_DEFAULT_MODULE_API_VERSION;
} else if (module_api_version > NODE_API_SUPPORTED_VERSION_MAX &&
module_api_version != NAPI_VERSION_EXPERIMENTAL) {
node::Environment* node_env = node::Environment::GetCurrent(context);
CHECK_NOT_NULL(node_env);
ThrowNodeApiVersionError(
node_env, module_filename.c_str(), module_api_version);
return nullptr;
}

result = new node_napi_env__(context, module_filename, module_api_version);
// TODO(addaleax): There was previously code that tried to delete the
// napi_env when its v8::Context was garbage collected;
// However, as long as N-API addons using this napi_env are in place,
// the Context needs to be accessible and alive.
// Ideally, we'd want an on-addon-unload hook that takes care of this
// 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(); },
static_cast<void*>(result));

return result;
}

class ThreadSafeFunction : public node::AsyncResource {
public:
ThreadSafeFunction(v8::Local<v8::Function> func,
Expand Down Expand Up @@ -728,7 +730,8 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
}

// Create a new napi_env for this specific module.
napi_env env = v8impl::NewEnv(context, module_filename, module_api_version);
napi_env env =
node_napi_env__::New(context, module_filename, module_api_version);

napi_value _exports = nullptr;
env->CallIntoModule([&](napi_env env) {
Expand Down
4 changes: 4 additions & 0 deletions src/node_api_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include "util-inl.h"

struct node_napi_env__ : public napi_env__ {
static napi_env New(v8::Local<v8::Context> context,
const std::string& module_filename,
int32_t module_api_version);

node_napi_env__(v8::Local<v8::Context> context,
const std::string& module_filename,
int32_t module_api_version);
Expand Down
Loading