Skip to content

Commit 14318a8

Browse files
authored
Remove napi registration code from node.cc (V8) (nodejs#129)
* Remove napi registration code from node.cc * make napi_module_register extern "C"
1 parent e61a100 commit 14318a8

File tree

3 files changed

+68
-58
lines changed

3 files changed

+68
-58
lines changed

src/node.cc

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,23 +2428,26 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
24282428
return;
24292429
}
24302430

2431-
#ifdef ENABLE_NAPI
2432-
bool isNapiModule = (!no_napi_modules && mp->nm_version == -1);
2433-
2434-
if (mp->nm_version != NODE_MODULE_VERSION && !isNapiModule) {
2435-
#else /* !defined ENABLE_NAPI */
24362431
if (mp->nm_version != NODE_MODULE_VERSION) {
2437-
#endif /* def ENABLE_NAPI */
24382432
char errmsg[1024];
2439-
snprintf(errmsg,
2440-
sizeof(errmsg),
2441-
"The module '%s'"
2442-
"\nwas compiled against a different Node.js version using"
2443-
"\nNODE_MODULE_VERSION %d. This version of Node.js requires"
2444-
"\nNODE_MODULE_VERSION %d. Please try re-compiling or "
2445-
"re-installing\nthe module (for instance, using `npm rebuild` or "
2446-
"`npm install`).",
2447-
*filename, mp->nm_version, NODE_MODULE_VERSION);
2433+
if (mp->nm_version == -1) {
2434+
snprintf(errmsg,
2435+
sizeof(errmsg),
2436+
"The module '%s'"
2437+
"\nwas compiled against the Node.js API. This feature is "
2438+
"\nexperimental and must be enabled on the command-line.",
2439+
*filename);
2440+
} else {
2441+
snprintf(errmsg,
2442+
sizeof(errmsg),
2443+
"The module '%s'"
2444+
"\nwas compiled against a different Node.js version using"
2445+
"\nNODE_MODULE_VERSION %d. This version of Node.js requires"
2446+
"\nNODE_MODULE_VERSION %d. Please try re-compiling or "
2447+
"re-installing\nthe module (for instance, using `npm rebuild` or "
2448+
"`npm install`).",
2449+
*filename, mp->nm_version, NODE_MODULE_VERSION);
2450+
}
24482451

24492452
// NOTE: `mp` is allocated inside of the shared library's memory, calling
24502453
// `uv_dlclose` will deallocate it
@@ -2465,21 +2468,6 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
24652468
Local<String> exports_string = env->exports_string();
24662469
Local<Object> exports = module->Get(exports_string)->ToObject(env->isolate());
24672470

2468-
#ifdef ENABLE_NAPI
2469-
if (isNapiModule) {
2470-
if (mp->nm_register_func != nullptr) {
2471-
reinterpret_cast<node::addon_abi_register_func>(mp->nm_register_func)(
2472-
v8impl::JsEnvFromV8Isolate(v8::Isolate::GetCurrent()),
2473-
v8impl::JsValueFromV8LocalValue(exports),
2474-
v8impl::JsValueFromV8LocalValue(module),
2475-
mp->nm_priv);
2476-
} else {
2477-
uv_dlclose(&lib);
2478-
env->ThrowError("Module has no declared entry point.");
2479-
}
2480-
return;
2481-
}
2482-
#endif /* def ENABLE_NAPI */
24832471
if (mp->nm_context_register_func != nullptr) {
24842472
mp->nm_context_register_func(exports, module, env->context(), mp->nm_priv);
24852473
} else if (mp->nm_register_func != nullptr) {
@@ -2714,19 +2702,7 @@ static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {
27142702
env->context(),
27152703
mod->nm_priv);
27162704
} else if (mod->nm_register_func != nullptr) {
2717-
#ifdef ENABLE_NAPI
2718-
if (mod->nm_version != -1) {
2719-
mod->nm_register_func(exports, module, mod->nm_priv);
2720-
} else {
2721-
reinterpret_cast<node::addon_abi_register_func>(mod->nm_register_func)(
2722-
v8impl::JsEnvFromV8Isolate(v8::Isolate::GetCurrent()),
2723-
v8impl::JsValueFromV8LocalValue(exports),
2724-
v8impl::JsValueFromV8LocalValue(module),
2725-
mod->nm_priv);
2726-
}
2727-
#else /* !defined ENABLE_NAPI */
27282705
mod->nm_register_func(exports, module, mod->nm_priv);
2729-
#endif /* def ENABLE_NAPI */
27302706
} else {
27312707
return env->ThrowError("Linked module has no declared entry point.");
27322708
}

src/node_jsvmapi.cc

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
#include <vector>
1515
#include <string.h>
1616

17-
void napi_module_register(void* mod) {
18-
node::node_module_register(mod);
19-
}
20-
2117
namespace v8impl {
2218

2319
//=== Conversion between V8 Isolate and napi_env ==========================
@@ -493,6 +489,48 @@ namespace v8impl {
493489

494490
} // end of namespace v8impl
495491

492+
// Intercepts the Node-V8 module registration callback. Converts parameters to NAPI equivalents
493+
// and then calls the registration callback specified by the NAPI module.
494+
void napi_module_register_cb(v8::Local<v8::Object> exports,
495+
v8::Local<v8::Value> module,
496+
v8::Local<v8::Context> context,
497+
void* priv) {
498+
napi_module* mod = static_cast<napi_module*>(priv);
499+
mod->nm_register_func(
500+
v8impl::JsEnvFromV8Isolate(context->GetIsolate()),
501+
v8impl::JsValueFromV8LocalValue(exports),
502+
v8impl::JsValueFromV8LocalValue(module),
503+
mod->nm_priv);
504+
}
505+
506+
namespace node {
507+
// Indicates whether NAPI was enabled/disabled via the node command-line.
508+
extern bool no_napi_modules;
509+
}
510+
511+
// Registers a NAPI module.
512+
void napi_module_register(napi_module* mod) {
513+
// NAPI modules always work with the current node version.
514+
int moduleVersion = NODE_MODULE_VERSION;
515+
if (node::no_napi_modules) {
516+
// NAPI is disabled, so set the module version to -1 to cause the module to be unloaded.
517+
moduleVersion = -1;
518+
}
519+
520+
node::node_module* nm = new node::node_module {
521+
moduleVersion,
522+
mod->nm_flags,
523+
nullptr,
524+
mod->nm_filename,
525+
nullptr,
526+
napi_module_register_cb,
527+
mod->nm_modname,
528+
mod, // priv
529+
nullptr,
530+
};
531+
node::node_module_register(nm);
532+
}
533+
496534
#define RETURN_STATUS_IF_FALSE(condition, status) \
497535
do { \
498536
if (!(condition)) { \

src/node_jsvmapi.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,17 @@ NODE_EXTERN typedef void (*addon_abi_register_func)(
2323
void* priv);
2424
} // namespace node
2525

26-
struct napi_module_struct {
26+
struct napi_module {
2727
int nm_version;
2828
unsigned int nm_flags;
29-
void* nm_dso_handle;
3029
const char* nm_filename;
3130
node::addon_abi_register_func nm_register_func;
32-
void* nm_context_register_func;
3331
const char* nm_modname;
3432
void* nm_priv;
35-
void* nm_link;
33+
void* reserved[4];
3634
};
3735

38-
NODE_EXTERN void napi_module_register(void* mod);
36+
#define NAPI_MODULE_VERSION 1
3937

4038
#if defined(_MSC_VER)
4139
#pragma section(".CRT$XCU", read)
@@ -52,17 +50,15 @@ NODE_EXTERN void napi_module_register(void* mod);
5250

5351
#define NODE_MODULE_ABI_X(modname, regfunc, priv, flags) \
5452
extern "C" { \
55-
static napi_module_struct _module = \
53+
static napi_module _module = \
5654
{ \
57-
-1, \
55+
NAPI_MODULE_VERSION, \
5856
flags, \
59-
NULL, \
6057
__FILE__, \
6158
(node::addon_abi_register_func)regfunc, \
62-
NULL, \
6359
#modname, \
6460
priv, \
65-
NULL \
61+
{0}, \
6662
}; \
6763
NODE_ABI_CTOR(_register_ ## modname) { \
6864
napi_module_register(&_module); \
@@ -73,10 +69,10 @@ NODE_EXTERN void napi_module_register(void* mod);
7369
NODE_MODULE_ABI_X(modname, regfunc, NULL, 0)
7470

7571

76-
// TODO(ianhall): We're using C linkage for the API but we're also using the
77-
// bool type in these exports. Is that safe and stable?
7872
extern "C" {
7973

74+
NODE_EXTERN void napi_module_register(napi_module* mod);
75+
8076
NODE_EXTERN const napi_extended_error_info* napi_get_last_error_info();
8177

8278
// Environment

0 commit comments

Comments
 (0)