Skip to content

Commit d668738

Browse files
joyeecheungtargos
authored andcommitted
bootstrap: store internal loaders in C++ via a binding
Instead of returning the internal loaders from the bootstrap script, we can simply call a binding to store them in C++. This eliminates the need for specializing the handling of this script. PR-URL: #47215 Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 3551a19 commit d668738

File tree

4 files changed

+17
-31
lines changed

4 files changed

+17
-31
lines changed

lib/internal/bootstrap/loaders.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ const loaderId = 'internal/bootstrap/loaders';
185185
const {
186186
builtinIds,
187187
compileFunction,
188+
setInternalLoaders,
188189
} = internalBinding('builtins');
189190

190191
const getOwn = (target, property, receiver) => {
@@ -378,5 +379,5 @@ function requireWithFallbackInDeps(request) {
378379
return requireBuiltin(request);
379380
}
380381

381-
// Pass the exports back to C++ land for C++ internals to use.
382-
return loaderExports;
382+
// Store the internal loaders in C++.
383+
setInternalLoaders(internalBinding, requireBuiltin);

src/node_builtins.cc

+12
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,16 @@ void BuiltinLoader::HasCachedBuiltins(const FunctionCallbackInfo<Value>& args) {
666666
args.GetIsolate(), instance->code_cache_->has_code_cache));
667667
}
668668

669+
void SetInternalLoaders(const FunctionCallbackInfo<Value>& args) {
670+
Realm* realm = Realm::GetCurrent(args);
671+
CHECK(args[0]->IsFunction());
672+
CHECK(args[1]->IsFunction());
673+
DCHECK(realm->internal_binding_loader().IsEmpty());
674+
DCHECK(realm->builtin_module_require().IsEmpty());
675+
realm->set_internal_binding_loader(args[0].As<Function>());
676+
realm->set_builtin_module_require(args[1].As<Function>());
677+
}
678+
669679
void BuiltinLoader::CopySourceAndCodeCacheReferenceFrom(
670680
const BuiltinLoader* other) {
671681
code_cache_ = other->code_cache_;
@@ -704,6 +714,7 @@ void BuiltinLoader::CreatePerIsolateProperties(IsolateData* isolate_data,
704714
SetMethod(isolate, proto, "getCacheUsage", BuiltinLoader::GetCacheUsage);
705715
SetMethod(isolate, proto, "compileFunction", BuiltinLoader::CompileFunction);
706716
SetMethod(isolate, proto, "hasCachedBuiltins", HasCachedBuiltins);
717+
SetMethod(isolate, proto, "setInternalLoaders", SetInternalLoaders);
707718
}
708719

709720
void BuiltinLoader::CreatePerContextProperties(Local<Object> target,
@@ -722,6 +733,7 @@ void BuiltinLoader::RegisterExternalReferences(
722733
registry->Register(GetCacheUsage);
723734
registry->Register(CompileFunction);
724735
registry->Register(HasCachedBuiltins);
736+
registry->Register(SetInternalLoaders);
725737
}
726738

727739
} // namespace builtins

src/node_realm.cc

+2-28
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace node {
1010

1111
using v8::Context;
1212
using v8::EscapableHandleScope;
13-
using v8::Function;
1413
using v8::HandleScope;
1514
using v8::Local;
1615
using v8::MaybeLocal;
@@ -183,31 +182,6 @@ MaybeLocal<Value> Realm::ExecuteBootstrapper(const char* id) {
183182
return scope.EscapeMaybe(result);
184183
}
185184

186-
MaybeLocal<Value> Realm::BootstrapInternalLoaders() {
187-
EscapableHandleScope scope(isolate_);
188-
189-
// Bootstrap internal loaders
190-
Local<Value> loader_exports;
191-
if (!ExecuteBootstrapper("internal/bootstrap/loaders")
192-
.ToLocal(&loader_exports)) {
193-
return MaybeLocal<Value>();
194-
}
195-
CHECK(loader_exports->IsObject());
196-
Local<Object> loader_exports_obj = loader_exports.As<Object>();
197-
Local<Value> internal_binding_loader =
198-
loader_exports_obj->Get(context(), env_->internal_binding_string())
199-
.ToLocalChecked();
200-
CHECK(internal_binding_loader->IsFunction());
201-
set_internal_binding_loader(internal_binding_loader.As<Function>());
202-
Local<Value> require =
203-
loader_exports_obj->Get(context(), env_->require_string())
204-
.ToLocalChecked();
205-
CHECK(require->IsFunction());
206-
set_builtin_module_require(require.As<Function>());
207-
208-
return scope.Escape(loader_exports);
209-
}
210-
211185
MaybeLocal<Value> Realm::BootstrapNode() {
212186
EscapableHandleScope scope(isolate_);
213187

@@ -261,11 +235,11 @@ MaybeLocal<Value> Realm::RunBootstrapping() {
261235

262236
CHECK(!has_run_bootstrapping_code());
263237

264-
if (BootstrapInternalLoaders().IsEmpty()) {
238+
Local<Value> result;
239+
if (!ExecuteBootstrapper("internal/bootstrap/loaders").ToLocal(&result)) {
265240
return MaybeLocal<Value>();
266241
}
267242

268-
Local<Value> result;
269243
if (!BootstrapNode().ToLocal(&result)) {
270244
return MaybeLocal<Value>();
271245
}

src/node_realm.h

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ class Realm : public MemoryRetainer {
6969
void DeserializeProperties(const RealmSerializeInfo* info);
7070

7171
v8::MaybeLocal<v8::Value> ExecuteBootstrapper(const char* id);
72-
v8::MaybeLocal<v8::Value> BootstrapInternalLoaders();
7372
v8::MaybeLocal<v8::Value> BootstrapNode();
7473
v8::MaybeLocal<v8::Value> RunBootstrapping();
7574

0 commit comments

Comments
 (0)