@@ -2472,46 +2472,62 @@ node_module* get_linked_module(const char* name) {
2472
2472
}
2473
2473
2474
2474
struct DLib {
2475
- std::string filename_;
2476
- std::string errmsg_;
2477
- void * handle_;
2478
- int flags_;
2479
-
2480
2475
#ifdef __POSIX__
2481
2476
static const int kDefaultFlags = RTLD_LAZY;
2477
+ #else
2478
+ static const int kDefaultFlags = 0 ;
2479
+ #endif
2482
2480
2483
- bool Open () {
2484
- handle_ = dlopen (filename_.c_str (), flags_);
2485
- if (handle_ != nullptr )
2486
- return true ;
2487
- errmsg_ = dlerror ();
2488
- return false ;
2489
- }
2481
+ inline DLib (const char * filename, int flags)
2482
+ : filename_(filename), flags_(flags), handle_(nullptr ) {}
2490
2483
2491
- void Close () {
2492
- if (handle_ != nullptr )
2493
- dlclose (handle_);
2494
- }
2495
- #else // !__POSIX__
2496
- static const int kDefaultFlags = 0 ;
2484
+ inline bool Open ();
2485
+ inline void Close ();
2486
+
2487
+ const std::string filename_;
2488
+ const int flags_;
2489
+ std::string errmsg_;
2490
+ void * handle_;
2491
+ #ifndef __POSIX__
2497
2492
uv_lib_t lib_;
2493
+ #endif
2498
2494
2499
- bool Open () {
2500
- int ret = uv_dlopen (filename_.c_str (), &lib_);
2501
- if (ret == 0 ) {
2502
- handle_ = static_cast <void *>(lib_.handle );
2503
- return true ;
2504
- }
2505
- errmsg_ = uv_dlerror (&lib_);
2506
- uv_dlclose (&lib_);
2507
- return false ;
2508
- }
2495
+ DISALLOW_COPY_AND_ASSIGN (DLib);
2496
+ };
2497
+
2498
+
2499
+ #ifdef __POSIX__
2500
+ bool DLib::Open () {
2501
+ handle_ = dlopen (filename_.c_str (), flags_);
2502
+ if (handle_ != nullptr )
2503
+ return true ;
2504
+ errmsg_ = dlerror ();
2505
+ return false ;
2506
+ }
2509
2507
2510
- void Close () {
2511
- uv_dlclose (&lib_);
2508
+ void DLib::Close () {
2509
+ if (handle_ == nullptr ) return ;
2510
+ dlclose (handle_);
2511
+ handle_ = nullptr ;
2512
+ }
2513
+ #else // !__POSIX__
2514
+ bool DLib::Open () {
2515
+ int ret = uv_dlopen (filename_.c_str (), &lib_);
2516
+ if (ret == 0 ) {
2517
+ handle_ = static_cast <void *>(lib_.handle );
2518
+ return true ;
2512
2519
}
2520
+ errmsg_ = uv_dlerror (&lib_);
2521
+ uv_dlclose (&lib_);
2522
+ return false ;
2523
+ }
2524
+
2525
+ void DLib::Close () {
2526
+ if (handle_ == nullptr ) return ;
2527
+ uv_dlclose (&lib_);
2528
+ handle_ = nullptr ;
2529
+ }
2513
2530
#endif // !__POSIX__
2514
- };
2515
2531
2516
2532
// DLOpen is process.dlopen(module, filename, flags).
2517
2533
// Used to load 'module.node' dynamically shared objects.
@@ -2521,6 +2537,7 @@ struct DLib {
2521
2537
// cache that's a plain C list or hash table that's shared across contexts?
2522
2538
static void DLOpen (const FunctionCallbackInfo<Value>& args) {
2523
2539
Environment* env = Environment::GetCurrent (args);
2540
+ auto context = env->context ();
2524
2541
2525
2542
CHECK_EQ (modpending, nullptr );
2526
2543
@@ -2530,16 +2547,21 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
2530
2547
}
2531
2548
2532
2549
int32_t flags = DLib::kDefaultFlags ;
2533
- if (args.Length () > 2 && !args[2 ]->Int32Value (env-> context () ).To (&flags)) {
2550
+ if (args.Length () > 2 && !args[2 ]->Int32Value (context).To (&flags)) {
2534
2551
return env->ThrowTypeError (" flag argument must be an integer." );
2535
2552
}
2536
2553
2537
- Local<Object> module =
2538
- args[0 ]->ToObject (env->context ()).ToLocalChecked (); // Cast
2554
+ Local<Object> module;
2555
+ Local<Object> exports;
2556
+ Local<Value> exports_v;
2557
+ if (!args[0 ]->ToObject (context).ToLocal (&module) ||
2558
+ !module->Get (context, env->exports_string ()).ToLocal (&exports_v) ||
2559
+ !exports_v->ToObject (context).ToLocal (&exports)) {
2560
+ return ; // Exception pending.
2561
+ }
2562
+
2539
2563
node::Utf8Value filename (env->isolate (), args[1 ]); // Cast
2540
- DLib dlib;
2541
- dlib.filename_ = *filename;
2542
- dlib.flags_ = flags;
2564
+ DLib dlib (*filename, flags);
2543
2565
bool is_opened = dlib.Open ();
2544
2566
2545
2567
// Objects containing v14 or later modules will have registered themselves
@@ -2554,7 +2576,7 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
2554
2576
#ifdef _WIN32
2555
2577
// Windows needs to add the filename into the error message
2556
2578
errmsg = String::Concat (errmsg,
2557
- args[1 ]->ToString (env-> context () ).ToLocalChecked ());
2579
+ args[1 ]->ToString (context).ToLocalChecked ());
2558
2580
#endif // _WIN32
2559
2581
env->isolate ()->ThrowException (Exception::Error (errmsg));
2560
2582
return ;
@@ -2601,22 +2623,8 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
2601
2623
mp->nm_link = modlist_addon;
2602
2624
modlist_addon = mp;
2603
2625
2604
- Local<String> exports_string = env->exports_string ();
2605
- MaybeLocal<Value> maybe_exports =
2606
- module->Get (env->context (), exports_string);
2607
-
2608
- if (maybe_exports.IsEmpty () ||
2609
- maybe_exports.ToLocalChecked ()->ToObject (env->context ()).IsEmpty ()) {
2610
- dlib.Close ();
2611
- return ;
2612
- }
2613
-
2614
- Local<Object> exports =
2615
- maybe_exports.ToLocalChecked ()->ToObject (env->context ())
2616
- .FromMaybe (Local<Object>());
2617
-
2618
2626
if (mp->nm_context_register_func != nullptr ) {
2619
- mp->nm_context_register_func (exports, module, env-> context () , mp->nm_priv );
2627
+ mp->nm_context_register_func (exports, module, context, mp->nm_priv );
2620
2628
} else if (mp->nm_register_func != nullptr ) {
2621
2629
mp->nm_register_func (exports, module, mp->nm_priv );
2622
2630
} else {
0 commit comments