diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-11-06 00:50:24 +0300 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2019-11-08 06:51:11 +0300 |
commit | e460e14d57869c37b181f1e4839c70d8fa89ffb3 (patch) | |
tree | 9c481c15fbce5e5a5e8413518faf099badb5ae0b /src/node_binding.cc | |
parent | 96db5a271c859eabbf81565e4ce82a93fd037fb9 (diff) |
src: allow adding linked bindings to Environment
This allows manually adding linked bindings to an `Environment`
instance, without having to register modules at program load in
a global namespace.
PR-URL: https://github.com/nodejs/node/pull/30274
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'src/node_binding.cc')
-rw-r--r-- | src/node_binding.cc | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/src/node_binding.cc b/src/node_binding.cc index 3ae361634d1..d083c64d102 100644 --- a/src/node_binding.cc +++ b/src/node_binding.cc @@ -552,13 +552,6 @@ inline struct node_module* FindModule(struct node_module* list, return mp; } -node_module* get_internal_module(const char* name) { - return FindModule(modlist_internal, name, NM_F_INTERNAL); -} -node_module* get_linked_module(const char* name) { - return FindModule(modlist_linked, name, NM_F_LINKED); -} - static Local<Object> InitModule(Environment* env, node_module* mod, Local<String> module) { @@ -586,7 +579,7 @@ void GetInternalBinding(const FunctionCallbackInfo<Value>& args) { node::Utf8Value module_v(env->isolate(), module); Local<Object> exports; - node_module* mod = get_internal_module(*module_v); + node_module* mod = FindModule(modlist_internal, *module_v, NM_F_INTERNAL); if (mod != nullptr) { exports = InitModule(env, mod, module); } else if (!strcmp(*module_v, "constants")) { @@ -619,7 +612,20 @@ void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) { Local<String> module_name = args[0].As<String>(); node::Utf8Value module_name_v(env->isolate(), module_name); - node_module* mod = get_linked_module(*module_name_v); + const char* name = *module_name_v; + node_module* mod = nullptr; + + // Iterate from here to the nearest non-Worker Environment to see if there's + // a linked binding defined locally rather than through the global list. + Environment* cur_env = env; + while (mod == nullptr && cur_env != nullptr) { + Mutex::ScopedLock lock(cur_env->extra_linked_bindings_mutex()); + mod = FindModule(cur_env->extra_linked_bindings_head(), name, NM_F_LINKED); + cur_env = cur_env->worker_parent_env(); + } + + if (mod == nullptr) + mod = FindModule(modlist_linked, name, NM_F_LINKED); if (mod == nullptr) { char errmsg[1024]; |