From eca22a4aa9414c17b988b1fc1bd542821d446b52 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Tue, 24 Dec 2024 18:07:32 +0100 Subject: [PATCH] lib: refactor execution.js PR-URL: https://github.com/nodejs/node/pull/56358 Reviewed-By: James M Snell Reviewed-By: Yagiz Nizipli Reviewed-By: Jacob Smith --- lib/internal/process/execution.js | 65 +++++++++++-------------------- 1 file changed, 23 insertions(+), 42 deletions(-) diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index e8a1a29efcc..f5b19d5a7e8 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -84,38 +84,19 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) { evalModuleEntryPoint(body, print); } - const runScript = () => { - // Create wrapper for cache entry - const script = ` - globalThis.module = module; - globalThis.exports = exports; - globalThis.__dirname = __dirname; - globalThis.require = require; - return (main) => main(); - `; - globalThis.__filename = name; - RegExpPrototypeExec(/^/, ''); // Necessary to reset RegExp statics before user code runs. - const result = module._compile(script, `${name}-wrapper`)(() => { - const compiledScript = compileScript(name, body, baseUrl); - return runScriptInThisContext(compiledScript, true, !!breakFirstLine); - }); - if (print) { - const { log } = require('internal/console/global'); - - process.on('exit', () => { - log(result); - }); - } - - if (origModule !== undefined) - globalThis.module = origModule; - }; + const evalFunction = () => runScriptInContext(name, + body, + breakFirstLine, + print, + module, + baseUrl, + undefined, + origModule); if (shouldLoadESM) { - require('internal/modules/run_main').runEntryPointWithESMLoader(runScript); - return; + return require('internal/modules/run_main').runEntryPointWithESMLoader(evalFunction); } - runScript(); + evalFunction(); } const exceptionHandlerState = { @@ -301,19 +282,19 @@ function evalTypeScript(name, source, breakFirstLine, print, shouldLoadESM = fal } } - if (shouldLoadESM) { - return require('internal/modules/run_main').runEntryPointWithESMLoader( - () => runScriptInContext(name, - sourceToRun, - breakFirstLine, - print, - module, - baseUrl, - compiledScript, - origModule)); - } + const evalFunction = () => runScriptInContext(name, + sourceToRun, + breakFirstLine, + print, + module, + baseUrl, + compiledScript, + origModule); - runScriptInContext(name, sourceToRun, breakFirstLine, print, module, baseUrl, compiledScript, origModule); + if (shouldLoadESM) { + return require('internal/modules/run_main').runEntryPointWithESMLoader(evalFunction); + } + evalFunction(); } /** @@ -476,7 +457,7 @@ function runScriptInContext(name, body, breakFirstLine, print, module, baseUrl, const result = module._compile(script, `${name}-wrapper`)(() => { // If the script was already compiled, use it. return runScriptInThisContext( - compiledScript, + compiledScript ?? compileScript(name, body, baseUrl), true, !!breakFirstLine); }); if (print) {