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