Skip to content

Commit c2baae8

Browse files
marco-ippolitoaduh95
authored andcommitted
lib: refactor execution.js
PR-URL: #56358 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
1 parent 8a87e39 commit c2baae8

File tree

1 file changed

+22
-41
lines changed

1 file changed

+22
-41
lines changed

‎lib/internal/process/execution.js‎

Lines 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-
construnScript=()=>{
88-
// Create wrapper for cache entry
89-
constscript=`
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-
constresult=module._compile(script,`${name}-wrapper`)(()=>{
99-
constcompiledScript=compileScript(name,body,baseUrl);
100-
returnrunScriptInThisContext(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+
constevalFunction=()=>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+
returnrequire('internal/modules/run_main').runEntryPointWithESMLoader(evalFunction);
11798
}
118-
runScript();
99+
evalFunction();
119100
}
120101

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

285+
constevalFunction=()=>runScriptInContext(name,
286+
sourceToRun,
287+
breakFirstLine,
288+
print,
289+
module,
290+
baseUrl,
291+
compiledScript,
292+
origModule);
293+
304294
if(shouldLoadESM){
305-
returnrequire('internal/modules/run_main').runEntryPointWithESMLoader(
306-
()=>runScriptInContext(name,
307-
sourceToRun,
308-
breakFirstLine,
309-
print,
310-
module,
311-
baseUrl,
312-
compiledScript,
313-
origModule));
295+
returnrequire('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
constresult=module._compile(script,`${name}-wrapper`)(()=>{
477458
// If the script was already compiled, use it.
478459
returnrunScriptInThisContext(
479-
compiledScript,
460+
compiledScript??compileScript(name,body,baseUrl),
480461
true,!!breakFirstLine);
481462
});
482463
if(print){

0 commit comments

Comments
(0)