@@ -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
121102const 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