GeezerVison is showing github.com — lib: refactor execution.js · nodejs/node@c2baae8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/execution.js +22-41Lines changed: 22 additions & 41 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -84,38 +84,19 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) { | 84 | 84 | evalModuleEntryPoint(body, print); | 85 | 85 | } | 86 | 86 |
| 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); | 113 | 95 |
| 114 | 96 | if (shouldLoadESM) { | 115 | | - require('internal/modules/run_main').runEntryPointWithESMLoader(runScript); | 116 | | - return; | | 97 | + return require('internal/modules/run_main').runEntryPointWithESMLoader(evalFunction); | 117 | 98 | } | 118 | | - runScript(); | | 99 | + evalFunction(); | 119 | 100 | } | 120 | 101 |
| 121 | 102 | const exceptionHandlerState = { | @@ -301,19 +282,19 @@ function evalTypeScript(name, source, breakFirstLine, print, shouldLoadESM = fal | 301 | 282 | } | 302 | 283 | } | 303 | 284 |
| | 285 | + const evalFunction = () => runScriptInContext(name, | | 286 | + sourceToRun, | | 287 | + breakFirstLine, | | 288 | + print, | | 289 | + module, | | 290 | + baseUrl, | | 291 | + compiledScript, | | 292 | + origModule); | | 293 | + | 304 | 294 | 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); | 314 | 296 | } | 315 | | - | 316 | | - runScriptInContext(name, sourceToRun, breakFirstLine, print, module, baseUrl, compiledScript, origModule); | | 297 | + evalFunction(); | 317 | 298 | } | 318 | 299 |
| 319 | 300 | /** | @@ -476,7 +457,7 @@ function runScriptInContext(name, body, breakFirstLine, print, module, baseUrl, | 476 | 457 | const result = module._compile(script, `${name}-wrapper`)(() => { | 477 | 458 | // If the script was already compiled, use it. | 478 | 459 | return runScriptInThisContext( | 479 | | - compiledScript, | | 460 | + compiledScript ?? compileScript(name, body, baseUrl), | 480 | 461 | true, !!breakFirstLine); | 481 | 462 | }); | 482 | 463 | if (print) { | |
|