From 49cfb9d001d41bc1debd5782cc654ed68cbfddd6 Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Thu, 20 Jun 2024 19:07:19 +0100 Subject: [PATCH] src: reset `process.versions` during pre-execution Reset `process.versions` during pre-execution so that it reflects the versions at run-time rather than when the snapshot was taken. PR-URL: https://github.com/nodejs/node/pull/53444 Reviewed-By: James M Snell Reviewed-By: Chengzhong Wu --- src/node_process_object.cc | 64 ++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/src/node_process_object.cc b/src/node_process_object.cc index 274f1f01de8d84..a13f8d15724cb0 100644 --- a/src/node_process_object.cc +++ b/src/node_process_object.cc @@ -78,34 +78,9 @@ static void GetParentProcessId(Local property, info.GetReturnValue().Set(uv_os_getppid()); } -MaybeLocal CreateProcessObject(Realm* realm) { - Isolate* isolate = realm->isolate(); - EscapableHandleScope scope(isolate); - Local context = realm->context(); - - Local process_template = FunctionTemplate::New(isolate); - process_template->SetClassName(realm->env()->process_string()); - Local process_ctor; - Local process; - if (!process_template->GetFunction(context).ToLocal(&process_ctor) || - !process_ctor->NewInstance(context).ToLocal(&process)) { - return MaybeLocal(); - } - - // process[exit_info_private_symbol] - if (process - ->SetPrivate(context, - realm->env()->exit_info_private_symbol(), - realm->env()->exit_info().GetJSArray()) - .IsNothing()) { - return {}; - } - - // process.version - READONLY_PROPERTY( - process, "version", FIXED_ONE_BYTE_STRING(isolate, NODE_VERSION)); +static void SetVersions(Isolate* isolate, Local versions) { + Local context = isolate->GetCurrentContext(); - Local versions = Object::New(isolate); // Node.js version is always on the top READONLY_STRING_PROPERTY( versions, "node", per_process::metadata.versions.node); @@ -138,8 +113,38 @@ MaybeLocal CreateProcessObject(Realm* realm) { v8::ReadOnly) .Check(); } +} + +MaybeLocal CreateProcessObject(Realm* realm) { + Isolate* isolate = realm->isolate(); + EscapableHandleScope scope(isolate); + Local context = realm->context(); + + Local process_template = FunctionTemplate::New(isolate); + process_template->SetClassName(realm->env()->process_string()); + Local process_ctor; + Local process; + if (!process_template->GetFunction(context).ToLocal(&process_ctor) || + !process_ctor->NewInstance(context).ToLocal(&process)) { + return MaybeLocal(); + } + + // process[exit_info_private_symbol] + if (process + ->SetPrivate(context, + realm->env()->exit_info_private_symbol(), + realm->env()->exit_info().GetJSArray()) + .IsNothing()) { + return {}; + } + + // process.version + READONLY_PROPERTY( + process, "version", FIXED_ONE_BYTE_STRING(isolate, NODE_VERSION)); // process.versions + Local versions = Object::New(isolate); + SetVersions(isolate, versions); READONLY_PROPERTY(process, "versions", versions); // process.arch @@ -241,6 +246,11 @@ void PatchProcessObject(const FunctionCallbackInfo& args) { env->owns_process_state() ? DebugPortSetter : nullptr, Local()) .FromJust()); + + // process.versions + Local versions = Object::New(isolate); + SetVersions(isolate, versions); + READONLY_PROPERTY(process, "versions", versions); } void RegisterProcessExternalReferences(ExternalReferenceRegistry* registry) {