Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 89edbae

Browse filesBrowse files
bnoordhuisMylesBorins
authored andcommitted
src: clean up process.dlopen()
Move some code around and clean up the DLib helper class as prep work for a follow-up commit. PR-URL: #18934 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Matheus Marchini <matheus@sthima.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 5bbf009 commit 89edbae
Copy full SHA for 89edbae

File tree

Expand file treeCollapse file tree

1 file changed

+61
-53
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+61
-53
lines changed
Open diff view settings
Collapse file

‎src/node.cc‎

Copy file name to clipboardExpand all lines: src/node.cc
+61-53Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,46 +2472,62 @@ node_module* get_linked_module(const char* name) {
24722472
}
24732473

24742474
struct DLib {
2475-
std::string filename_;
2476-
std::string errmsg_;
2477-
void* handle_;
2478-
int flags_;
2479-
24802475
#ifdef __POSIX__
24812476
static const int kDefaultFlags = RTLD_LAZY;
2477+
#else
2478+
static const int kDefaultFlags = 0;
2479+
#endif
24822480

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) {}
24902483

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__
24972492
uv_lib_t lib_;
2493+
#endif
24982494

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+
}
25092507

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;
25122519
}
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+
}
25132530
#endif // !__POSIX__
2514-
};
25152531

25162532
// DLOpen is process.dlopen(module, filename, flags).
25172533
// Used to load 'module.node' dynamically shared objects.
@@ -2521,6 +2537,7 @@ struct DLib {
25212537
// cache that's a plain C list or hash table that's shared across contexts?
25222538
static void DLOpen(const FunctionCallbackInfo<Value>& args) {
25232539
Environment* env = Environment::GetCurrent(args);
2540+
auto context = env->context();
25242541

25252542
CHECK_EQ(modpending, nullptr);
25262543

@@ -2530,16 +2547,21 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
25302547
}
25312548

25322549
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)) {
25342551
return env->ThrowTypeError("flag argument must be an integer.");
25352552
}
25362553

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+
25392563
node::Utf8Value filename(env->isolate(), args[1]); // Cast
2540-
DLib dlib;
2541-
dlib.filename_ = *filename;
2542-
dlib.flags_ = flags;
2564+
DLib dlib(*filename, flags);
25432565
bool is_opened = dlib.Open();
25442566

25452567
// Objects containing v14 or later modules will have registered themselves
@@ -2554,7 +2576,7 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
25542576
#ifdef _WIN32
25552577
// Windows needs to add the filename into the error message
25562578
errmsg = String::Concat(errmsg,
2557-
args[1]->ToString(env->context()).ToLocalChecked());
2579+
args[1]->ToString(context).ToLocalChecked());
25582580
#endif // _WIN32
25592581
env->isolate()->ThrowException(Exception::Error(errmsg));
25602582
return;
@@ -2601,22 +2623,8 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
26012623
mp->nm_link = modlist_addon;
26022624
modlist_addon = mp;
26032625

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-
26182626
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);
26202628
} else if (mp->nm_register_func != nullptr) {
26212629
mp->nm_register_func(exports, module, mp->nm_priv);
26222630
} else {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.