From d66873871aea4d7e2b41325214049962b45bc670 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 22 Mar 2023 15:13:13 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/47215 Reviewed-By: Chengzhong Wu --- lib/internal/bootstrap/loaders.js | 5 +++-- src/node_builtins.cc | 12 ++++++++++++ src/node_realm.cc | 30 ++---------------------------- src/node_realm.h | 1 - 4 files changed, 17 insertions(+), 31 deletions(-) diff --git a/lib/internal/bootstrap/loaders.js b/lib/internal/bootstrap/loaders.js index 064a735f9a637e..0db70f1cc85687 100644 --- a/lib/internal/bootstrap/loaders.js +++ b/lib/internal/bootstrap/loaders.js @@ -185,6 +185,7 @@ const loaderId = 'internal/bootstrap/loaders'; const { builtinIds, compileFunction, + setInternalLoaders, } = internalBinding('builtins'); const getOwn = (target, property, receiver) => { @@ -378,5 +379,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); diff --git a/src/node_builtins.cc b/src/node_builtins.cc index 363586d4b197f8..663a693bb4696f 100644 --- a/src/node_builtins.cc +++ b/src/node_builtins.cc @@ -666,6 +666,16 @@ void BuiltinLoader::HasCachedBuiltins(const FunctionCallbackInfo& args) { args.GetIsolate(), instance->code_cache_->has_code_cache)); } +void SetInternalLoaders(const FunctionCallbackInfo& 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()); + realm->set_builtin_module_require(args[1].As()); +} + void BuiltinLoader::CopySourceAndCodeCacheReferenceFrom( const BuiltinLoader* other) { code_cache_ = other->code_cache_; @@ -704,6 +714,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 target, @@ -722,6 +733,7 @@ void BuiltinLoader::RegisterExternalReferences( registry->Register(GetCacheUsage); registry->Register(CompileFunction); registry->Register(HasCachedBuiltins); + registry->Register(SetInternalLoaders); } } // namespace builtins diff --git a/src/node_realm.cc b/src/node_realm.cc index ed141f3d6f4db2..1903c65f179b54 100644 --- a/src/node_realm.cc +++ b/src/node_realm.cc @@ -10,7 +10,6 @@ namespace node { using v8::Context; using v8::EscapableHandleScope; -using v8::Function; using v8::HandleScope; using v8::Local; using v8::MaybeLocal; @@ -183,31 +182,6 @@ MaybeLocal Realm::ExecuteBootstrapper(const char* id) { return scope.EscapeMaybe(result); } -MaybeLocal Realm::BootstrapInternalLoaders() { - EscapableHandleScope scope(isolate_); - - // Bootstrap internal loaders - Local loader_exports; - if (!ExecuteBootstrapper("internal/bootstrap/loaders") - .ToLocal(&loader_exports)) { - return MaybeLocal(); - } - CHECK(loader_exports->IsObject()); - Local loader_exports_obj = loader_exports.As(); - Local 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()); - Local require = - loader_exports_obj->Get(context(), env_->require_string()) - .ToLocalChecked(); - CHECK(require->IsFunction()); - set_builtin_module_require(require.As()); - - return scope.Escape(loader_exports); -} - MaybeLocal Realm::BootstrapNode() { EscapableHandleScope scope(isolate_); @@ -261,11 +235,11 @@ MaybeLocal Realm::RunBootstrapping() { CHECK(!has_run_bootstrapping_code()); - if (BootstrapInternalLoaders().IsEmpty()) { + Local result; + if (!ExecuteBootstrapper("internal/bootstrap/loaders").ToLocal(&result)) { return MaybeLocal(); } - Local result; if (!BootstrapNode().ToLocal(&result)) { return MaybeLocal(); } diff --git a/src/node_realm.h b/src/node_realm.h index 04129eec47d551..c9377b8ae17f86 100644 --- a/src/node_realm.h +++ b/src/node_realm.h @@ -69,7 +69,6 @@ class Realm : public MemoryRetainer { void DeserializeProperties(const RealmSerializeInfo* info); v8::MaybeLocal ExecuteBootstrapper(const char* id); - v8::MaybeLocal BootstrapInternalLoaders(); v8::MaybeLocal BootstrapNode(); v8::MaybeLocal RunBootstrapping();