Skip to content

Commit c326200

Browse files
authored
node-api: convert NewEnv to node_napi_env__::New
PR-URL: nodejs#57834 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 2acc8bc commit c326200

File tree

2 files changed

+52
-45
lines changed

2 files changed

+52
-45
lines changed

src/node_api.cc

+48-45
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,52 @@
2020
#include <cstring>
2121
#include <memory>
2222

23+
namespace v8impl {
24+
static void ThrowNodeApiVersionError(node::Environment* node_env,
25+
const char* module_name,
26+
int32_t module_api_version) {
27+
std::string error_message;
28+
error_message += module_name;
29+
error_message += " requires Node-API version ";
30+
error_message += std::to_string(module_api_version);
31+
error_message += ", but this version of Node.js only supports version ";
32+
error_message += NODE_STRINGIFY(NODE_API_SUPPORTED_VERSION_MAX) " add-ons.";
33+
node_env->ThrowError(error_message.c_str());
34+
}
35+
} // namespace v8impl
36+
37+
/*static*/ napi_env node_napi_env__::New(v8::Local<v8::Context> context,
38+
const std::string& module_filename,
39+
int32_t module_api_version) {
40+
node_napi_env result;
41+
42+
// Validate module_api_version.
43+
if (module_api_version < NODE_API_DEFAULT_MODULE_API_VERSION) {
44+
module_api_version = NODE_API_DEFAULT_MODULE_API_VERSION;
45+
} else if (module_api_version > NODE_API_SUPPORTED_VERSION_MAX &&
46+
module_api_version != NAPI_VERSION_EXPERIMENTAL) {
47+
node::Environment* node_env = node::Environment::GetCurrent(context);
48+
CHECK_NOT_NULL(node_env);
49+
v8impl::ThrowNodeApiVersionError(
50+
node_env, module_filename.c_str(), module_api_version);
51+
return nullptr;
52+
}
53+
54+
result = new node_napi_env__(context, module_filename, module_api_version);
55+
// TODO(addaleax): There was previously code that tried to delete the
56+
// napi_env when its v8::Context was garbage collected;
57+
// However, as long as N-API addons using this napi_env are in place,
58+
// the Context needs to be accessible and alive.
59+
// Ideally, we'd want an on-addon-unload hook that takes care of this
60+
// once all N-API addons using this napi_env are unloaded.
61+
// For now, a per-Environment cleanup hook is the best we can do.
62+
result->node_env()->AddCleanupHook(
63+
[](void* arg) { static_cast<napi_env>(arg)->Unref(); },
64+
static_cast<void*>(result));
65+
66+
return result;
67+
}
68+
2369
node_napi_env__::node_napi_env__(v8::Local<v8::Context> context,
2470
const std::string& module_filename,
2571
int32_t module_api_version)
@@ -152,50 +198,6 @@ class BufferFinalizer : private Finalizer {
152198
~BufferFinalizer() { env()->Unref(); }
153199
};
154200

155-
void ThrowNodeApiVersionError(node::Environment* node_env,
156-
const char* module_name,
157-
int32_t module_api_version) {
158-
std::string error_message;
159-
error_message += module_name;
160-
error_message += " requires Node-API version ";
161-
error_message += std::to_string(module_api_version);
162-
error_message += ", but this version of Node.js only supports version ";
163-
error_message += NODE_STRINGIFY(NODE_API_SUPPORTED_VERSION_MAX) " add-ons.";
164-
node_env->ThrowError(error_message.c_str());
165-
}
166-
167-
inline napi_env NewEnv(v8::Local<v8::Context> context,
168-
const std::string& module_filename,
169-
int32_t module_api_version) {
170-
node_napi_env result;
171-
172-
// Validate module_api_version.
173-
if (module_api_version < NODE_API_DEFAULT_MODULE_API_VERSION) {
174-
module_api_version = NODE_API_DEFAULT_MODULE_API_VERSION;
175-
} else if (module_api_version > NODE_API_SUPPORTED_VERSION_MAX &&
176-
module_api_version != NAPI_VERSION_EXPERIMENTAL) {
177-
node::Environment* node_env = node::Environment::GetCurrent(context);
178-
CHECK_NOT_NULL(node_env);
179-
ThrowNodeApiVersionError(
180-
node_env, module_filename.c_str(), module_api_version);
181-
return nullptr;
182-
}
183-
184-
result = new node_napi_env__(context, module_filename, module_api_version);
185-
// TODO(addaleax): There was previously code that tried to delete the
186-
// napi_env when its v8::Context was garbage collected;
187-
// However, as long as N-API addons using this napi_env are in place,
188-
// the Context needs to be accessible and alive.
189-
// Ideally, we'd want an on-addon-unload hook that takes care of this
190-
// once all N-API addons using this napi_env are unloaded.
191-
// For now, a per-Environment cleanup hook is the best we can do.
192-
result->node_env()->AddCleanupHook(
193-
[](void* arg) { static_cast<napi_env>(arg)->Unref(); },
194-
static_cast<void*>(result));
195-
196-
return result;
197-
}
198-
199201
class ThreadSafeFunction : public node::AsyncResource {
200202
public:
201203
ThreadSafeFunction(v8::Local<v8::Function> func,
@@ -728,7 +730,8 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
728730
}
729731

730732
// Create a new napi_env for this specific module.
731-
napi_env env = v8impl::NewEnv(context, module_filename, module_api_version);
733+
napi_env env =
734+
node_napi_env__::New(context, module_filename, module_api_version);
732735

733736
napi_value _exports = nullptr;
734737
env->CallIntoModule([&](napi_env env) {

src/node_api_internals.h

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include "util-inl.h"
1010

1111
struct node_napi_env__ : public napi_env__ {
12+
static napi_env New(v8::Local<v8::Context> context,
13+
const std::string& module_filename,
14+
int32_t module_api_version);
15+
1216
node_napi_env__(v8::Local<v8::Context> context,
1317
const std::string& module_filename,
1418
int32_t module_api_version);

0 commit comments

Comments
 (0)