Skip to content

Commit dfd7e99

Browse files
committed
src: make a Environment-independent proxy class for NativeModuleLoader
This patch splits `NativeModuleLoader` into two parts - a singleton that only relies on v8 and `node::Mutex` and a proxy class for the singleton (`NativeModuleEnv`) that provides limited access to the singleton as well as C++ bindings for the Node.js binary. `NativeModuleLoader` is then no longer aware of `Environment`. PR-URL: #27160 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent 9b6b567 commit dfd7e99

12 files changed

+431
-329
lines changed

node.gyp

+2
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@
463463
'src/node_messaging.cc',
464464
'src/node_metadata.cc',
465465
'src/node_native_module.cc',
466+
'src/node_native_module_env.cc',
466467
'src/node_options.cc',
467468
'src/node_os.cc',
468469
'src/node_perf.cc',
@@ -543,6 +544,7 @@
543544
'src/node_metadata.h',
544545
'src/node_mutex.h',
545546
'src/node_native_module.h',
547+
'src/node_native_module_env.h',
546548
'src/node_object_wrap.h',
547549
'src/node_options.h',
548550
'src/node_options-inl.h',

src/api/environment.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "node_context_data.h"
44
#include "node_errors.h"
55
#include "node_internals.h"
6-
#include "node_native_module.h"
6+
#include "node_native_module_env.h"
77
#include "node_platform.h"
88
#include "node_process.h"
99
#include "node_v8_platform-inl.h"
@@ -351,7 +351,7 @@ Local<Context> NewContext(Isolate* isolate,
351351
};
352352
Local<Value> arguments[] = {context->Global(), exports};
353353
MaybeLocal<Function> maybe_fn =
354-
per_process::native_module_loader.LookupAndCompile(
354+
native_module::NativeModuleEnv::LookupAndCompile(
355355
context, *module, &parameters, nullptr);
356356
if (maybe_fn.IsEmpty()) {
357357
return Local<Context>();

src/node.cc

+7-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "node_errors.h"
3232
#include "node_internals.h"
3333
#include "node_metadata.h"
34-
#include "node_native_module.h"
34+
#include "node_native_module_env.h"
3535
#include "node_options-inl.h"
3636
#include "node_perf.h"
3737
#include "node_platform.h"
@@ -118,8 +118,10 @@
118118

119119
namespace node {
120120

121+
using native_module::NativeModuleEnv;
121122
using options_parser::kAllowedInEnvironment;
122123
using options_parser::kDisallowedInEnvironment;
124+
123125
using v8::Array;
124126
using v8::Boolean;
125127
using v8::Context;
@@ -207,8 +209,7 @@ MaybeLocal<Value> ExecuteBootstrapper(Environment* env,
207209
std::vector<Local<Value>>* arguments) {
208210
EscapableHandleScope scope(env->isolate());
209211
MaybeLocal<Function> maybe_fn =
210-
per_process::native_module_loader.LookupAndCompile(
211-
env->context(), id, parameters, env);
212+
NativeModuleEnv::LookupAndCompile(env->context(), id, parameters, env);
212213

213214
if (maybe_fn.IsEmpty()) {
214215
return MaybeLocal<Value>();
@@ -401,7 +402,7 @@ MaybeLocal<Value> StartMainThreadExecution(Environment* env) {
401402
// To allow people to extend Node in different ways, this hook allows
402403
// one to drop a file lib/_third_party_main.js into the build
403404
// directory which will be executed instead of Node's normal loading.
404-
if (per_process::native_module_loader.Exists("_third_party_main")) {
405+
if (NativeModuleEnv::Exists("_third_party_main")) {
405406
return StartExecution(env, "internal/main/run_third_party_main");
406407
}
407408

@@ -724,6 +725,8 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv,
724725
per_process::metadata.versions.InitializeIntlVersions();
725726
#endif
726727

728+
NativeModuleEnv::InitializeCodeCache();
729+
727730
// We should set node_is_initialized here instead of in node::Start,
728731
// otherwise embedders using node::Init to initialize everything will not be
729732
// able to set it and native modules will not load for them.

src/node_binding.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "node_binding.h"
2+
#include <atomic>
23
#include "env-inl.h"
3-
#include "node_native_module.h"
4+
#include "node_native_module_env.h"
45
#include "util.h"
5-
#include <atomic>
66

77
#if HAVE_OPENSSL
88
#define NODE_BUILTIN_OPENSSL_MODULES(V) V(crypto) V(tls_wrap)
@@ -593,13 +593,13 @@ void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
593593
exports->SetPrototype(env->context(), Null(env->isolate())).FromJust());
594594
DefineConstants(env->isolate(), exports);
595595
} else if (!strcmp(*module_v, "natives")) {
596-
exports = per_process::native_module_loader.GetSourceObject(env->context());
596+
exports = native_module::NativeModuleEnv::GetSourceObject(env->context());
597597
// Legacy feature: process.binding('natives').config contains stringified
598598
// config.gypi
599599
CHECK(exports
600600
->Set(env->context(),
601601
env->config_string(),
602-
per_process::native_module_loader.GetConfigString(
602+
native_module::NativeModuleEnv::GetConfigString(
603603
env->isolate()))
604604
.FromJust());
605605
} else {

src/node_code_cache_stub.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
#include "node_native_module.h"
2+
#include "node_native_module_env.h"
33

44
// This is supposed to be generated by tools/generate_code_cache.js
55
// The stub here is used when configure is run without `--code-cache-path`
@@ -8,8 +8,8 @@ namespace node {
88
namespace native_module {
99

1010
// The generated source code would insert <std::string, UnionString> pairs
11-
// into native_module_loader.code_cache_.
12-
void NativeModuleLoader::LoadCodeCache() {}
11+
// into NativeModuleLoader::instance.code_cache_.
12+
void NativeModuleEnv::InitializeCodeCache() {}
1313

1414
} // namespace native_module
1515
} // namespace node

0 commit comments

Comments
 (0)