Skip to content

Commit c81e114

Browse files
joyeecheunglegendecas
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 3cf65bd commit c81e114

File tree

4 files changed

+17
-34
lines changed

4 files changed

+17
-34
lines changed

lib/internal/bootstrap/loaders.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ const loaderId = 'internal/bootstrap/loaders';
182182
const {
183183
builtinIds,
184184
compileFunction,
185+
setInternalLoaders,
185186
} = internalBinding('builtins');
186187

187188
const getOwn = (target, property, receiver) => {
@@ -373,5 +374,5 @@ function requireWithFallbackInDeps(request) {
373374
return requireBuiltin(request);
374375
}
375376

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

src/node_builtins.cc

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

650+
void SetInternalLoaders(const FunctionCallbackInfo<Value>& args) {
651+
Realm* realm = Realm::GetCurrent(args);
652+
CHECK(args[0]->IsFunction());
653+
CHECK(args[1]->IsFunction());
654+
DCHECK(realm->internal_binding_loader().IsEmpty());
655+
DCHECK(realm->builtin_module_require().IsEmpty());
656+
realm->set_internal_binding_loader(args[0].As<Function>());
657+
realm->set_builtin_module_require(args[1].As<Function>());
658+
}
659+
650660
void BuiltinLoader::CopySourceAndCodeCacheReferenceFrom(
651661
const BuiltinLoader* other) {
652662
code_cache_ = other->code_cache_;
@@ -685,6 +695,7 @@ void BuiltinLoader::CreatePerIsolateProperties(IsolateData* isolate_data,
685695
SetMethod(isolate, proto, "getCacheUsage", BuiltinLoader::GetCacheUsage);
686696
SetMethod(isolate, proto, "compileFunction", BuiltinLoader::CompileFunction);
687697
SetMethod(isolate, proto, "hasCachedBuiltins", HasCachedBuiltins);
698+
SetMethod(isolate, proto, "setInternalLoaders", SetInternalLoaders);
688699
}
689700

690701
void BuiltinLoader::CreatePerContextProperties(Local<Object> target,
@@ -703,6 +714,7 @@ void BuiltinLoader::RegisterExternalReferences(
703714
registry->Register(GetCacheUsage);
704715
registry->Register(CompileFunction);
705716
registry->Register(HasCachedBuiltins);
717+
registry->Register(SetInternalLoaders);
706718
}
707719

708720
} // namespace builtins

src/node_realm.cc

+2-31
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;
@@ -174,42 +173,14 @@ MaybeLocal<Value> Realm::ExecuteBootstrapper(const char* id) {
174173
return scope.EscapeMaybe(result);
175174
}
176175

177-
MaybeLocal<Value> Realm::BootstrapInternalLoaders() {
178-
EscapableHandleScope scope(isolate_);
179-
180-
// Bootstrap internal loaders
181-
Local<Value> loader_exports;
182-
if (!ExecuteBootstrapper("internal/bootstrap/loaders")
183-
.ToLocal(&loader_exports)) {
184-
return MaybeLocal<Value>();
185-
}
186-
CHECK(loader_exports->IsObject());
187-
Local<Object> loader_exports_obj = loader_exports.As<Object>();
188-
Local<Value> internal_binding_loader =
189-
loader_exports_obj->Get(context(), env_->internal_binding_string())
190-
.ToLocalChecked();
191-
CHECK(internal_binding_loader->IsFunction());
192-
set_internal_binding_loader(internal_binding_loader.As<Function>());
193-
Local<Value> require =
194-
loader_exports_obj->Get(context(), env_->require_string())
195-
.ToLocalChecked();
196-
CHECK(require->IsFunction());
197-
set_builtin_module_require(require.As<Function>());
198-
199-
return scope.Escape(loader_exports);
200-
}
201-
202176
MaybeLocal<Value> Realm::RunBootstrapping() {
203177
EscapableHandleScope scope(isolate_);
204178

205179
CHECK(!has_run_bootstrapping_code());
206180

207-
if (BootstrapInternalLoaders().IsEmpty()) {
208-
return MaybeLocal<Value>();
209-
}
210-
211181
Local<Value> result;
212-
if (!BootstrapRealm().ToLocal(&result)) {
182+
if (!ExecuteBootstrapper("internal/bootstrap/loaders").ToLocal(&result) ||
183+
!BootstrapRealm().ToLocal(&result)) {
213184
return MaybeLocal<Value>();
214185
}
215186

src/node_realm.h

-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ class Realm : public MemoryRetainer {
129129
protected:
130130
~Realm();
131131

132-
v8::MaybeLocal<v8::Value> BootstrapInternalLoaders();
133132
virtual v8::MaybeLocal<v8::Value> BootstrapRealm() = 0;
134133

135134
Environment* env_;

0 commit comments

Comments
 (0)