summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBen Noordhuis <info@bnoordhuis.nl>2018-02-22 13:35:26 +0100
committerBen Noordhuis <info@bnoordhuis.nl>2018-03-01 13:44:10 +0100
commit66315220aebf69573c682fea7f21531f58dd8c2a (patch)
treebbf74908f334291e96515820c15facf077f0ed75 /src
parent197fbbed553b61dbc614ff194a93602abdec5e2b (diff)
downloadandroid-node-v8-66315220aebf69573c682fea7f21531f58dd8c2a.tar.gz
android-node-v8-66315220aebf69573c682fea7f21531f58dd8c2a.tar.bz2
android-node-v8-66315220aebf69573c682fea7f21531f58dd8c2a.zip
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: https://github.com/nodejs/node/pull/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>
Diffstat (limited to 'src')
-rw-r--r--src/node.cc114
1 files changed, 61 insertions, 53 deletions
diff --git a/src/node.cc b/src/node.cc
index 28d66452f8..6d082caacf 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -2147,46 +2147,62 @@ node_module* get_linked_module(const char* name) {
}
struct DLib {
- std::string filename_;
- std::string errmsg_;
- void* handle_;
- int flags_;
-
#ifdef __POSIX__
static const int kDefaultFlags = RTLD_LAZY;
+#else
+ static const int kDefaultFlags = 0;
+#endif
- bool Open() {
- handle_ = dlopen(filename_.c_str(), flags_);
- if (handle_ != nullptr)
- return true;
- errmsg_ = dlerror();
- return false;
- }
+ inline DLib(const char* filename, int flags)
+ : filename_(filename), flags_(flags), handle_(nullptr) {}
- void Close() {
- if (handle_ != nullptr)
- dlclose(handle_);
- }
-#else // !__POSIX__
- static const int kDefaultFlags = 0;
+ inline bool Open();
+ inline void Close();
+
+ const std::string filename_;
+ const int flags_;
+ std::string errmsg_;
+ void* handle_;
+#ifndef __POSIX__
uv_lib_t lib_;
+#endif
- bool Open() {
- int ret = uv_dlopen(filename_.c_str(), &lib_);
- if (ret == 0) {
- handle_ = static_cast<void*>(lib_.handle);
- return true;
- }
- errmsg_ = uv_dlerror(&lib_);
- uv_dlclose(&lib_);
- return false;
- }
+ DISALLOW_COPY_AND_ASSIGN(DLib);
+};
+
+
+#ifdef __POSIX__
+bool DLib::Open() {
+ handle_ = dlopen(filename_.c_str(), flags_);
+ if (handle_ != nullptr)
+ return true;
+ errmsg_ = dlerror();
+ return false;
+}
- void Close() {
- uv_dlclose(&lib_);
+void DLib::Close() {
+ if (handle_ == nullptr) return;
+ dlclose(handle_);
+ handle_ = nullptr;
+}
+#else // !__POSIX__
+bool DLib::Open() {
+ int ret = uv_dlopen(filename_.c_str(), &lib_);
+ if (ret == 0) {
+ handle_ = static_cast<void*>(lib_.handle);
+ return true;
}
+ errmsg_ = uv_dlerror(&lib_);
+ uv_dlclose(&lib_);
+ return false;
+}
+
+void DLib::Close() {
+ if (handle_ == nullptr) return;
+ uv_dlclose(&lib_);
+ handle_ = nullptr;
+}
#endif // !__POSIX__
-};
// DLOpen is process.dlopen(module, filename, flags).
// Used to load 'module.node' dynamically shared objects.
@@ -2196,6 +2212,7 @@ struct DLib {
// cache that's a plain C list or hash table that's shared across contexts?
static void DLOpen(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
+ auto context = env->context();
CHECK_EQ(modpending, nullptr);
@@ -2205,16 +2222,21 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
}
int32_t flags = DLib::kDefaultFlags;
- if (args.Length() > 2 && !args[2]->Int32Value(env->context()).To(&flags)) {
+ if (args.Length() > 2 && !args[2]->Int32Value(context).To(&flags)) {
return env->ThrowTypeError("flag argument must be an integer.");
}
- Local<Object> module =
- args[0]->ToObject(env->context()).ToLocalChecked(); // Cast
+ Local<Object> module;
+ Local<Object> exports;
+ Local<Value> exports_v;
+ if (!args[0]->ToObject(context).ToLocal(&module) ||
+ !module->Get(context, env->exports_string()).ToLocal(&exports_v) ||
+ !exports_v->ToObject(context).ToLocal(&exports)) {
+ return; // Exception pending.
+ }
+
node::Utf8Value filename(env->isolate(), args[1]); // Cast
- DLib dlib;
- dlib.filename_ = *filename;
- dlib.flags_ = flags;
+ DLib dlib(*filename, flags);
bool is_opened = dlib.Open();
// Objects containing v14 or later modules will have registered themselves
@@ -2229,7 +2251,7 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
#ifdef _WIN32
// Windows needs to add the filename into the error message
errmsg = String::Concat(errmsg,
- args[1]->ToString(env->context()).ToLocalChecked());
+ args[1]->ToString(context).ToLocalChecked());
#endif // _WIN32
env->isolate()->ThrowException(Exception::Error(errmsg));
return;
@@ -2276,22 +2298,8 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {
mp->nm_link = modlist_addon;
modlist_addon = mp;
- Local<String> exports_string = env->exports_string();
- MaybeLocal<Value> maybe_exports =
- module->Get(env->context(), exports_string);
-
- if (maybe_exports.IsEmpty() ||
- maybe_exports.ToLocalChecked()->ToObject(env->context()).IsEmpty()) {
- dlib.Close();
- return;
- }
-
- Local<Object> exports =
- maybe_exports.ToLocalChecked()->ToObject(env->context())
- .FromMaybe(Local<Object>());
-
if (mp->nm_context_register_func != nullptr) {
- mp->nm_context_register_func(exports, module, env->context(), mp->nm_priv);
+ mp->nm_context_register_func(exports, module, context, mp->nm_priv);
} else if (mp->nm_register_func != nullptr) {
mp->nm_register_func(exports, module, mp->nm_priv);
} else {