|
2 | 2 | #include "node_internals.h" |
3 | 3 | #include "node_native_module.h" |
4 | 4 |
|
5 | | -#if defined(__POSIX__) |
6 | | -#include <dlfcn.h> |
7 | | -#endif |
8 | | - |
9 | 5 | #if HAVE_OPENSSL |
10 | 6 | #define NODE_BUILTIN_OPENSSL_MODULES(V) V(crypto) V(tls_wrap) |
11 | 7 | #else |
@@ -127,31 +123,8 @@ extern "C" void node_module_register(void* m) { |
127 | 123 |
|
128 | 124 | namespace binding { |
129 | 125 |
|
130 | | -class DLib { |
131 | | - public: |
132 | | -#ifdef __POSIX__ |
133 | | - static const int kDefaultFlags = RTLD_LAZY; |
134 | | -#else |
135 | | - static const int kDefaultFlags = 0; |
136 | | -#endif |
137 | | - |
138 | | - inline DLib(const char* filename, int flags) |
139 | | - : filename_(filename), flags_(flags), handle_(nullptr) {} |
140 | | - |
141 | | - inline bool Open(); |
142 | | - inline void Close(); |
143 | | - inline void* GetSymbolAddress(const char* name); |
144 | | - |
145 | | - const std::string filename_; |
146 | | - const int flags_; |
147 | | - std::string errmsg_; |
148 | | - void* handle_; |
149 | | -#ifndef __POSIX__ |
150 | | - uv_lib_t lib_; |
151 | | -#endif |
152 | | - private: |
153 | | - DISALLOW_COPY_AND_ASSIGN(DLib); |
154 | | -}; |
| 126 | +DLib::DLib(const char* filename, int flags) |
| 127 | + : filename_(filename), flags_(flags), handle_(nullptr) {} |
155 | 128 |
|
156 | 129 | #ifdef __POSIX__ |
157 | 130 | bool DLib::Open() { |
@@ -248,87 +221,92 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) { |
248 | 221 | } |
249 | 222 |
|
250 | 223 | node::Utf8Value filename(env->isolate(), args[1]); // Cast |
251 | | - DLib dlib(*filename, flags); |
252 | | - bool is_opened = dlib.Open(); |
253 | | - |
254 | | - // Objects containing v14 or later modules will have registered themselves |
255 | | - // on the pending list. Activate all of them now. At present, only one |
256 | | - // module per object is supported. |
257 | | - node_module* const mp = |
258 | | - static_cast<node_module*>(uv_key_get(&thread_local_modpending)); |
259 | | - uv_key_set(&thread_local_modpending, nullptr); |
260 | | - |
261 | | - if (!is_opened) { |
262 | | - Local<String> errmsg = OneByteString(env->isolate(), dlib.errmsg_.c_str()); |
263 | | - dlib.Close(); |
| 224 | + env->TryLoadAddon(*filename, flags, [&](DLib* dlib) { |
| 225 | + const bool is_opened = dlib->Open(); |
| 226 | + |
| 227 | + // Objects containing v14 or later modules will have registered themselves |
| 228 | + // on the pending list. Activate all of them now. At present, only one |
| 229 | + // module per object is supported. |
| 230 | + node_module* const mp = |
| 231 | + static_cast<node_module*>(uv_key_get(&thread_local_modpending)); |
| 232 | + uv_key_set(&thread_local_modpending, nullptr); |
| 233 | + |
| 234 | + if (!is_opened) { |
| 235 | + Local<String> errmsg = |
| 236 | + OneByteString(env->isolate(), dlib->errmsg_.c_str()); |
| 237 | + dlib->Close(); |
264 | 238 | #ifdef _WIN32 |
265 | | - // Windows needs to add the filename into the error message |
266 | | - errmsg = String::Concat( |
267 | | - env->isolate(), errmsg, args[1]->ToString(context).ToLocalChecked()); |
| 239 | + // Windows needs to add the filename into the error message |
| 240 | + errmsg = String::Concat( |
| 241 | + env->isolate(), errmsg, args[1]->ToString(context).ToLocalChecked()); |
268 | 242 | #endif // _WIN32 |
269 | | - env->isolate()->ThrowException(Exception::Error(errmsg)); |
270 | | - return; |
271 | | - } |
| 243 | + env->isolate()->ThrowException(Exception::Error(errmsg)); |
| 244 | + return false; |
| 245 | + } |
272 | 246 |
|
273 | | - if (mp == nullptr) { |
274 | | - if (auto callback = GetInitializerCallback(&dlib)) { |
275 | | - callback(exports, module, context); |
276 | | - } else if (auto napi_callback = GetNapiInitializerCallback(&dlib)) { |
277 | | - napi_module_register_by_symbol(exports, module, context, napi_callback); |
278 | | - } else { |
279 | | - dlib.Close(); |
280 | | - env->ThrowError("Module did not self-register."); |
| 247 | + if (mp == nullptr) { |
| 248 | + if (auto callback = GetInitializerCallback(dlib)) { |
| 249 | + callback(exports, module, context); |
| 250 | + } else if (auto napi_callback = GetNapiInitializerCallback(dlib)) { |
| 251 | + napi_module_register_by_symbol(exports, module, context, napi_callback); |
| 252 | + } else { |
| 253 | + dlib->Close(); |
| 254 | + env->ThrowError("Module did not self-register."); |
| 255 | + return false; |
| 256 | + } |
| 257 | + return true; |
281 | 258 | } |
282 | | - return; |
283 | | - } |
284 | 259 |
|
285 | | - // -1 is used for N-API modules |
286 | | - if ((mp->nm_version != -1) && (mp->nm_version != NODE_MODULE_VERSION)) { |
287 | | - // Even if the module did self-register, it may have done so with the wrong |
288 | | - // version. We must only give up after having checked to see if it has an |
289 | | - // appropriate initializer callback. |
290 | | - if (auto callback = GetInitializerCallback(&dlib)) { |
291 | | - callback(exports, module, context); |
292 | | - return; |
| 260 | + // -1 is used for N-API modules |
| 261 | + if ((mp->nm_version != -1) && (mp->nm_version != NODE_MODULE_VERSION)) { |
| 262 | + // Even if the module did self-register, it may have done so with the |
| 263 | + // wrong version. We must only give up after having checked to see if it |
| 264 | + // has an appropriate initializer callback. |
| 265 | + if (auto callback = GetInitializerCallback(dlib)) { |
| 266 | + callback(exports, module, context); |
| 267 | + return true; |
| 268 | + } |
| 269 | + char errmsg[1024]; |
| 270 | + snprintf(errmsg, |
| 271 | + sizeof(errmsg), |
| 272 | + "The module '%s'" |
| 273 | + "\nwas compiled against a different Node.js version using" |
| 274 | + "\nNODE_MODULE_VERSION %d. This version of Node.js requires" |
| 275 | + "\nNODE_MODULE_VERSION %d. Please try re-compiling or " |
| 276 | + "re-installing\nthe module (for instance, using `npm rebuild` " |
| 277 | + "or `npm install`).", |
| 278 | + *filename, |
| 279 | + mp->nm_version, |
| 280 | + NODE_MODULE_VERSION); |
| 281 | + |
| 282 | + // NOTE: `mp` is allocated inside of the shared library's memory, calling |
| 283 | + // `dlclose` will deallocate it |
| 284 | + dlib->Close(); |
| 285 | + env->ThrowError(errmsg); |
| 286 | + return false; |
| 287 | + } |
| 288 | + if (mp->nm_flags & NM_F_BUILTIN) { |
| 289 | + dlib->Close(); |
| 290 | + env->ThrowError("Built-in module self-registered."); |
| 291 | + return false; |
293 | 292 | } |
294 | | - char errmsg[1024]; |
295 | | - snprintf(errmsg, |
296 | | - sizeof(errmsg), |
297 | | - "The module '%s'" |
298 | | - "\nwas compiled against a different Node.js version using" |
299 | | - "\nNODE_MODULE_VERSION %d. This version of Node.js requires" |
300 | | - "\nNODE_MODULE_VERSION %d. Please try re-compiling or " |
301 | | - "re-installing\nthe module (for instance, using `npm rebuild` " |
302 | | - "or `npm install`).", |
303 | | - *filename, |
304 | | - mp->nm_version, |
305 | | - NODE_MODULE_VERSION); |
306 | | - |
307 | | - // NOTE: `mp` is allocated inside of the shared library's memory, calling |
308 | | - // `dlclose` will deallocate it |
309 | | - dlib.Close(); |
310 | | - env->ThrowError(errmsg); |
311 | | - return; |
312 | | - } |
313 | | - if (mp->nm_flags & NM_F_BUILTIN) { |
314 | | - dlib.Close(); |
315 | | - env->ThrowError("Built-in module self-registered."); |
316 | | - return; |
317 | | - } |
318 | 293 |
|
319 | | - mp->nm_dso_handle = dlib.handle_; |
320 | | - mp->nm_link = modlist_addon; |
321 | | - modlist_addon = mp; |
| 294 | + mp->nm_dso_handle = dlib->handle_; |
| 295 | + mp->nm_link = modlist_addon; |
| 296 | + modlist_addon = mp; |
322 | 297 |
|
323 | | - if (mp->nm_context_register_func != nullptr) { |
324 | | - mp->nm_context_register_func(exports, module, context, mp->nm_priv); |
325 | | - } else if (mp->nm_register_func != nullptr) { |
326 | | - mp->nm_register_func(exports, module, mp->nm_priv); |
327 | | - } else { |
328 | | - dlib.Close(); |
329 | | - env->ThrowError("Module has no declared entry point."); |
330 | | - return; |
331 | | - } |
| 298 | + if (mp->nm_context_register_func != nullptr) { |
| 299 | + mp->nm_context_register_func(exports, module, context, mp->nm_priv); |
| 300 | + } else if (mp->nm_register_func != nullptr) { |
| 301 | + mp->nm_register_func(exports, module, mp->nm_priv); |
| 302 | + } else { |
| 303 | + dlib->Close(); |
| 304 | + env->ThrowError("Module has no declared entry point."); |
| 305 | + return false; |
| 306 | + } |
| 307 | + |
| 308 | + return true; |
| 309 | + }); |
332 | 310 |
|
333 | 311 | // Tell coverity that 'handle' should not be freed when we return. |
334 | 312 | // coverity[leaked_storage] |
|
0 commit comments