MCPcopy Index your code
hub / github.com/nodejs/node / runScriptInContext

Function runScriptInContext

lib/internal/process/execution.js:470–500  ·  view source on GitHub ↗

* * @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)

Source from the content-addressed store, hash-verified

468 * @returns {void}
469 */
470function 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
502module.exports = {
503 parseAndEvalCommonjsTypeScript,

Callers 1

evalFunctionFunction · 0.85

Calls 8

runScriptInThisContextFunction · 0.85
printResultFunction · 0.85
compileScriptFunction · 0.70
requireFunction · 0.50
_compileMethod · 0.45
onMethod · 0.45
onceMethod · 0.45
offMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…