From 31058b87854cf9cfd5ee455eded94f371d285bbb Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Fri, 23 May 2025 10:20:14 +0100 Subject: [PATCH] src: reorganize ContextifyFunction methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/58434 Reviewed-By: Joyee Cheung Reviewed-By: Juan José Arboleda Reviewed-By: Darshan Sen --- src/node_contextify.cc | 36 ++++++++++++++++------------------- src/node_contextify.h | 41 ++++++++++++++++++++++------------------ src/node_sea.cc | 20 ++++++++++++++------ src/node_snapshotable.cc | 16 ++++++++++++++-- 4 files changed, 67 insertions(+), 46 deletions(-) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 9f1f7e64ee1..477b8044ed3 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -371,13 +371,11 @@ void ContextifyContext::CreatePerIsolateProperties( IsolateData* isolate_data, Local target) { Isolate* isolate = isolate_data->isolate(); SetMethod(isolate, target, "makeContext", MakeContext); - SetMethod(isolate, target, "compileFunction", CompileFunction); } void ContextifyContext::RegisterExternalReferences( ExternalReferenceRegistry* registry) { registry->Register(MakeContext); - registry->Register(CompileFunction); registry->Register(PropertyQueryCallback); registry->Register(PropertyGetterCallback); registry->Register(PropertySetterCallback); @@ -1161,22 +1159,6 @@ Maybe StoreCodeCacheResult( return JustVoid(); } -// TODO(RaisinTen): Reuse in ContextifyContext::CompileFunction(). -MaybeLocal CompileFunction(Local context, - Local filename, - Local content, - LocalVector* parameters) { - ScriptOrigin script_origin(filename, 0, 0, true); - ScriptCompiler::Source script_source(content, script_origin); - - return ScriptCompiler::CompileFunction(context, - &script_source, - parameters->size(), - parameters->data(), - 0, - nullptr); -} - bool ContextifyScript::InstanceOf(Environment* env, const Local& value) { return !value.IsEmpty() && @@ -1382,7 +1364,19 @@ ContextifyScript::ContextifyScript(Environment* env, Local object) ContextifyScript::~ContextifyScript() {} -void ContextifyContext::CompileFunction( +void ContextifyFunction::RegisterExternalReferences( + ExternalReferenceRegistry* registry) { + registry->Register(CompileFunction); +} + +void ContextifyFunction::CreatePerIsolateProperties( + IsolateData* isolate_data, Local target) { + Isolate* isolate = isolate_data->isolate(); + + SetMethod(isolate, target, "compileFunction", CompileFunction); +} + +void ContextifyFunction::CompileFunction( const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); Isolate* isolate = env->isolate(); @@ -1532,7 +1526,7 @@ static LocalVector GetCJSParameters(IsolateData* data) { return result; } -MaybeLocal ContextifyContext::CompileFunctionAndCacheResult( +MaybeLocal ContextifyFunction::CompileFunctionAndCacheResult( Environment* env, Local parsing_context, ScriptCompiler::Source* source, @@ -1963,6 +1957,7 @@ void CreatePerIsolateProperties(IsolateData* isolate_data, ContextifyContext::CreatePerIsolateProperties(isolate_data, target); ContextifyScript::CreatePerIsolateProperties(isolate_data, target); + ContextifyFunction::CreatePerIsolateProperties(isolate_data, target); SetMethod(isolate, target, "startSigintWatchdog", StartSigintWatchdog); SetMethod(isolate, target, "stopSigintWatchdog", StopSigintWatchdog); @@ -2015,6 +2010,7 @@ static void CreatePerContextProperties(Local target, void RegisterExternalReferences(ExternalReferenceRegistry* registry) { ContextifyContext::RegisterExternalReferences(registry); ContextifyScript::RegisterExternalReferences(registry); + ContextifyFunction::RegisterExternalReferences(registry); registry->Register(CompileFunctionForCJSLoader); registry->Register(StartSigintWatchdog); diff --git a/src/node_contextify.h b/src/node_contextify.h index 1948fddeebc..3a02f083c26 100644 --- a/src/node_contextify.h +++ b/src/node_contextify.h @@ -86,18 +86,6 @@ class ContextifyContext : public BaseObject { static bool IsStillInitializing(const ContextifyContext* ctx); static void MakeContext(const v8::FunctionCallbackInfo& args); static void IsContext(const v8::FunctionCallbackInfo& args); - static void CompileFunction( - const v8::FunctionCallbackInfo& args); - static v8::MaybeLocal CompileFunctionAndCacheResult( - Environment* env, - v8::Local parsing_context, - v8::ScriptCompiler::Source* source, - v8::LocalVector params, - v8::LocalVector context_extensions, - v8::ScriptCompiler::CompileOptions options, - bool produce_cached_data, - v8::Local id_symbol, - const errors::TryCatchScope& try_catch); static v8::Intercepted PropertyQueryCallback( v8::Local property, const v8::PropertyCallbackInfo& args); @@ -177,6 +165,29 @@ class ContextifyScript : public BaseObject { v8::Global script_; }; +class ContextifyFunction final { + public: + static void RegisterExternalReferences(ExternalReferenceRegistry* registry); + static void CreatePerIsolateProperties(IsolateData* isolate_data, + v8::Local target); + + static void CompileFunction(const v8::FunctionCallbackInfo& args); + static v8::MaybeLocal CompileFunctionAndCacheResult( + Environment* env, + v8::Local parsing_context, + v8::ScriptCompiler::Source* source, + v8::LocalVector params, + v8::LocalVector context_extensions, + v8::ScriptCompiler::CompileOptions options, + bool produce_cached_data, + v8::Local id_symbol, + const errors::TryCatchScope& try_catch); + + private: + ContextifyFunction() = delete; + ~ContextifyFunction() = delete; +}; + v8::Maybe StoreCodeCacheResult( Environment* env, v8::Local target, @@ -185,12 +196,6 @@ v8::Maybe StoreCodeCacheResult( bool produce_cached_data, std::unique_ptr new_cached_data); -v8::MaybeLocal CompileFunction( - v8::Local context, - v8::Local filename, - v8::Local content, - v8::LocalVector* parameters); - } // namespace contextify } // namespace node diff --git a/src/node_sea.cc b/src/node_sea.cc index 1f7f3c8a707..65a338e00d4 100644 --- a/src/node_sea.cc +++ b/src/node_sea.cc @@ -41,6 +41,7 @@ using v8::MaybeLocal; using v8::NewStringType; using v8::Object; using v8::ScriptCompiler; +using v8::ScriptOrigin; using v8::String; using v8::Value; @@ -460,16 +461,23 @@ std::optional GenerateCodeCache(std::string_view main_path, FIXED_ONE_BYTE_STRING(isolate, "__filename"), FIXED_ONE_BYTE_STRING(isolate, "__dirname"), }); + ScriptOrigin script_origin(filename, 0, 0, true); + ScriptCompiler::Source script_source(content, script_origin); + MaybeLocal maybe_fn = + ScriptCompiler::CompileFunction(context, + &script_source, + parameters.size(), + parameters.data(), + 0, + nullptr); + Local fn; + if (!maybe_fn.ToLocal(&fn)) { + return std::nullopt; + } // TODO(RaisinTen): Using the V8 code cache prevents us from using `import()` // in the SEA code. Support it. // Refs: https://github.com/nodejs/node/pull/48191#discussion_r1213271430 - Local fn; - if (!contextify::CompileFunction(context, filename, content, ¶meters) - .ToLocal(&fn)) { - return std::nullopt; - } - std::unique_ptr cache{ ScriptCompiler::CreateCodeCacheForFunction(fn)}; std::string code_cache(cache->data, cache->data + cache->length); diff --git a/src/node_snapshotable.cc b/src/node_snapshotable.cc index 7207e68d127..a500204c476 100644 --- a/src/node_snapshotable.cc +++ b/src/node_snapshotable.cc @@ -42,8 +42,11 @@ using v8::HandleScope; using v8::Isolate; using v8::Local; using v8::LocalVector; +using v8::MaybeLocal; using v8::Object; using v8::ObjectTemplate; +using v8::ScriptCompiler; +using v8::ScriptOrigin; using v8::SnapshotCreator; using v8::StartupData; using v8::String; @@ -1487,9 +1490,18 @@ void CompileSerializeMain(const FunctionCallbackInfo& args) { FIXED_ONE_BYTE_STRING(isolate, "__filename"), FIXED_ONE_BYTE_STRING(isolate, "__dirname"), }); + + ScriptOrigin script_origin(filename, 0, 0, true); + ScriptCompiler::Source script_source(source, script_origin); + MaybeLocal maybe_fn = + ScriptCompiler::CompileFunction(context, + &script_source, + parameters.size(), + parameters.data(), + 0, + nullptr); Local fn; - if (contextify::CompileFunction(context, filename, source, ¶meters) - .ToLocal(&fn)) { + if (maybe_fn.ToLocal(&fn)) { args.GetReturnValue().Set(fn); } }