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

Method defaultEval

lib/repl.js:451–684  ·  view source on GitHub ↗
(code, context, file, cb)

Source from the content-addressed store, hash-verified

449 }
450
451 function defaultEval(code, context, file, cb) {
452 let result, script, wrappedErr;
453 let err = null;
454 let wrappedCmd = false;
455 let awaitPromise = false;
456 const input = code;
457
458 if (isObjectLiteral(code) && isValidSyntax(code)) {
459 // Add parentheses to make sure `code` is parsed as an expression
460 code = `(${StringPrototypeTrim(code)})\n`;
461 wrappedCmd = true;
462 }
463
464 const hostDefinedOptionId = Symbol(`eval:${file}`);
465 let parentURL;
466 try {
467 const { pathToFileURL } = require('internal/url');
468 // Adding `/repl` prevents dynamic imports from loading relative
469 // to the parent of `process.cwd()`.
470 parentURL = pathToFileURL(path.join(process.cwd(), 'repl')).href;
471 } catch {
472 // Continue regardless of error.
473 }
474 async function importModuleDynamically(specifier, _, importAttributes, phase) {
475 const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
476 return cascadedLoader.import(specifier, parentURL, importAttributes,
477 phase === 'evaluation' ? cascadedLoader.kEvaluationPhase :
478 cascadedLoader.kSourcePhase);
479 }
480 // `experimentalREPLAwait` is set to true by default.
481 // Shall be false in case `--no-experimental-repl-await` flag is used.
482 if (experimentalREPLAwait && StringPrototypeIncludes(code, 'await')) {
483 if (processTopLevelAwait === undefined) {
484 ({ processTopLevelAwait } = require('internal/repl/await'));
485 }
486
487 try {
488 const potentialWrappedCode = processTopLevelAwait(code);
489 if (potentialWrappedCode !== null) {
490 code = potentialWrappedCode;
491 wrappedCmd = true;
492 awaitPromise = true;
493 }
494 } catch (e) {
495 let recoverableError = false;
496 if (e.name === 'SyntaxError') {
497 // Remove all "await"s and attempt running the script
498 // in order to detect if error is truly non recoverable
499 const fallbackCode = SideEffectFreeRegExpPrototypeSymbolReplace(/\bawait\b/g, code, '');
500 try {
501 makeContextifyScript(
502 fallbackCode, // code
503 file, // filename,
504 0, // lineOffset
505 0, // columnOffset,
506 undefined, // cachedData
507 false, // produceCachedData
508 undefined, // parsingContext

Callers

nothing calls this directly

Calls 15

isObjectLiteralFunction · 0.85
isValidSyntaxFunction · 0.85
processTopLevelAwaitFunction · 0.85
makeContextifyScriptFunction · 0.85
isRecoverableErrorFunction · 0.85
decorateErrorStackFunction · 0.85
listenerCountMethod · 0.80
_handleErrorMethod · 0.80
pathToFileURLFunction · 0.70
addMethod · 0.65

Tested by

no test coverage detected