2929#include " node_lttng.h"
3030#endif
3131
32+ #if defined ENABLE_NAPI
3233#include " node_jsvmapi_internal.h"
34+ #endif
3335
3436#include " ares.h"
3537#include " async-wrap.h"
@@ -98,7 +100,6 @@ typedef int mode_t;
98100extern char **environ;
99101#endif
100102
101-
102103namespace node {
103104
104105using v8::Array;
@@ -162,6 +163,9 @@ static const char* trace_enabled_categories = nullptr;
162163static const char * icu_data_dir = nullptr ;
163164#endif
164165
166+ // By default we accept N-API addons
167+ bool no_napi_modules = false ;
168+
165169// used by C++ modules as well
166170bool no_deprecation = false ;
167171
@@ -2418,73 +2422,19 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
24182422 return ;
24192423 }
24202424
2421- // try loading with node 0.10 manner in case of node 0.10
2422- struct node_module_old * mod;
2423- char symbol[1024 ], *base, *pos;
2424- int r;
2425- if (mp == NULL ) {
2426- node::Utf8Value path (env->isolate (), args[1 ]);
2427- base = *path;
2428-
2429- /* Find the shared library filename within the full path. */
2430- #ifdef __POSIX__
2431- pos = strrchr (base, ' /' );
2432- if (pos != NULL ) {
2433- base = pos + 1 ;
2434- }
2435- #else // Windows
2436- for (;;) {
2437- pos = strpbrk (base, " \\ /:" );
2438- if (pos == NULL ) {
2439- break ;
2440- }
2441- base = pos + 1 ;
2442- }
2443- #endif
2444-
2445- /* Strip the .node extension. */
2446- pos = strrchr (base, ' .' );
2447- if (pos != NULL ) {
2448- *pos = ' \0 ' ;
2449- }
2450- /* Add the `_module` suffix to the extension name. */
2451- r = snprintf (symbol, sizeof symbol, " %s_module" , base);
2452- if (r <= 0 || static_cast <size_t >(r) >= sizeof symbol) {
2453- env->ThrowError (" Out of memory." );
2454- }
2455-
2456- /* Replace dashes with underscores. When loading foo-bar.node,
2457- * look for foo_bar_module, not foo-bar_module.
2458- */
2459- for (pos = symbol; *pos != ' \0 ' ; ++pos) {
2460- if (*pos == ' -' ) *pos = ' _' ;
2461- }
2462-
2463- if (uv_dlsym (&lib, symbol, reinterpret_cast <void **>(&mod))) {
2464- char errmsg[1024 ];
2465- snprintf (errmsg, sizeof (errmsg), " Symbol %s not found." , symbol);
2466- env->ThrowError (errmsg);
2467- return ;
2468- }
2469- mp = new struct node_module ;
2470- mp->nm_version = mod->version ;
2471- mp->nm_flags = 0 ;
2472- mp->nm_filename = mod->filename ;
2473- mp->nm_register_func =
2474- reinterpret_cast <node::addon_register_func>(mod->register_func );
2475- mp->nm_context_register_func = NULL ;
2476- mp->nm_modname = mod->modname ;
2477- }
2478-
24792425 if (mp == nullptr ) {
24802426 uv_dlclose (&lib);
24812427 env->ThrowError (" Module did not self-register." );
24822428 return ;
24832429 }
24842430
2485- bool isNapiModule = mp->nm_version == -1 ;
2431+ #ifdef ENABLE_NAPI
2432+ bool isNapiModule = (!no_napi_modules && mp->nm_version == -1 );
24862433
24872434 if (mp->nm_version != NODE_MODULE_VERSION && !isNapiModule) {
2435+ #else /* !defined ENABLE_NAPI */
2436+ if (mp->nm_version != NODE_MODULE_VERSION) {
2437+ #endif /* def ENABLE_NAPI */
24882438 char errmsg[1024 ];
24892439 snprintf (errmsg,
24902440 sizeof (errmsg),
@@ -2515,6 +2465,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
25152465 Local<String> exports_string = env->exports_string ();
25162466 Local<Object> exports = module ->Get (exports_string)->ToObject (env->isolate ());
25172467
2468+ #ifdef ENABLE_NAPI
25182469 if (isNapiModule) {
25192470 if (mp->nm_register_func != nullptr ) {
25202471 reinterpret_cast <node::addon_abi_register_func>(mp->nm_register_func )(
@@ -2525,18 +2476,18 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
25252476 } else {
25262477 uv_dlclose (&lib);
25272478 env->ThrowError (" Module has no declared entry point." );
2528- return ;
25292479 }
2480+ return ;
2481+ }
2482+ #endif /* def ENABLE_NAPI */
2483+ if (mp->nm_context_register_func != nullptr ) {
2484+ mp->nm_context_register_func (exports, module , env->context (), mp->nm_priv );
2485+ } else if (mp->nm_register_func != nullptr ) {
2486+ mp->nm_register_func (exports, module , mp->nm_priv );
25302487 } else {
2531- if (mp->nm_context_register_func != nullptr ) {
2532- mp->nm_context_register_func (exports, module , env->context (), mp->nm_priv );
2533- } else if (mp->nm_register_func != nullptr ) {
2534- mp->nm_register_func (exports, module , mp->nm_priv );
2535- } else {
2536- uv_dlclose (&lib);
2537- env->ThrowError (" Module has no declared entry point." );
2538- return ;
2539- }
2488+ uv_dlclose (&lib);
2489+ env->ThrowError (" Module has no declared entry point." );
2490+ return ;
25402491 }
25412492
25422493 // Tell coverity that 'handle' should not be freed when we return.
@@ -2763,6 +2714,7 @@ static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {
27632714 env->context (),
27642715 mod->nm_priv );
27652716 } else if (mod->nm_register_func != nullptr ) {
2717+ #ifdef ENABLE_NAPI
27662718 if (mod->nm_version != -1 ) {
27672719 mod->nm_register_func (exports, module , mod->nm_priv );
27682720 } else {
@@ -2772,6 +2724,9 @@ static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {
27722724 v8impl::JsValueFromV8LocalValue (module ),
27732725 mod->nm_priv );
27742726 }
2727+ #else /* !defined ENABLE_NAPI */
2728+ mod->nm_register_func (exports, module , mod->nm_priv );
2729+ #endif /* def ENABLE_NAPI */
27752730 } else {
27762731 return env->ThrowError (" Linked module has no declared entry point." );
27772732 }
@@ -3755,6 +3710,8 @@ static void ParseArgs(int* argc,
37553710 force_repl = true ;
37563711 } else if (strcmp (arg, " --no-deprecation" ) == 0 ) {
37573712 no_deprecation = true ;
3713+ } else if (strcmp (arg, " --no-napi-modules" ) == 0 ) {
3714+ no_napi_modules = true ;
37583715 } else if (strcmp (arg, " --no-warnings" ) == 0 ) {
37593716 no_process_warnings = true ;
37603717 } else if (strcmp (arg, " --trace-warnings" ) == 0 ) {
0 commit comments