diff --git a/src/env-inl.h b/src/env-inl.h index 96c8f533478b3e..ecbf39cd0b15b8 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -396,9 +396,10 @@ inline uv_loop_t* Environment::event_loop() const { return isolate_data()->event_loop(); } -inline void Environment::TryLoadAddon(const char* filename, - int flags, - std::function was_loaded) { +inline void Environment::TryLoadAddon( + const char* filename, + int flags, + std::function was_loaded) { loaded_addons_.emplace_back(filename, flags); if (!was_loaded(&loaded_addons_.back())) { loaded_addons_.pop_back(); diff --git a/src/node_binding-inl.h b/src/node_binding-inl.h index 7d2703072f0fb8..0c74236969be1e 100644 --- a/src/node_binding-inl.h +++ b/src/node_binding-inl.h @@ -7,49 +7,49 @@ namespace node { namespace binding { -inline DLib::DLib(const char* filename, int flags): - filename_(filename), flags_(flags), handle_(nullptr) {} +inline DLib::DLib(const char* filename, int flags) + : filename_(filename), flags_(flags), handle_(nullptr) {} #ifdef __POSIX__ - inline bool DLib::Open() { - handle_ = dlopen(filename_.c_str(), flags_); - if (handle_ != nullptr) return true; - errmsg_ = dlerror(); - return false; - } - - inline void DLib::Close() { - if (handle_ == nullptr) return; - dlclose(handle_); - handle_ = nullptr; - } - - inline void* DLib::GetSymbolAddress(const char* name) { - return dlsym(handle_, name); - } +inline bool DLib::Open() { + handle_ = dlopen(filename_.c_str(), flags_); + if (handle_ != nullptr) return true; + errmsg_ = dlerror(); + return false; +} + +inline void DLib::Close() { + if (handle_ == nullptr) return; + dlclose(handle_); + handle_ = nullptr; +} + +inline void* DLib::GetSymbolAddress(const char* name) { + return dlsym(handle_, name); +} #else // !__POSIX__ - inline bool DLib::Open() { - int ret = uv_dlopen(filename_.c_str(), &lib_); - if (ret == 0) { - handle_ = static_cast(lib_.handle); - return true; - } - errmsg_ = uv_dlerror(&lib_); - uv_dlclose(&lib_); - return false; - } - - inline void DLib::Close() { - if (handle_ == nullptr) return; - uv_dlclose(&lib_); - handle_ = nullptr; - } - - inline void* DLib::GetSymbolAddress(const char* name) { - void* address; - if (0 == uv_dlsym(&lib_, name, &address)) return address; - return nullptr; +inline bool DLib::Open() { + int ret = uv_dlopen(filename_.c_str(), &lib_); + if (ret == 0) { + handle_ = static_cast(lib_.handle); + return true; } + errmsg_ = uv_dlerror(&lib_); + uv_dlclose(&lib_); + return false; +} + +inline void DLib::Close() { + if (handle_ == nullptr) return; + uv_dlclose(&lib_); + handle_ = nullptr; +} + +inline void* DLib::GetSymbolAddress(const char* name) { + void* address; + if (0 == uv_dlsym(&lib_, name, &address)) return address; + return nullptr; +} #endif // !__POSIX__ } // end of namespace binding diff --git a/src/node_binding.cc b/src/node_binding.cc index 0653f40e80edb3..79d1c42a67e081 100644 --- a/src/node_binding.cc +++ b/src/node_binding.cc @@ -176,89 +176,90 @@ void DLOpen(const FunctionCallbackInfo& args) { node::Utf8Value filename(env->isolate(), args[1]); // Cast env->TryLoadAddon(*filename, flags, [&](DLib* dlib) { - const bool is_opened = dlib->Open(); - - // Objects containing v14 or later modules will have registered themselves - // on the pending list. Activate all of them now. At present, only one - // module per object is supported. - node_module* const mp = - static_cast(uv_key_get(&thread_local_modpending)); - uv_key_set(&thread_local_modpending, nullptr); - - if (!is_opened) { - Local errmsg = OneByteString(env->isolate(), dlib->errmsg_.c_str()); - dlib->Close(); + const bool is_opened = dlib->Open(); + + // Objects containing v14 or later modules will have registered themselves + // on the pending list. Activate all of them now. At present, only one + // module per object is supported. + node_module* const mp = + static_cast(uv_key_get(&thread_local_modpending)); + uv_key_set(&thread_local_modpending, nullptr); + + if (!is_opened) { + Local errmsg = + OneByteString(env->isolate(), dlib->errmsg_.c_str()); + dlib->Close(); #ifdef _WIN32 - // Windows needs to add the filename into the error message - errmsg = String::Concat( - env->isolate(), errmsg, args[1]->ToString(context).ToLocalChecked()); + // Windows needs to add the filename into the error message + errmsg = String::Concat( + env->isolate(), errmsg, args[1]->ToString(context).ToLocalChecked()); #endif // _WIN32 - env->isolate()->ThrowException(Exception::Error(errmsg)); - return false; - } - - if (mp == nullptr) { - if (auto callback = GetInitializerCallback(dlib)) { - callback(exports, module, context); - } else if (auto napi_callback = GetNapiInitializerCallback(dlib)) { - napi_module_register_by_symbol(exports, module, context, napi_callback); - } else { - dlib->Close(); - env->ThrowError("Module did not self-register."); + env->isolate()->ThrowException(Exception::Error(errmsg)); return false; } - return true; - } - // -1 is used for N-API modules - if ((mp->nm_version != -1) && (mp->nm_version != NODE_MODULE_VERSION)) { - // Even if the module did self-register, it may have done so with the wrong - // version. We must only give up after having checked to see if it has an - // appropriate initializer callback. - if (auto callback = GetInitializerCallback(dlib)) { - callback(exports, module, context); + if (mp == nullptr) { + if (auto callback = GetInitializerCallback(dlib)) { + callback(exports, module, context); + } else if (auto napi_callback = GetNapiInitializerCallback(dlib)) { + napi_module_register_by_symbol(exports, module, context, napi_callback); + } else { + dlib->Close(); + env->ThrowError("Module did not self-register."); + return false; + } return true; } - char errmsg[1024]; - snprintf(errmsg, - sizeof(errmsg), - "The module '%s'" - "\nwas compiled against a different Node.js version using" - "\nNODE_MODULE_VERSION %d. This version of Node.js requires" - "\nNODE_MODULE_VERSION %d. Please try re-compiling or " - "re-installing\nthe module (for instance, using `npm rebuild` " - "or `npm install`).", - *filename, - mp->nm_version, - NODE_MODULE_VERSION); - - // NOTE: `mp` is allocated inside of the shared library's memory, calling - // `dlclose` will deallocate it - dlib->Close(); - env->ThrowError(errmsg); - return false; - } - if (mp->nm_flags & NM_F_BUILTIN) { - dlib->Close(); - env->ThrowError("Built-in module self-registered."); - return false; - } - mp->nm_dso_handle = dlib->handle_; - mp->nm_link = modlist_addon; - modlist_addon = mp; + // -1 is used for N-API modules + if ((mp->nm_version != -1) && (mp->nm_version != NODE_MODULE_VERSION)) { + // Even if the module did self-register, it may have done so with the + // wrong version. We must only give up after having checked to see if it + // has an appropriate initializer callback. + if (auto callback = GetInitializerCallback(dlib)) { + callback(exports, module, context); + return true; + } + char errmsg[1024]; + snprintf(errmsg, + sizeof(errmsg), + "The module '%s'" + "\nwas compiled against a different Node.js version using" + "\nNODE_MODULE_VERSION %d. This version of Node.js requires" + "\nNODE_MODULE_VERSION %d. Please try re-compiling or " + "re-installing\nthe module (for instance, using `npm rebuild` " + "or `npm install`).", + *filename, + mp->nm_version, + NODE_MODULE_VERSION); + + // NOTE: `mp` is allocated inside of the shared library's memory, calling + // `dlclose` will deallocate it + dlib->Close(); + env->ThrowError(errmsg); + return false; + } + if (mp->nm_flags & NM_F_BUILTIN) { + dlib->Close(); + env->ThrowError("Built-in module self-registered."); + return false; + } - if (mp->nm_context_register_func != nullptr) { - mp->nm_context_register_func(exports, module, context, mp->nm_priv); - } else if (mp->nm_register_func != nullptr) { - mp->nm_register_func(exports, module, mp->nm_priv); - } else { - dlib->Close(); - env->ThrowError("Module has no declared entry point."); - return false; - } + mp->nm_dso_handle = dlib->handle_; + mp->nm_link = modlist_addon; + modlist_addon = mp; - return true; + if (mp->nm_context_register_func != nullptr) { + mp->nm_context_register_func(exports, module, context, mp->nm_priv); + } else if (mp->nm_register_func != nullptr) { + mp->nm_register_func(exports, module, mp->nm_priv); + } else { + dlib->Close(); + env->ThrowError("Module has no declared entry point."); + return false; + } + + return true; }); // Tell coverity that 'handle' should not be freed when we return.