Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c2baae8

Browse filesBrowse files
marco-ippolitoaduh95
authored andcommitted
lib: refactor execution.js
PR-URL: #56358 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent 8a87e39 commit c2baae8
Copy full SHA for c2baae8

File tree

Expand file treeCollapse file tree

1 file changed

+22
-41
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+22
-41
lines changed
Open diff view settings
Collapse file

‎lib/internal/process/execution.js‎

Copy file name to clipboardExpand all lines: lib/internal/process/execution.js
+22-41Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -84,38 +84,19 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) {
8484
evalModuleEntryPoint(body, print);
8585
}
8686

87-
const runScript = () => {
88-
// Create wrapper for cache entry
89-
const script = `
90-
globalThis.module = module;
91-
globalThis.exports = exports;
92-
globalThis.__dirname = __dirname;
93-
globalThis.require = require;
94-
return (main) => main();
95-
`;
96-
globalThis.__filename = name;
97-
RegExpPrototypeExec(/^/, ''); // Necessary to reset RegExp statics before user code runs.
98-
const result = module._compile(script, `${name}-wrapper`)(() => {
99-
const compiledScript = compileScript(name, body, baseUrl);
100-
return runScriptInThisContext(compiledScript, true, !!breakFirstLine);
101-
});
102-
if (print) {
103-
const { log } = require('internal/console/global');
104-
105-
process.on('exit', () => {
106-
log(result);
107-
});
108-
}
109-
110-
if (origModule !== undefined)
111-
globalThis.module = origModule;
112-
};
87+
const evalFunction = () => runScriptInContext(name,
88+
body,
89+
breakFirstLine,
90+
print,
91+
module,
92+
baseUrl,
93+
undefined,
94+
origModule);
11395

11496
if (shouldLoadESM) {
115-
require('internal/modules/run_main').runEntryPointWithESMLoader(runScript);
116-
return;
97+
return require('internal/modules/run_main').runEntryPointWithESMLoader(evalFunction);
11798
}
118-
runScript();
99+
evalFunction();
119100
}
120101

121102
const exceptionHandlerState = {
@@ -301,19 +282,19 @@ function evalTypeScript(name, source, breakFirstLine, print, shouldLoadESM = fal
301282
}
302283
}
303284

285+
const evalFunction = () => runScriptInContext(name,
286+
sourceToRun,
287+
breakFirstLine,
288+
print,
289+
module,
290+
baseUrl,
291+
compiledScript,
292+
origModule);
293+
304294
if (shouldLoadESM) {
305-
return require('internal/modules/run_main').runEntryPointWithESMLoader(
306-
() => runScriptInContext(name,
307-
sourceToRun,
308-
breakFirstLine,
309-
print,
310-
module,
311-
baseUrl,
312-
compiledScript,
313-
origModule));
295+
return require('internal/modules/run_main').runEntryPointWithESMLoader(evalFunction);
314296
}
315-
316-
runScriptInContext(name, sourceToRun, breakFirstLine, print, module, baseUrl, compiledScript, origModule);
297+
evalFunction();
317298
}
318299

319300
/**
@@ -476,7 +457,7 @@ function runScriptInContext(name, body, breakFirstLine, print, module, baseUrl,
476457
const result = module._compile(script, `${name}-wrapper`)(() => {
477458
// If the script was already compiled, use it.
478459
return runScriptInThisContext(
479-
compiledScript,
460+
compiledScript ?? compileScript(name, body, baseUrl),
480461
true, !!breakFirstLine);
481462
});
482463
if (print) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.