@@ -2147,46 +2147,62 @@ node_module* get_linked_module(const char* name) {
21472147}
21482148
21492149struct DLib {
2150- std::string filename_;
2151- std::string errmsg_;
2152- void * handle_;
2153- int flags_;
2154-
21552150#ifdef __POSIX__
21562151 static const int kDefaultFlags = RTLD_LAZY;
2152+ #else
2153+ static const int kDefaultFlags = 0 ;
2154+ #endif
21572155
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 ) {}
21652158
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__
21722167 uv_lib_t lib_;
2168+ #endif
21732169
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+ }
21842182
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 ;
21872194 }
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+ }
21882205#endif // !__POSIX__
2189- };
21902206
21912207// DLOpen is process.dlopen(module, filename, flags).
21922208// Used to load 'module.node' dynamically shared objects.
@@ -2196,6 +2212,7 @@ struct DLib {
21962212// cache that's a plain C list or hash table that's shared across contexts?
21972213static void DLOpen (const FunctionCallbackInfo<Value>& args) {
21982214 Environment* env = Environment::GetCurrent (args);
2215+ auto context = env->context ();
21992216
22002217 CHECK_EQ (modpending, nullptr );
22012218
@@ -2205,16 +2222,21 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
22052222 }
22062223
22072224 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)) {
22092226 return env->ThrowTypeError (" flag argument must be an integer." );
22102227 }
22112228
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+
22142238 node::Utf8Value filename (env->isolate (), args[1 ]); // Cast
2215- DLib dlib;
2216- dlib.filename_ = *filename;
2217- dlib.flags_ = flags;
2239+ DLib dlib (*filename, flags);
22182240 bool is_opened = dlib.Open ();
22192241
22202242 // Objects containing v14 or later modules will have registered themselves
@@ -2229,7 +2251,7 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
22292251#ifdef _WIN32
22302252 // Windows needs to add the filename into the error message
22312253 errmsg = String::Concat (errmsg,
2232- args[1 ]->ToString (env-> context () ).ToLocalChecked ());
2254+ args[1 ]->ToString (context).ToLocalChecked ());
22332255#endif // _WIN32
22342256 env->isolate ()->ThrowException (Exception::Error (errmsg));
22352257 return ;
@@ -2276,22 +2298,8 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
22762298 mp->nm_link = modlist_addon;
22772299 modlist_addon = mp;
22782300
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-
22932301 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 );
22952303 } else if (mp->nm_register_func != nullptr ) {
22962304 mp->nm_register_func (exports, module , mp->nm_priv );
22972305 } else {
0 commit comments