From 4b015979b93429f8f7a3a886fb82412420cc9ef7 Mon Sep 17 00:00:00 2001 From: Sergey Chernyshev Date: Thu, 25 Apr 2024 01:48:55 +0200 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 ++-- graal-nodejs/src/node_builtins.cc | 12 ++++++++ graal-nodejs/src/node_realm.cc | 30 ++----------------- graal-nodejs/src/node_realm.h | 1 - 4 files changed, 17 insertions(+), 31 deletions(-) diff --git a/graal-nodejs/lib/internal/bootstrap/loaders.js b/graal-nodejs/lib/internal/bootstrap/loaders.js index 064a735f9a6..0db70f1cc85 100644 --- a/graal-nodejs/lib/internal/bootstrap/loaders.js +++ b/graal-nodejs/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/graal-nodejs/src/node_builtins.cc b/graal-nodejs/src/node_builtins.cc index 363586d4b19..663a693bb46 100644 --- a/graal-nodejs/src/node_builtins.cc +++ b/graal-nodejs/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/graal-nodejs/src/node_realm.cc b/graal-nodejs/src/node_realm.cc index ed141f3d6f4..1903c65f179 100644 --- a/graal-nodejs/src/node_realm.cc +++ b/graal-nodejs/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/graal-nodejs/src/node_realm.h b/graal-nodejs/src/node_realm.h index 04129eec47d..c9377b8ae17 100644 --- a/graal-nodejs/src/node_realm.h +++ b/graal-nodejs/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();