diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-11-06 00:50:24 +0300 |
---|---|---|
committer | Myles Borins <mylesborins@google.com> | 2019-11-17 10:59:11 +0300 |
commit | b744070d74c710ea884512cb2998e7f2bb837a2c (patch) | |
tree | 6c1b2f2774e523b503b1d8696cf1db0dbd4ae7c0 /src/node_binding.cc | |
parent | de68720908bc783cadd811e84de7837fb4fa6759 (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]; |