2828#include " node_lttng.h"
2929#endif
3030
31+ #if defined ENABLE_NAPI
3132#include " node_jsvmapi_internal.h"
33+ #endif
3234
3335#include " ares.h"
3436#include " async-wrap.h"
@@ -92,7 +94,6 @@ typedef int mode_t;
9294extern char **environ;
9395#endif
9496
95-
9697namespace node {
9798
9899using v8::Array;
@@ -158,6 +159,9 @@ static node_module* modlist_addon;
158159static const char * icu_data_dir = nullptr ;
159160#endif
160161
162+ // By default we accept N-API addons
163+ bool no_napi_modules = false ;
164+
161165// used by C++ modules as well
162166bool no_deprecation = false ;
163167
@@ -2339,73 +2343,19 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
23392343 return ;
23402344 }
23412345
2342- // try loading with node 0.10 manner in case of node 0.10
2343- struct node_module_old * mod;
2344- char symbol[1024 ], *base, *pos;
2345- int r;
2346- if (mp == NULL ) {
2347- node::Utf8Value path (env->isolate (), args[1 ]);
2348- base = *path;
2349-
2350- /* Find the shared library filename within the full path. */
2351- #ifdef __POSIX__
2352- pos = strrchr (base, ' /' );
2353- if (pos != NULL ) {
2354- base = pos + 1 ;
2355- }
2356- #else // Windows
2357- for (;;) {
2358- pos = strpbrk (base, " \\ /:" );
2359- if (pos == NULL ) {
2360- break ;
2361- }
2362- base = pos + 1 ;
2363- }
2364- #endif
2365-
2366- /* Strip the .node extension. */
2367- pos = strrchr (base, ' .' );
2368- if (pos != NULL ) {
2369- *pos = ' \0 ' ;
2370- }
2371- /* Add the `_module` suffix to the extension name. */
2372- r = snprintf (symbol, sizeof symbol, " %s_module" , base);
2373- if (r <= 0 || static_cast <size_t >(r) >= sizeof symbol) {
2374- env->ThrowError (" Out of memory." );
2375- }
2376-
2377- /* Replace dashes with underscores. When loading foo-bar.node,
2378- * look for foo_bar_module, not foo-bar_module.
2379- */
2380- for (pos = symbol; *pos != ' \0 ' ; ++pos) {
2381- if (*pos == ' -' ) *pos = ' _' ;
2382- }
2383-
2384- if (uv_dlsym (&lib, symbol, reinterpret_cast <void **>(&mod))) {
2385- char errmsg[1024 ];
2386- snprintf (errmsg, sizeof (errmsg), " Symbol %s not found." , symbol);
2387- env->ThrowError (errmsg);
2388- return ;
2389- }
2390- mp = new struct node_module ;
2391- mp->nm_version = mod->version ;
2392- mp->nm_flags = 0 ;
2393- mp->nm_filename = mod->filename ;
2394- mp->nm_register_func =
2395- reinterpret_cast <node::addon_register_func>(mod->register_func );
2396- mp->nm_context_register_func = NULL ;
2397- mp->nm_modname = mod->modname ;
2398- }
2399-
24002346 if (mp == nullptr ) {
24012347 uv_dlclose (&lib);
24022348 env->ThrowError (" Module did not self-register." );
24032349 return ;
24042350 }
24052351
2406- bool isNapiModule = mp->nm_version == -1 ;
2352+ #ifdef ENABLE_NAPI
2353+ bool isNapiModule = (!no_napi_modules && mp->nm_version == -1 );
24072354
24082355 if (mp->nm_version != NODE_MODULE_VERSION && !isNapiModule) {
2356+ #else /* !defined ENABLE_NAPI */
2357+ if (mp->nm_version != NODE_MODULE_VERSION ) {
2358+ #endif /* def ENABLE_NAPI */
24092359 char errmsg[1024 ];
24102360 snprintf (errmsg,
24112361 sizeof (errmsg),
@@ -2431,6 +2381,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
24312381 Local<String> exports_string = env->exports_string ();
24322382 Local<Object> exports = module ->Get (exports_string)->ToObject (env->isolate ());
24332383
2384+ #ifdef ENABLE_NAPI
24342385 if (isNapiModule) {
24352386 if (mp->nm_register_func != nullptr ) {
24362387 reinterpret_cast <node::addon_abi_register_func>(mp->nm_register_func )(
@@ -2441,18 +2392,18 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
24412392 } else {
24422393 uv_dlclose (&lib);
24432394 env->ThrowError (" Module has no declared entry point." );
2444- return ;
24452395 }
2396+ return ;
2397+ }
2398+ #endif /* def ENABLE_NAPI */
2399+ if (mp->nm_context_register_func != nullptr ) {
2400+ mp->nm_context_register_func (exports, module , env->context (), mp->nm_priv );
2401+ } else if (mp->nm_register_func != nullptr ) {
2402+ mp->nm_register_func (exports, module , mp->nm_priv );
24462403 } else {
2447- if (mp->nm_context_register_func != nullptr ) {
2448- mp->nm_context_register_func (exports, module , env->context (), mp->nm_priv );
2449- } else if (mp->nm_register_func != nullptr ) {
2450- mp->nm_register_func (exports, module , mp->nm_priv );
2451- } else {
2452- uv_dlclose (&lib);
2453- env->ThrowError (" Module has no declared entry point." );
2454- return ;
2455- }
2404+ uv_dlclose (&lib);
2405+ env->ThrowError (" Module has no declared entry point." );
2406+ return ;
24562407 }
24572408
24582409 // Tell coverity that 'handle' should not be freed when we return.
@@ -2640,6 +2591,7 @@ static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {
26402591 env->context (),
26412592 mod->nm_priv );
26422593 } else if (mod->nm_register_func != nullptr ) {
2594+ #ifdef ENABLE_NAPI
26432595 if (mod->nm_version != -1 ) {
26442596 mod->nm_register_func (exports, module , mod->nm_priv );
26452597 } else {
@@ -2649,6 +2601,9 @@ static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {
26492601 v8impl::JsValueFromV8LocalValue (module ),
26502602 mod->nm_priv );
26512603 }
2604+ #else /* !defined ENABLE_NAPI */
2605+ mod->nm_register_func (exports, module , mod->nm_priv );
2606+ #endif /* def ENABLE_NAPI */
26522607 } else {
26532608 return env->ThrowError (" Linked module has no declared entry point." );
26542609 }
@@ -3668,6 +3623,8 @@ static void ParseArgs(int* argc,
36683623 force_repl = true ;
36693624 } else if (strcmp (arg, " --no-deprecation" ) == 0 ) {
36703625 no_deprecation = true ;
3626+ } else if (strcmp (arg, " --no-napi-modules" ) == 0 ) {
3627+ no_napi_modules = true ;
36713628 } else if (strcmp (arg, " --no-warnings" ) == 0 ) {
36723629 no_process_warnings = true ;
36733630 } else if (strcmp (arg, " --trace-warnings" ) == 0 ) {
0 commit comments