Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bootstrap: store internal loaders in C++ via a binding #47215

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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());
anonrig marked this conversation as resolved.
Show resolved Hide resolved
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
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