Skip to content

Commit

Permalink
bootstrap: store internal loaders in C++ via a binding
Browse files Browse the repository at this point in the history
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 <legendecas@gmail.com>
  • Loading branch information
joyeecheung authored and sbaayel committed Sep 20, 2023
1 parent 6b9fb23 commit 163d360
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 34 deletions.
5 changes: 3 additions & 2 deletions lib/internal/bootstrap/loaders.js
Expand Up @@ -182,6 +182,7 @@ const loaderId = 'internal/bootstrap/loaders';
const {
builtinIds,
compileFunction,
setInternalLoaders,
} = internalBinding('builtins');

const getOwn = (target, property, receiver) => {
Expand Down Expand Up @@ -373,5 +374,5 @@ function requireWithFallbackInDeps(request) {
return requireBuiltin(request);
}

// Pass the exports back to C++ land for C++ internals to use.
return loaderExports;
// Store the internal loaders in C++.
setInternalLoaders(internalBinding, requireBuiltin);
12 changes: 12 additions & 0 deletions src/node_builtins.cc
Expand Up @@ -647,6 +647,16 @@ void BuiltinLoader::HasCachedBuiltins(const FunctionCallbackInfo<Value>& args) {
args.GetIsolate(), instance->code_cache_->has_code_cache));
}

void SetInternalLoaders(const FunctionCallbackInfo<Value>& args) {
Realm* realm = Realm::GetCurrent(args);
CHECK(args[0]->IsFunction());
CHECK(args[1]->IsFunction());
DCHECK(realm->internal_binding_loader().IsEmpty());
DCHECK(realm->builtin_module_require().IsEmpty());
realm->set_internal_binding_loader(args[0].As<Function>());
realm->set_builtin_module_require(args[1].As<Function>());
}

void BuiltinLoader::CopySourceAndCodeCacheReferenceFrom(
const BuiltinLoader* other) {
code_cache_ = other->code_cache_;
Expand Down Expand Up @@ -685,6 +695,7 @@ void BuiltinLoader::CreatePerIsolateProperties(IsolateData* isolate_data,
SetMethod(isolate, proto, "getCacheUsage", BuiltinLoader::GetCacheUsage);
SetMethod(isolate, proto, "compileFunction", BuiltinLoader::CompileFunction);
SetMethod(isolate, proto, "hasCachedBuiltins", HasCachedBuiltins);
SetMethod(isolate, proto, "setInternalLoaders", SetInternalLoaders);
}

void BuiltinLoader::CreatePerContextProperties(Local<Object> target,
Expand All @@ -703,6 +714,7 @@ void BuiltinLoader::RegisterExternalReferences(
registry->Register(GetCacheUsage);
registry->Register(CompileFunction);
registry->Register(HasCachedBuiltins);
registry->Register(SetInternalLoaders);
}

} // namespace builtins
Expand Down
33 changes: 2 additions & 31 deletions src/node_realm.cc
Expand Up @@ -10,7 +10,6 @@ namespace node {

using v8::Context;
using v8::EscapableHandleScope;
using v8::Function;
using v8::HandleScope;
using v8::Local;
using v8::MaybeLocal;
Expand Down Expand Up @@ -174,42 +173,14 @@ MaybeLocal<Value> Realm::ExecuteBootstrapper(const char* id) {
return scope.EscapeMaybe(result);
}

MaybeLocal<Value> Realm::BootstrapInternalLoaders() {
EscapableHandleScope scope(isolate_);

// Bootstrap internal loaders
Local<Value> loader_exports;
if (!ExecuteBootstrapper("internal/bootstrap/loaders")
.ToLocal(&loader_exports)) {
return MaybeLocal<Value>();
}
CHECK(loader_exports->IsObject());
Local<Object> loader_exports_obj = loader_exports.As<Object>();
Local<Value> internal_binding_loader =
loader_exports_obj->Get(context(), env_->internal_binding_string())
.ToLocalChecked();
CHECK(internal_binding_loader->IsFunction());
set_internal_binding_loader(internal_binding_loader.As<Function>());
Local<Value> require =
loader_exports_obj->Get(context(), env_->require_string())
.ToLocalChecked();
CHECK(require->IsFunction());
set_builtin_module_require(require.As<Function>());

return scope.Escape(loader_exports);
}

MaybeLocal<Value> Realm::RunBootstrapping() {
EscapableHandleScope scope(isolate_);

CHECK(!has_run_bootstrapping_code());

if (BootstrapInternalLoaders().IsEmpty()) {
return MaybeLocal<Value>();
}

Local<Value> result;
if (!BootstrapRealm().ToLocal(&result)) {
if (!ExecuteBootstrapper("internal/bootstrap/loaders").ToLocal(&result) ||
!BootstrapRealm().ToLocal(&result)) {
return MaybeLocal<Value>();
}

Expand Down
1 change: 0 additions & 1 deletion src/node_realm.h
Expand Up @@ -129,7 +129,6 @@ class Realm : public MemoryRetainer {
protected:
~Realm();

v8::MaybeLocal<v8::Value> BootstrapInternalLoaders();
virtual v8::MaybeLocal<v8::Value> BootstrapRealm() = 0;

Environment* env_;
Expand Down

0 comments on commit 163d360

Please sign in to comment.