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 <[email protected]>
  • Loading branch information
joyeecheung authored and RafaelGSS committed Apr 6, 2023
1 parent f8dd97e commit 7fc2c57
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,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 @@ -700,6 +710,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 @@ -718,6 +729,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 7fc2c57

Please sign in to comment.