* * @param {string} name - The filename of the script. * @param {string} body - The code of the script. * @param {boolean} breakFirstLine Whether to break on the first line * @param {boolean} print If the result should be printed * @param {import('internal/modules/esm/loader').CJSModule} module
(name, body, breakFirstLine, print, module, baseUrl, compiledScript, origModule)
| 468 | * @returns {void} |
| 469 | */ |
| 470 | function runScriptInContext(name, body, breakFirstLine, print, module, baseUrl, compiledScript, origModule) { |
| 471 | // Create wrapper for cache entry |
| 472 | const script = ` |
| 473 | globalThis.module = module; |
| 474 | globalThis.exports = exports; |
| 475 | globalThis.__dirname = __dirname; |
| 476 | globalThis.require = require; |
| 477 | return (main) => main(); |
| 478 | `; |
| 479 | globalThis.__filename = name; |
| 480 | RegExpPrototypeExec(/^/, ''); // Necessary to reset RegExp statics before user code runs. |
| 481 | const result = module._compile(script, `${name}-wrapper`)(() => { |
| 482 | // If the script was already compiled, use it. |
| 483 | return runScriptInThisContext( |
| 484 | compiledScript ?? compileScript(name, body, baseUrl), |
| 485 | true, !!breakFirstLine); |
| 486 | }); |
| 487 | if (print) { |
| 488 | const { log } = require('internal/console/global'); |
| 489 | |
| 490 | const printResult = () => log(result); |
| 491 | |
| 492 | process.on('exit', printResult); |
| 493 | process.once('beforeExit', () => { |
| 494 | printResult(); |
| 495 | process.off('exit', printResult); |
| 496 | }); |
| 497 | } |
| 498 | if (origModule !== undefined) |
| 499 | globalThis.module = origModule; |
| 500 | } |
| 501 | |
| 502 | module.exports = { |
| 503 | parseAndEvalCommonjsTypeScript, |
no test coverage detected
searching dependent graphs…