Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
lib: refactor execution.js
  • Loading branch information
marco-ippolito committed Dec 30, 2024
commit 10198baa7e2383e37bd7ab9320ba459487ec5917
63 changes: 22 additions & 41 deletions lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment thread
marco-ippolito marked this conversation as resolved.

if (shouldLoadESM) {
require('internal/modules/run_main').runEntryPointWithESMLoader(runScript);
return;
return require('internal/modules/run_main').runEntryPointWithESMLoader(evalFunction);
}
runScript();
evalFunction();
}

const exceptionHandlerState = {
Expand Down Expand Up @@ -301,19 +282,19 @@ function evalTypeScript(name, source, breakFirstLine, print, shouldLoadESM = fal
}
}

const evalFunction = () => runScriptInContext(name,
sourceToRun,
breakFirstLine,
print,
module,
baseUrl,
compiledScript,
origModule);
Comment thread
marco-ippolito marked this conversation as resolved.

if (shouldLoadESM) {
return require('internal/modules/run_main').runEntryPointWithESMLoader(
() => runScriptInContext(name,
sourceToRun,
breakFirstLine,
print,
module,
baseUrl,
compiledScript,
origModule));
return require('internal/modules/run_main').runEntryPointWithESMLoader(evalFunction);
}

runScriptInContext(name, sourceToRun, breakFirstLine, print, module, baseUrl, compiledScript, origModule);
evalFunction();
}

/**
Expand Down Expand Up @@ -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) {
Expand Down