@@ -2147,46 +2147,62 @@ node_module* get_linked_module(const char* name) {
2147
2147
}
2148
2148
2149
2149
struct DLib {
2150
- std::string filename_;
2151
- std::string errmsg_;
2152
- void * handle_;
2153
- int flags_;
2154
-
2155
2150
#ifdef __POSIX__
2156
2151
static const int kDefaultFlags = RTLD_LAZY;
2152
+ #else
2153
+ static const int kDefaultFlags = 0 ;
2154
+ #endif
2157
2155
2158
- bool Open () {
2159
- handle_ = dlopen (filename_.c_str (), flags_);
2160
- if (handle_ != nullptr )
2161
- return true ;
2162
- errmsg_ = dlerror ();
2163
- return false ;
2164
- }
2156
+ inline DLib (const char * filename, int flags)
2157
+ : filename_(filename), flags_(flags), handle_(nullptr ) {}
2165
2158
2166
- void Close () {
2167
- if (handle_ != nullptr )
2168
- dlclose (handle_);
2169
- }
2170
- #else // !__POSIX__
2171
- static const int kDefaultFlags = 0 ;
2159
+ inline bool Open ();
2160
+ inline void Close ();
2161
+
2162
+ const std::string filename_;
2163
+ const int flags_;
2164
+ std::string errmsg_;
2165
+ void * handle_;
2166
+ #ifndef __POSIX__
2172
2167
uv_lib_t lib_;
2168
+ #endif
2173
2169
2174
- bool Open () {
2175
- int ret = uv_dlopen (filename_.c_str (), &lib_);
2176
- if (ret == 0 ) {
2177
- handle_ = static_cast <void *>(lib_.handle );
2178
- return true ;
2179
- }
2180
- errmsg_ = uv_dlerror (&lib_);
2181
- uv_dlclose (&lib_);
2182
- return false ;
2183
- }
2170
+ DISALLOW_COPY_AND_ASSIGN (DLib);
2171
+ };
2172
+
2173
+
2174
+ #ifdef __POSIX__
2175
+ bool DLib::Open () {
2176
+ handle_ = dlopen (filename_.c_str (), flags_);
2177
+ if (handle_ != nullptr )
2178
+ return true ;
2179
+ errmsg_ = dlerror ();
2180
+ return false ;
2181
+ }
2184
2182
2185
- void Close () {
2186
- uv_dlclose (&lib_);
2183
+ void DLib::Close () {
2184
+ if (handle_ == nullptr ) return ;
2185
+ dlclose (handle_);
2186
+ handle_ = nullptr ;
2187
+ }
2188
+ #else // !__POSIX__
2189
+ bool DLib::Open () {
2190
+ int ret = uv_dlopen (filename_.c_str (), &lib_);
2191
+ if (ret == 0 ) {
2192
+ handle_ = static_cast <void *>(lib_.handle );
2193
+ return true ;
2187
2194
}
2195
+ errmsg_ = uv_dlerror (&lib_);
2196
+ uv_dlclose (&lib_);
2197
+ return false ;
2198
+ }
2199
+
2200
+ void DLib::Close () {
2201
+ if (handle_ == nullptr ) return ;
2202
+ uv_dlclose (&lib_);
2203
+ handle_ = nullptr ;
2204
+ }
2188
2205
#endif // !__POSIX__
2189
- };
2190
2206
2191
2207
// DLOpen is process.dlopen(module, filename, flags).
2192
2208
// Used to load 'module.node' dynamically shared objects.
@@ -2196,6 +2212,7 @@ struct DLib {
2196
2212
// cache that's a plain C list or hash table that's shared across contexts?
2197
2213
static void DLOpen (const FunctionCallbackInfo<Value>& args) {
2198
2214
Environment* env = Environment::GetCurrent (args);
2215
+ auto context = env->context ();
2199
2216
2200
2217
CHECK_EQ (modpending, nullptr );
2201
2218
@@ -2205,16 +2222,21 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
2205
2222
}
2206
2223
2207
2224
int32_t flags = DLib::kDefaultFlags ;
2208
- if (args.Length () > 2 && !args[2 ]->Int32Value (env-> context () ).To (&flags)) {
2225
+ if (args.Length () > 2 && !args[2 ]->Int32Value (context).To (&flags)) {
2209
2226
return env->ThrowTypeError (" flag argument must be an integer." );
2210
2227
}
2211
2228
2212
- Local<Object> module =
2213
- args[0 ]->ToObject (env->context ()).ToLocalChecked (); // Cast
2229
+ Local<Object> module;
2230
+ Local<Object> exports;
2231
+ Local<Value> exports_v;
2232
+ if (!args[0 ]->ToObject (context).ToLocal (&module) ||
2233
+ !module->Get (context, env->exports_string ()).ToLocal (&exports_v) ||
2234
+ !exports_v->ToObject (context).ToLocal (&exports)) {
2235
+ return ; // Exception pending.
2236
+ }
2237
+
2214
2238
node::Utf8Value filename (env->isolate (), args[1 ]); // Cast
2215
- DLib dlib;
2216
- dlib.filename_ = *filename;
2217
- dlib.flags_ = flags;
2239
+ DLib dlib (*filename, flags);
2218
2240
bool is_opened = dlib.Open ();
2219
2241
2220
2242
// Objects containing v14 or later modules will have registered themselves
@@ -2229,7 +2251,7 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
2229
2251
#ifdef _WIN32
2230
2252
// Windows needs to add the filename into the error message
2231
2253
errmsg = String::Concat (errmsg,
2232
- args[1 ]->ToString (env-> context () ).ToLocalChecked ());
2254
+ args[1 ]->ToString (context).ToLocalChecked ());
2233
2255
#endif // _WIN32
2234
2256
env->isolate ()->ThrowException (Exception::Error (errmsg));
2235
2257
return ;
@@ -2276,22 +2298,8 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
2276
2298
mp->nm_link = modlist_addon;
2277
2299
modlist_addon = mp;
2278
2300
2279
- Local<String> exports_string = env->exports_string ();
2280
- MaybeLocal<Value> maybe_exports =
2281
- module->Get (env->context (), exports_string);
2282
-
2283
- if (maybe_exports.IsEmpty () ||
2284
- maybe_exports.ToLocalChecked ()->ToObject (env->context ()).IsEmpty ()) {
2285
- dlib.Close ();
2286
- return ;
2287
- }
2288
-
2289
- Local<Object> exports =
2290
- maybe_exports.ToLocalChecked ()->ToObject (env->context ())
2291
- .FromMaybe (Local<Object>());
2292
-
2293
2301
if (mp->nm_context_register_func != nullptr ) {
2294
- mp->nm_context_register_func (exports, module, env-> context () , mp->nm_priv );
2302
+ mp->nm_context_register_func (exports, module, context, mp->nm_priv );
2295
2303
} else if (mp->nm_register_func != nullptr ) {
2296
2304
mp->nm_register_func (exports, module, mp->nm_priv );
2297
2305
} else {
0 commit comments