2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
|
// following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
2020-08-05 22:03:54 -07:00
|
|
|
/* A REPL library that you can include in your own code to get a runtime
|
2010-12-01 18:07:20 -08:00
|
|
|
* interface to your program.
|
|
|
|
|
*
|
2019-09-16 07:09:26 -04:00
|
|
|
* const repl = require("repl");
|
2010-12-01 18:07:20 -08:00
|
|
|
* // start repl on stdin
|
|
|
|
|
* repl.start("prompt> ");
|
|
|
|
|
*
|
|
|
|
|
* // listen for unix socket connections and start repl on them
|
2012-02-18 15:01:35 -08:00
|
|
|
* net.createServer(function(socket) {
|
2010-12-01 18:07:20 -08:00
|
|
|
* repl.start("node via Unix socket> ", socket);
|
|
|
|
|
* }).listen("/tmp/node-repl-sock");
|
|
|
|
|
*
|
|
|
|
|
* // listen for TCP socket connections and start repl on them
|
2012-02-18 15:01:35 -08:00
|
|
|
* net.createServer(function(socket) {
|
2010-12-01 18:07:20 -08:00
|
|
|
* repl.start("node via TCP socket> ", socket);
|
|
|
|
|
* }).listen(5001);
|
|
|
|
|
*
|
|
|
|
|
* // expose foo to repl context
|
|
|
|
|
* repl.start("node > ").context.foo = "stdin is fun";
|
|
|
|
|
*/
|
2009-09-24 00:56:24 +02:00
|
|
|
|
2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
const {
|
2023-06-11 01:10:39 -07:00
|
|
|
ArrayPrototypeAt,
|
2020-11-16 11:12:25 +01:00
|
|
|
ArrayPrototypeFilter,
|
2023-11-22 01:47:32 +08:00
|
|
|
ArrayPrototypeFindLastIndex,
|
2020-12-28 20:04:04 +05:30
|
|
|
ArrayPrototypeForEach,
|
2020-11-16 11:12:25 +01:00
|
|
|
ArrayPrototypeJoin,
|
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
|
ArrayPrototypePop,
|
|
|
|
|
ArrayPrototypePush,
|
|
|
|
|
ArrayPrototypeShift,
|
2021-02-02 02:10:00 +01:00
|
|
|
ArrayPrototypeSlice,
|
2020-11-16 11:12:25 +01:00
|
|
|
ArrayPrototypeSort,
|
|
|
|
|
Boolean,
|
2023-11-22 01:47:32 +08:00
|
|
|
Error: MainContextError,
|
2020-11-16 11:12:25 +01:00
|
|
|
FunctionPrototypeBind,
|
2025-11-23 16:45:46 +08:00
|
|
|
FunctionPrototypeCall,
|
2023-06-11 01:10:39 -07:00
|
|
|
JSONStringify,
|
2021-02-12 12:59:21 +01:00
|
|
|
MathMaxApply,
|
2019-11-27 19:59:29 +01:00
|
|
|
NumberIsNaN,
|
2020-09-28 16:42:29 +02:00
|
|
|
NumberParseFloat,
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectAssign,
|
|
|
|
|
ObjectDefineProperty,
|
|
|
|
|
ObjectGetOwnPropertyDescriptor,
|
|
|
|
|
ObjectGetOwnPropertyNames,
|
|
|
|
|
ObjectKeys,
|
2019-12-13 16:46:35 +01:00
|
|
|
Promise,
|
2020-11-16 11:12:25 +01:00
|
|
|
ReflectApply,
|
2020-01-06 03:48:14 +01:00
|
|
|
RegExp,
|
2020-11-16 11:12:25 +01:00
|
|
|
RegExpPrototypeExec,
|
2022-07-12 23:50:35 +02:00
|
|
|
SafePromiseRace,
|
2020-11-16 11:12:25 +01:00
|
|
|
SafeSet,
|
|
|
|
|
SafeWeakSet,
|
2020-09-28 16:42:29 +02:00
|
|
|
StringPrototypeCharAt,
|
2020-11-16 11:12:25 +01:00
|
|
|
StringPrototypeEndsWith,
|
2020-05-27 12:05:55 -04:00
|
|
|
StringPrototypeIncludes,
|
2020-11-16 11:12:25 +01:00
|
|
|
StringPrototypeRepeat,
|
|
|
|
|
StringPrototypeSlice,
|
|
|
|
|
StringPrototypeStartsWith,
|
|
|
|
|
StringPrototypeTrim,
|
2019-11-30 16:55:29 +01:00
|
|
|
Symbol,
|
2020-09-28 16:35:42 +02:00
|
|
|
SyntaxError,
|
2021-04-13 18:54:09 +02:00
|
|
|
globalThis,
|
2019-11-22 18:04:46 +01:00
|
|
|
} = primordials;
|
2019-03-31 13:30:12 +02:00
|
|
|
|
2018-03-07 02:30:18 +08:00
|
|
|
const {
|
|
|
|
|
makeRequireFunction,
|
2023-02-24 09:43:27 +01:00
|
|
|
addBuiltinLibsToObject,
|
2022-12-13 22:24:28 +01:00
|
|
|
} = require('internal/modules/helpers');
|
2018-06-27 12:57:57 +08:00
|
|
|
const {
|
2023-06-11 01:10:39 -07:00
|
|
|
parse: acornParse,
|
2019-01-31 08:36:48 +01:00
|
|
|
} = require('internal/deps/acorn/acorn/dist/acorn');
|
2023-06-11 01:10:39 -07:00
|
|
|
const acornWalk = require('internal/deps/acorn/acorn-walk/dist/walk');
|
2019-03-21 10:24:13 +01:00
|
|
|
const {
|
|
|
|
|
decorateErrorStack,
|
|
|
|
|
isError,
|
2022-12-14 15:48:50 +01:00
|
|
|
deprecate,
|
|
|
|
|
SideEffectFreeRegExpPrototypeSymbolReplace,
|
|
|
|
|
SideEffectFreeRegExpPrototypeSymbolSplit,
|
2019-03-21 10:24:13 +01:00
|
|
|
} = require('internal/util');
|
|
|
|
|
const { inspect } = require('internal/util/inspect');
|
2015-01-21 11:36:59 -05:00
|
|
|
const vm = require('vm');
|
2023-10-11 04:50:50 +02:00
|
|
|
|
|
|
|
|
const { runInThisContext, runInContext } = vm.Script.prototype;
|
|
|
|
|
|
2015-01-21 11:36:59 -05:00
|
|
|
const path = require('path');
|
|
|
|
|
const fs = require('fs');
|
2017-10-07 22:50:42 +08:00
|
|
|
const { Interface } = require('readline');
|
2019-12-11 19:21:40 +01:00
|
|
|
const {
|
2023-02-24 09:43:27 +01:00
|
|
|
commonPrefix,
|
2019-12-11 19:21:40 +01:00
|
|
|
} = require('internal/readline/utils');
|
2017-10-07 22:50:42 +08:00
|
|
|
const { Console } = require('console');
|
2023-05-18 15:36:38 +03:00
|
|
|
const { shouldColorize } = require('internal/util/colors');
|
2019-05-18 22:48:46 -05:00
|
|
|
const CJSModule = require('internal/modules/cjs/loader').Module;
|
2015-01-21 11:36:59 -05:00
|
|
|
const domain = require('domain');
|
2020-03-14 07:55:44 -04:00
|
|
|
let debug = require('internal/util/debuglog').debuglog('repl', (fn) => {
|
|
|
|
|
debug = fn;
|
|
|
|
|
});
|
2018-02-27 14:55:32 +01:00
|
|
|
const {
|
2024-04-23 19:05:38 +02:00
|
|
|
ErrorPrepareStackTrace,
|
2019-09-16 15:32:15 -05:00
|
|
|
codes: {
|
|
|
|
|
ERR_CANNOT_WATCH_SIGINT,
|
|
|
|
|
ERR_INVALID_REPL_EVAL_CONFIG,
|
|
|
|
|
ERR_INVALID_REPL_INPUT,
|
2024-04-06 04:12:19 +02:00
|
|
|
ERR_MISSING_ARGS,
|
2019-09-16 15:32:15 -05:00
|
|
|
ERR_SCRIPT_EXECUTION_INTERRUPTED,
|
|
|
|
|
},
|
2021-04-12 17:48:57 +02:00
|
|
|
isErrorStackTraceLimitWritable,
|
2019-09-16 15:32:15 -05:00
|
|
|
overrideStackTrace,
|
|
|
|
|
} = require('internal/errors');
|
2017-10-29 18:19:24 +01:00
|
|
|
const { sendInspectorCommand } = require('internal/util/inspector');
|
2020-05-08 00:20:08 +02:00
|
|
|
const { getOptionValue } = require('internal/options');
|
2021-01-21 19:03:00 +08:00
|
|
|
const {
|
|
|
|
|
validateFunction,
|
|
|
|
|
validateObject,
|
|
|
|
|
} = require('internal/validators');
|
2020-05-08 00:20:08 +02:00
|
|
|
const experimentalREPLAwait = getOptionValue(
|
2023-02-14 18:45:16 +01:00
|
|
|
'--experimental-repl-await',
|
2018-10-15 23:13:18 -05:00
|
|
|
);
|
2020-05-08 00:20:08 +02:00
|
|
|
const pendingDeprecation = getOptionValue('--pending-deprecation');
|
2019-01-19 15:34:00 +01:00
|
|
|
const {
|
2020-05-04 03:49:04 +02:00
|
|
|
REPL_MODE_SLOPPY,
|
|
|
|
|
REPL_MODE_STRICT,
|
2019-01-19 15:34:00 +01:00
|
|
|
isRecoverableError,
|
2019-12-05 14:41:49 +01:00
|
|
|
kStandaloneREPL,
|
|
|
|
|
setupPreview,
|
2019-12-16 10:22:48 +01:00
|
|
|
setupReverseSearch,
|
2025-03-18 19:30:31 +00:00
|
|
|
isObjectLiteral,
|
2025-08-28 01:32:01 +08:00
|
|
|
isValidSyntax,
|
2025-10-08 08:37:22 +01:00
|
|
|
kContextId,
|
|
|
|
|
getREPLResourceName,
|
|
|
|
|
globalBuiltins,
|
|
|
|
|
getReplBuiltinLibs,
|
|
|
|
|
setReplBuiltinLibs,
|
|
|
|
|
fixReplRequire,
|
2019-01-19 15:34:00 +01:00
|
|
|
} = require('internal/repl/utils');
|
2018-08-20 03:26:27 +02:00
|
|
|
const {
|
2025-10-08 08:37:22 +01:00
|
|
|
complete,
|
|
|
|
|
} = require('internal/repl/completion');
|
2019-04-18 10:58:58 +08:00
|
|
|
const {
|
2018-09-03 11:28:38 -04:00
|
|
|
startSigintWatchdog,
|
2023-02-24 09:43:27 +01:00
|
|
|
stopSigintWatchdog,
|
2019-04-18 10:58:58 +08:00
|
|
|
} = internalBinding('contextify');
|
|
|
|
|
|
2023-10-11 04:50:50 +02:00
|
|
|
const {
|
|
|
|
|
makeContextifyScript,
|
|
|
|
|
} = require('internal/vm');
|
2025-04-13 04:58:01 -07:00
|
|
|
const {
|
|
|
|
|
kMultilinePrompt,
|
2025-04-23 15:05:56 +02:00
|
|
|
kAddNewLineOnTTY,
|
2025-04-13 04:58:01 -07:00
|
|
|
kLastCommandErrored,
|
|
|
|
|
} = require('internal/readline/interface');
|
2018-03-25 20:13:54 -07:00
|
|
|
|
|
|
|
|
// Lazy-loaded.
|
|
|
|
|
let processTopLevelAwait;
|
2010-05-09 17:02:02 -07:00
|
|
|
|
2015-12-09 21:55:29 +01:00
|
|
|
const parentModule = module;
|
2020-11-16 11:12:25 +01:00
|
|
|
const domainSet = new SafeWeakSet();
|
2015-10-13 21:39:16 -07:00
|
|
|
|
2017-06-14 15:52:15 -04:00
|
|
|
const kBufferedCommandSymbol = Symbol('bufferedCommand');
|
2023-09-21 06:37:41 -07:00
|
|
|
const kLoadingSymbol = Symbol('loading');
|
2016-06-22 13:07:59 -04:00
|
|
|
|
2018-05-17 16:25:36 +02:00
|
|
|
let addedNewListener = false;
|
|
|
|
|
|
2025-10-08 08:37:22 +01:00
|
|
|
fixReplRequire(module);
|
2015-05-04 01:40:40 -07:00
|
|
|
|
2018-10-21 17:32:03 +08:00
|
|
|
// This is the default "writer" value, if none is passed in the REPL options,
|
|
|
|
|
// and it can be overridden by custom print functions, such as `probe` or
|
|
|
|
|
// `eyes.js`.
|
2020-05-08 00:35:15 +02:00
|
|
|
const writer = (obj) => inspect(obj, writer.options);
|
2019-03-21 10:24:13 +01:00
|
|
|
writer.options = { ...inspect.defaultOptions, showProxy: true };
|
2010-04-11 16:13:32 -07:00
|
|
|
|
2023-06-11 01:10:39 -07:00
|
|
|
// Converts static import statement to dynamic import statement
|
|
|
|
|
const toDynamicImport = (codeLine) => {
|
|
|
|
|
let dynamicImportStatement = '';
|
|
|
|
|
const ast = acornParse(codeLine, { __proto__: null, sourceType: 'module', ecmaVersion: 'latest' });
|
|
|
|
|
acornWalk.ancestor(ast, {
|
|
|
|
|
ImportDeclaration(node) {
|
|
|
|
|
const awaitDynamicImport = `await import(${JSONStringify(node.source.value)});`;
|
|
|
|
|
if (node.specifiers.length === 0) {
|
|
|
|
|
dynamicImportStatement += awaitDynamicImport;
|
|
|
|
|
} else if (node.specifiers.length === 1 && node.specifiers[0].type === 'ImportNamespaceSpecifier') {
|
|
|
|
|
dynamicImportStatement += `const ${node.specifiers[0].local.name} = ${awaitDynamicImport}`;
|
|
|
|
|
} else {
|
|
|
|
|
const importNames = ArrayPrototypeJoin(ArrayPrototypeMap(node.specifiers, ({ local, imported }) =>
|
|
|
|
|
(local.name === imported?.name ? local.name : `${imported?.name ?? 'default'}: ${local.name}`),
|
|
|
|
|
), ', ');
|
|
|
|
|
dynamicImportStatement += `const { ${importNames} } = ${awaitDynamicImport}`;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return dynamicImportStatement;
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
class Recoverable extends SyntaxError {
|
|
|
|
|
constructor(err) {
|
|
|
|
|
super();
|
|
|
|
|
this.err = err;
|
2012-03-26 15:21:25 -07:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
}
|
2012-03-26 15:21:25 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
class REPLServer extends Interface {
|
|
|
|
|
constructor(prompt,
|
|
|
|
|
stream,
|
|
|
|
|
eval_,
|
|
|
|
|
useGlobal,
|
|
|
|
|
ignoreUndefined,
|
|
|
|
|
replMode) {
|
|
|
|
|
let options;
|
|
|
|
|
if (prompt !== null && typeof prompt === 'object') {
|
|
|
|
|
// An options object was given.
|
|
|
|
|
options = { ...prompt };
|
|
|
|
|
stream = options.stream || options.socket;
|
|
|
|
|
eval_ = options.eval;
|
|
|
|
|
useGlobal = options.useGlobal;
|
|
|
|
|
ignoreUndefined = options.ignoreUndefined;
|
|
|
|
|
prompt = options.prompt;
|
|
|
|
|
replMode = options.replMode;
|
|
|
|
|
} else {
|
|
|
|
|
options = {};
|
|
|
|
|
}
|
2012-03-26 15:21:25 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (!options.input && !options.output) {
|
|
|
|
|
// Legacy API, passing a 'stream'/'socket' option.
|
|
|
|
|
// Use stdin and stdout as the default streams if none were given.
|
|
|
|
|
stream ||= process;
|
2024-10-09 02:42:16 -04:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// We're given a duplex readable/writable Stream, like a `net.Socket`
|
|
|
|
|
// or a custom object with 2 streams, or the `process` object.
|
|
|
|
|
options.input = stream.stdin || stream;
|
|
|
|
|
options.output = stream.stdout || stream;
|
|
|
|
|
}
|
2019-03-08 12:21:35 +01:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (options.terminal === undefined) {
|
|
|
|
|
options.terminal = options.output.isTTY;
|
|
|
|
|
}
|
|
|
|
|
options.terminal = !!options.terminal;
|
2019-03-08 12:21:35 +01:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (options.terminal && options.useColors === undefined) {
|
|
|
|
|
// If possible, check if stdout supports colors or not.
|
|
|
|
|
options.useColors = shouldColorize(options.output);
|
|
|
|
|
}
|
2019-03-08 12:21:35 +01:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
const preview = options.terminal &&
|
|
|
|
|
(options.preview !== undefined ? !!options.preview : !eval_);
|
2019-12-05 14:41:49 +01:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
super({
|
|
|
|
|
input: options.input,
|
|
|
|
|
output: options.output,
|
|
|
|
|
completer: options.completer || completer,
|
|
|
|
|
terminal: options.terminal,
|
|
|
|
|
historySize: options.historySize,
|
|
|
|
|
prompt,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ObjectDefineProperty(this, 'inputStream', {
|
|
|
|
|
__proto__: null,
|
|
|
|
|
get: pendingDeprecation ?
|
|
|
|
|
deprecate(() => this.input,
|
|
|
|
|
'repl.inputStream and repl.outputStream are deprecated. ' +
|
2020-05-08 00:20:08 +02:00
|
|
|
'Use repl.input and repl.output instead',
|
2025-09-07 15:08:39 -04:00
|
|
|
'DEP0141') :
|
|
|
|
|
() => this.input,
|
|
|
|
|
set: pendingDeprecation ?
|
|
|
|
|
deprecate((val) => this.input = val,
|
|
|
|
|
'repl.inputStream and repl.outputStream are deprecated. ' +
|
2020-05-08 00:20:08 +02:00
|
|
|
'Use repl.input and repl.output instead',
|
2025-09-07 15:08:39 -04:00
|
|
|
'DEP0141') :
|
|
|
|
|
(val) => this.input = val,
|
|
|
|
|
enumerable: false,
|
|
|
|
|
configurable: true,
|
|
|
|
|
});
|
|
|
|
|
ObjectDefineProperty(this, 'outputStream', {
|
|
|
|
|
__proto__: null,
|
|
|
|
|
get: pendingDeprecation ?
|
|
|
|
|
deprecate(() => this.output,
|
|
|
|
|
'repl.inputStream and repl.outputStream are deprecated. ' +
|
2020-05-08 00:20:08 +02:00
|
|
|
'Use repl.input and repl.output instead',
|
2025-09-07 15:08:39 -04:00
|
|
|
'DEP0141') :
|
|
|
|
|
() => this.output,
|
|
|
|
|
set: pendingDeprecation ?
|
|
|
|
|
deprecate((val) => this.output = val,
|
|
|
|
|
'repl.inputStream and repl.outputStream are deprecated. ' +
|
2020-05-08 00:20:08 +02:00
|
|
|
'Use repl.input and repl.output instead',
|
2025-09-07 15:08:39 -04:00
|
|
|
'DEP0141') :
|
|
|
|
|
(val) => this.output = val,
|
|
|
|
|
enumerable: false,
|
|
|
|
|
configurable: true,
|
|
|
|
|
});
|
2020-05-08 00:20:08 +02:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
this.allowBlockingCompletions = !!options.allowBlockingCompletions;
|
|
|
|
|
this.useColors = !!options.useColors;
|
|
|
|
|
this._domain = options.domain || domain.create();
|
|
|
|
|
this.useGlobal = !!useGlobal;
|
|
|
|
|
this.ignoreUndefined = !!ignoreUndefined;
|
|
|
|
|
this.replMode = replMode || module.exports.REPL_MODE_SLOPPY;
|
|
|
|
|
this.underscoreAssigned = false;
|
|
|
|
|
this.last = undefined;
|
|
|
|
|
this.underscoreErrAssigned = false;
|
|
|
|
|
this.lastError = undefined;
|
|
|
|
|
this.breakEvalOnSigint = !!options.breakEvalOnSigint;
|
|
|
|
|
this.editorMode = false;
|
|
|
|
|
// Context id for use with the inspector protocol.
|
|
|
|
|
this[kContextId] = undefined;
|
|
|
|
|
this[kLastCommandErrored] = false;
|
|
|
|
|
|
|
|
|
|
if (this.breakEvalOnSigint && eval_) {
|
|
|
|
|
// Allowing this would not reflect user expectations.
|
|
|
|
|
// breakEvalOnSigint affects only the behavior of the default eval().
|
|
|
|
|
throw new ERR_INVALID_REPL_EVAL_CONFIG();
|
|
|
|
|
}
|
2016-05-08 03:30:23 +02:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (options[kStandaloneREPL]) {
|
|
|
|
|
// It is possible to introspect the running REPL accessing this variable
|
|
|
|
|
// from inside the REPL. This is useful for anyone working on the REPL.
|
|
|
|
|
module.exports.repl = this;
|
|
|
|
|
} else if (!addedNewListener) {
|
|
|
|
|
// Add this listener only once and use a WeakSet that contains the REPLs
|
|
|
|
|
// domains. Otherwise we'd have to add a single listener to each REPL
|
|
|
|
|
// instance and that could trigger the `MaxListenersExceededWarning`.
|
|
|
|
|
process.prependListener('newListener', (event, listener) => {
|
|
|
|
|
if (event === 'uncaughtException' &&
|
2018-05-17 16:25:36 +02:00
|
|
|
process.domain &&
|
|
|
|
|
listener.name !== 'domainUncaughtExceptionClear' &&
|
|
|
|
|
domainSet.has(process.domain)) {
|
2025-09-07 15:08:39 -04:00
|
|
|
// Throw an error so that the event will not be added and the current
|
|
|
|
|
// domain takes over. That way the user is notified about the error
|
|
|
|
|
// and the current code evaluation is stopped, just as any other code
|
|
|
|
|
// that contains an error.
|
|
|
|
|
throw new ERR_INVALID_REPL_INPUT(
|
|
|
|
|
'Listeners for `uncaughtException` cannot be used in the REPL');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
addedNewListener = true;
|
|
|
|
|
}
|
2018-05-17 16:25:36 +02:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
domainSet.add(this._domain);
|
2018-05-17 16:25:36 +02:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
const savedRegExMatches = ['', '', '', '', '', '', '', '', '', ''];
|
|
|
|
|
const sep = '\u0000\u0000\u0000';
|
|
|
|
|
const regExMatcher = new RegExp(`^${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` +
|
|
|
|
|
`${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` +
|
|
|
|
|
`${sep}(.*)$`);
|
2018-02-11 19:17:03 +01:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
eval_ ||= defaultEval;
|
2018-02-11 19:17:03 +01:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
const self = this;
|
2019-03-08 12:21:35 +01:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// Pause taking in new input, and store the keys in a buffer.
|
|
|
|
|
const pausedBuffer = [];
|
|
|
|
|
let paused = false;
|
|
|
|
|
function pause() {
|
|
|
|
|
paused = true;
|
|
|
|
|
}
|
2019-11-27 22:56:21 -08:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
function unpause() {
|
|
|
|
|
if (!paused) return;
|
|
|
|
|
paused = false;
|
|
|
|
|
let entry;
|
|
|
|
|
const tmpCompletionEnabled = self.isCompletionEnabled;
|
|
|
|
|
while ((entry = ArrayPrototypeShift(pausedBuffer)) !== undefined) {
|
|
|
|
|
const { 0: type, 1: payload, 2: isCompletionEnabled } = entry;
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'key': {
|
|
|
|
|
const { 0: d, 1: key } = payload;
|
|
|
|
|
self.isCompletionEnabled = isCompletionEnabled;
|
|
|
|
|
self._ttyWrite(d, key);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'close':
|
|
|
|
|
self.emit('exit');
|
|
|
|
|
break;
|
2018-02-11 19:17:03 +01:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
if (paused) {
|
2018-02-11 19:17:03 +01:00
|
|
|
break;
|
2025-09-07 15:08:39 -04:00
|
|
|
}
|
2018-02-11 19:17:03 +01:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
self.isCompletionEnabled = tmpCompletionEnabled;
|
2016-11-23 03:46:25 +05:30
|
|
|
}
|
|
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
function defaultEval(code, context, file, cb) {
|
|
|
|
|
let result, script, wrappedErr;
|
|
|
|
|
let err = null;
|
|
|
|
|
let wrappedCmd = false;
|
|
|
|
|
let awaitPromise = false;
|
|
|
|
|
const input = code;
|
|
|
|
|
|
|
|
|
|
if (isObjectLiteral(code) && isValidSyntax(code)) {
|
|
|
|
|
// Add parentheses to make sure `code` is parsed as an expression
|
|
|
|
|
code = `(${StringPrototypeTrim(code)})\n`;
|
|
|
|
|
wrappedCmd = true;
|
2018-03-25 20:13:54 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
const hostDefinedOptionId = Symbol(`eval:${file}`);
|
|
|
|
|
let parentURL;
|
2021-06-25 11:12:21 -07:00
|
|
|
try {
|
2025-09-07 15:08:39 -04:00
|
|
|
const { pathToFileURL } = require('internal/url');
|
|
|
|
|
// Adding `/repl` prevents dynamic imports from loading relative
|
|
|
|
|
// to the parent of `process.cwd()`.
|
|
|
|
|
parentURL = pathToFileURL(path.join(process.cwd(), 'repl')).href;
|
|
|
|
|
} catch {
|
|
|
|
|
// Continue regardless of error.
|
|
|
|
|
}
|
|
|
|
|
async function importModuleDynamically(specifier, _, importAttributes, phase) {
|
|
|
|
|
const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
|
|
|
|
|
return cascadedLoader.import(specifier, parentURL, importAttributes,
|
|
|
|
|
phase === 'evaluation' ? cascadedLoader.kEvaluationPhase :
|
|
|
|
|
cascadedLoader.kSourcePhase);
|
|
|
|
|
}
|
|
|
|
|
// `experimentalREPLAwait` is set to true by default.
|
|
|
|
|
// Shall be false in case `--no-experimental-repl-await` flag is used.
|
|
|
|
|
if (experimentalREPLAwait && StringPrototypeIncludes(code, 'await')) {
|
|
|
|
|
if (processTopLevelAwait === undefined) {
|
|
|
|
|
({ processTopLevelAwait } = require('internal/repl/await'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const potentialWrappedCode = processTopLevelAwait(code);
|
|
|
|
|
if (potentialWrappedCode !== null) {
|
|
|
|
|
code = potentialWrappedCode;
|
|
|
|
|
wrappedCmd = true;
|
|
|
|
|
awaitPromise = true;
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
let recoverableError = false;
|
|
|
|
|
if (e.name === 'SyntaxError') {
|
|
|
|
|
// Remove all "await"s and attempt running the script
|
|
|
|
|
// in order to detect if error is truly non recoverable
|
|
|
|
|
const fallbackCode = SideEffectFreeRegExpPrototypeSymbolReplace(/\bawait\b/g, code, '');
|
|
|
|
|
try {
|
|
|
|
|
makeContextifyScript(
|
|
|
|
|
fallbackCode, // code
|
|
|
|
|
file, // filename,
|
|
|
|
|
0, // lineOffset
|
|
|
|
|
0, // columnOffset,
|
|
|
|
|
undefined, // cachedData
|
|
|
|
|
false, // produceCachedData
|
|
|
|
|
undefined, // parsingContext
|
|
|
|
|
hostDefinedOptionId, // hostDefinedOptionId
|
|
|
|
|
importModuleDynamically, // importModuleDynamically
|
|
|
|
|
);
|
|
|
|
|
} catch (fallbackError) {
|
|
|
|
|
if (isRecoverableError(fallbackError, fallbackCode)) {
|
|
|
|
|
recoverableError = true;
|
|
|
|
|
err = new Recoverable(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!recoverableError) {
|
|
|
|
|
decorateErrorStack(e);
|
|
|
|
|
err = e;
|
|
|
|
|
}
|
2021-06-25 11:12:21 -07:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// First, create the Script object to check the syntax
|
|
|
|
|
if (code === '\n')
|
|
|
|
|
return cb(null);
|
|
|
|
|
|
|
|
|
|
if (err === null) {
|
|
|
|
|
while (true) {
|
2021-07-06 19:34:36 -03:00
|
|
|
try {
|
2025-09-07 15:08:39 -04:00
|
|
|
if (self.replMode === module.exports.REPL_MODE_STRICT &&
|
|
|
|
|
RegExpPrototypeExec(/^\s*$/, code) === null) {
|
|
|
|
|
// "void 0" keeps the repl from returning "use strict" as the result
|
|
|
|
|
// value for statements and declarations that don't return a value.
|
|
|
|
|
code = `'use strict'; void 0;\n${code}`;
|
|
|
|
|
}
|
|
|
|
|
script = makeContextifyScript(
|
|
|
|
|
code, // code
|
2023-10-11 04:50:50 +02:00
|
|
|
file, // filename,
|
|
|
|
|
0, // lineOffset
|
|
|
|
|
0, // columnOffset,
|
|
|
|
|
undefined, // cachedData
|
|
|
|
|
false, // produceCachedData
|
|
|
|
|
undefined, // parsingContext
|
|
|
|
|
hostDefinedOptionId, // hostDefinedOptionId
|
|
|
|
|
importModuleDynamically, // importModuleDynamically
|
|
|
|
|
);
|
2025-09-07 15:08:39 -04:00
|
|
|
} catch (e) {
|
|
|
|
|
debug('parse error %j', code, e);
|
|
|
|
|
if (wrappedCmd) {
|
|
|
|
|
// Unwrap and try again
|
|
|
|
|
wrappedCmd = false;
|
|
|
|
|
awaitPromise = false;
|
|
|
|
|
code = input;
|
|
|
|
|
wrappedErr = e;
|
|
|
|
|
continue;
|
2021-07-06 19:34:36 -03:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
// Preserve original error for wrapped command
|
|
|
|
|
const error = wrappedErr || e;
|
|
|
|
|
if (isRecoverableError(error, code))
|
|
|
|
|
err = new Recoverable(error);
|
2017-09-23 00:10:47 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
else
|
|
|
|
|
err = error;
|
2021-06-25 11:12:21 -07:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
break;
|
2015-04-23 00:35:53 -07:00
|
|
|
}
|
|
|
|
|
}
|
repl: Simplify paren wrap, continuation-detection
This simplifies the logic that was in isSyntaxError, as well as the
choice to wrap command input in parens to coerce to an expression
statement.
1. Rather than a growing blacklist of allowed-to-throw syntax errors,
just sniff for the one we really care about ("Unexpected end of input")
and let all the others pass through.
2. Wrapping {a:1} in parens makes sense, because blocks and line labels
are silly and confusing and should not be in JavaScript at all.
However, wrapping functions and other types of programs in parens is
weird and required yet *more* hacking to work around. By only wrapping
statements that start with { and end with }, we can handle the confusing
use-case, without having to then do extra work for functions and other
cases.
This also fixes the repl wart where `console.log)(` works in the repl,
but only by virtue of the fact that it's wrapped in parens first, as
well as potential side effects of double-running the commands, such as:
> x = 1
1
> eval('x++; throw new SyntaxError("e")')
... ^C
> x
3
2013-08-29 14:48:24 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// This will set the values from `savedRegExMatches` to corresponding
|
|
|
|
|
// predefined RegExp properties `RegExp.$1`, `RegExp.$2` ... `RegExp.$9`
|
|
|
|
|
RegExpPrototypeExec(regExMatcher,
|
|
|
|
|
ArrayPrototypeJoin(savedRegExMatches, sep));
|
2015-07-09 03:23:48 +05:30
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
let finished = false;
|
|
|
|
|
function finishExecution(err, result) {
|
|
|
|
|
if (finished) return;
|
|
|
|
|
finished = true;
|
2017-09-23 00:10:47 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// After executing the current expression, store the values of RegExp
|
|
|
|
|
// predefined properties back in `savedRegExMatches`
|
|
|
|
|
for (let idx = 1; idx < savedRegExMatches.length; idx += 1) {
|
|
|
|
|
savedRegExMatches[idx] = RegExp[`$${idx}`];
|
|
|
|
|
}
|
2017-09-23 00:10:47 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
cb(err, result);
|
2016-05-08 03:30:23 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (!err) {
|
|
|
|
|
// Unset raw mode during evaluation so that Ctrl+C raises a signal.
|
|
|
|
|
let previouslyInRawMode;
|
|
|
|
|
if (self.breakEvalOnSigint) {
|
|
|
|
|
// Start the SIGINT watchdog before entering raw mode so that a very
|
|
|
|
|
// quick Ctrl+C doesn't lead to aborting the process completely.
|
|
|
|
|
if (!startSigintWatchdog())
|
|
|
|
|
throw new ERR_CANNOT_WATCH_SIGINT();
|
|
|
|
|
previouslyInRawMode = self._setRawMode(false);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-08 03:30:23 +02:00
|
|
|
try {
|
2025-09-07 15:08:39 -04:00
|
|
|
try {
|
|
|
|
|
const scriptOptions = {
|
|
|
|
|
displayErrors: false,
|
|
|
|
|
breakOnSigint: self.breakEvalOnSigint,
|
|
|
|
|
};
|
2016-05-08 03:30:23 +02:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (self.useGlobal) {
|
2025-11-23 16:45:46 +08:00
|
|
|
result = FunctionPrototypeCall(runInThisContext, script, scriptOptions);
|
2025-09-07 15:08:39 -04:00
|
|
|
} else {
|
2025-11-23 16:45:46 +08:00
|
|
|
result = FunctionPrototypeCall(runInContext, script, context, scriptOptions);
|
2025-09-07 15:08:39 -04:00
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (self.breakEvalOnSigint) {
|
|
|
|
|
// Reset terminal mode to its previous value.
|
|
|
|
|
self._setRawMode(previouslyInRawMode);
|
|
|
|
|
|
|
|
|
|
// Returns true if there were pending SIGINTs *after* the script
|
|
|
|
|
// has terminated without being interrupted itself.
|
|
|
|
|
if (stopSigintWatchdog()) {
|
|
|
|
|
self.emit('SIGINT');
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-08 03:30:23 +02:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
} catch (e) {
|
|
|
|
|
err = e;
|
2016-05-08 03:30:23 +02:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (process.domain) {
|
|
|
|
|
debug('not recoverable, send to domain');
|
|
|
|
|
process.domain.emit('error', err);
|
|
|
|
|
process.domain.exit();
|
|
|
|
|
return;
|
2016-05-08 03:30:23 +02:00
|
|
|
}
|
2013-08-27 18:53:39 -07:00
|
|
|
}
|
2018-05-25 22:26:34 -04:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (awaitPromise && !err) {
|
|
|
|
|
let sigintListener;
|
|
|
|
|
pause();
|
|
|
|
|
let promise = result;
|
|
|
|
|
if (self.breakEvalOnSigint) {
|
|
|
|
|
const interrupt = new Promise((resolve, reject) => {
|
|
|
|
|
sigintListener = () => {
|
|
|
|
|
const tmp = MainContextError.stackTraceLimit;
|
|
|
|
|
if (isErrorStackTraceLimitWritable()) MainContextError.stackTraceLimit = 0;
|
|
|
|
|
const err = new ERR_SCRIPT_EXECUTION_INTERRUPTED();
|
|
|
|
|
if (isErrorStackTraceLimitWritable()) MainContextError.stackTraceLimit = tmp;
|
|
|
|
|
reject(err);
|
|
|
|
|
};
|
|
|
|
|
prioritizedSigintQueue.add(sigintListener);
|
|
|
|
|
});
|
|
|
|
|
promise = SafePromiseRace([promise, interrupt]);
|
|
|
|
|
}
|
repl: Simplify paren wrap, continuation-detection
This simplifies the logic that was in isSyntaxError, as well as the
choice to wrap command input in parens to coerce to an expression
statement.
1. Rather than a growing blacklist of allowed-to-throw syntax errors,
just sniff for the one we really care about ("Unexpected end of input")
and let all the others pass through.
2. Wrapping {a:1} in parens makes sense, because blocks and line labels
are silly and confusing and should not be in JavaScript at all.
However, wrapping functions and other types of programs in parens is
weird and required yet *more* hacking to work around. By only wrapping
statements that start with { and end with }, we can handle the confusing
use-case, without having to then do extra work for functions and other
cases.
This also fixes the repl wart where `console.log)(` works in the repl,
but only by virtue of the fact that it's wrapped in parens first, as
well as potential side effects of double-running the commands, such as:
> x = 1
1
> eval('x++; throw new SyntaxError("e")')
... ^C
> x
3
2013-08-29 14:48:24 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
(async () => {
|
|
|
|
|
try {
|
|
|
|
|
const result = (await promise)?.value;
|
|
|
|
|
finishExecution(null, result);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err && process.domain) {
|
|
|
|
|
debug('not recoverable, send to domain');
|
|
|
|
|
process.domain.emit('error', err);
|
|
|
|
|
process.domain.exit();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
finishExecution(err);
|
|
|
|
|
} finally {
|
|
|
|
|
// Remove prioritized SIGINT listener if it was not called.
|
|
|
|
|
prioritizedSigintQueue.delete(sigintListener);
|
|
|
|
|
unpause();
|
|
|
|
|
}
|
|
|
|
|
})();
|
2017-09-23 00:10:47 -07:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
}
|
2017-09-23 00:10:47 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (!awaitPromise || err) {
|
|
|
|
|
finishExecution(err, result);
|
2017-09-23 00:10:47 -07:00
|
|
|
}
|
2015-07-09 03:23:48 +05:30
|
|
|
}
|
|
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
self.eval = self._domain.bind(eval_);
|
|
|
|
|
|
|
|
|
|
self._domain.on('error', function debugDomainError(e) {
|
|
|
|
|
debug('domain error');
|
|
|
|
|
let errStack = '';
|
|
|
|
|
|
|
|
|
|
if (typeof e === 'object' && e !== null) {
|
|
|
|
|
overrideStackTrace.set(e, (error, stackFrames) => {
|
|
|
|
|
let frames;
|
|
|
|
|
if (typeof stackFrames === 'object') {
|
|
|
|
|
// Search from the bottom of the call stack to
|
|
|
|
|
// find the first frame with a null function name
|
|
|
|
|
const idx = ArrayPrototypeFindLastIndex(
|
|
|
|
|
stackFrames,
|
|
|
|
|
(frame) => frame.getFunctionName() === null,
|
|
|
|
|
);
|
|
|
|
|
// If found, get rid of it and everything below it
|
|
|
|
|
frames = ArrayPrototypeSlice(stackFrames, 0, idx);
|
|
|
|
|
} else {
|
|
|
|
|
frames = stackFrames;
|
|
|
|
|
}
|
|
|
|
|
// FIXME(devsnek): this is inconsistent with the checks
|
|
|
|
|
// that the real prepareStackTrace dispatch uses in
|
|
|
|
|
// lib/internal/errors.js.
|
|
|
|
|
if (typeof MainContextError.prepareStackTrace === 'function') {
|
|
|
|
|
return MainContextError.prepareStackTrace(error, frames);
|
|
|
|
|
}
|
|
|
|
|
return ErrorPrepareStackTrace(error, frames);
|
|
|
|
|
});
|
|
|
|
|
decorateErrorStack(e);
|
2013-03-14 14:16:13 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (e.domainThrown) {
|
|
|
|
|
delete e.domain;
|
|
|
|
|
delete e.domainThrown;
|
2019-09-16 15:32:15 -05:00
|
|
|
}
|
2018-08-21 03:22:18 +02:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (isError(e)) {
|
|
|
|
|
if (e.stack) {
|
|
|
|
|
if (e.name === 'SyntaxError') {
|
|
|
|
|
// Remove stack trace.
|
2022-12-14 15:48:50 +01:00
|
|
|
e.stack = SideEffectFreeRegExpPrototypeSymbolReplace(
|
2025-09-07 15:08:39 -04:00
|
|
|
/^\s+at\s.*\n?/gm,
|
|
|
|
|
SideEffectFreeRegExpPrototypeSymbolReplace(/^REPL\d+:\d+\r?\n/, e.stack, ''),
|
|
|
|
|
'');
|
|
|
|
|
const importErrorStr = 'Cannot use import statement outside a ' +
|
|
|
|
|
'module';
|
|
|
|
|
if (StringPrototypeIncludes(e.message, importErrorStr)) {
|
|
|
|
|
e.message = 'Cannot use import statement inside the Node.js ' +
|
|
|
|
|
'REPL, alternatively use dynamic import: ' + toDynamicImport(ArrayPrototypeAt(self.lines, -1));
|
|
|
|
|
e.stack = SideEffectFreeRegExpPrototypeSymbolReplace(
|
|
|
|
|
/SyntaxError:.*\n/,
|
|
|
|
|
e.stack,
|
|
|
|
|
`SyntaxError: ${e.message}\n`);
|
|
|
|
|
}
|
|
|
|
|
} else if (self.replMode === module.exports.REPL_MODE_STRICT) {
|
|
|
|
|
e.stack = SideEffectFreeRegExpPrototypeSymbolReplace(
|
|
|
|
|
/(\s+at\s+REPL\d+:)(\d+)/,
|
2022-06-27 17:16:06 +02:00
|
|
|
e.stack,
|
2025-09-07 15:08:39 -04:00
|
|
|
(_, pre, line) => pre + (line - 1),
|
|
|
|
|
);
|
2020-05-27 12:05:55 -04:00
|
|
|
}
|
2018-08-21 03:22:18 +02:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
errStack = self.writer(e);
|
2018-08-21 03:22:18 +02:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// Remove one line error braces to keep the old style in place.
|
|
|
|
|
if (errStack[0] === '[' && errStack[errStack.length - 1] === ']') {
|
|
|
|
|
errStack = StringPrototypeSlice(errStack, 1, -1);
|
|
|
|
|
}
|
2018-08-21 03:22:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (!self.underscoreErrAssigned) {
|
|
|
|
|
self.lastError = e;
|
|
|
|
|
}
|
2018-08-21 03:22:18 +02:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (options[kStandaloneREPL] &&
|
2018-05-17 16:25:36 +02:00
|
|
|
process.listenerCount('uncaughtException') !== 0) {
|
2025-09-07 15:08:39 -04:00
|
|
|
process.nextTick(() => {
|
|
|
|
|
process.emit('uncaughtException', e);
|
|
|
|
|
self.clearBufferedCommand();
|
|
|
|
|
self.lines.level = [];
|
|
|
|
|
if (!self.closed) {
|
|
|
|
|
self.displayPrompt();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
if (errStack === '') {
|
|
|
|
|
errStack = self.writer(e);
|
|
|
|
|
}
|
|
|
|
|
const lines = SideEffectFreeRegExpPrototypeSymbolSplit(/(?<=\n)/, errStack);
|
|
|
|
|
let matched = false;
|
|
|
|
|
|
|
|
|
|
errStack = '';
|
|
|
|
|
ArrayPrototypeForEach(lines, (line) => {
|
|
|
|
|
if (!matched &&
|
|
|
|
|
RegExpPrototypeExec(/^\[?([A-Z][a-z0-9_]*)*Error/, line) !== null) {
|
|
|
|
|
errStack += writer.options.breakLength >= line.length ?
|
|
|
|
|
`Uncaught ${line}` :
|
|
|
|
|
`Uncaught:\n${line}`;
|
|
|
|
|
matched = true;
|
|
|
|
|
} else {
|
|
|
|
|
errStack += line;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (!matched) {
|
|
|
|
|
const ln = lines.length === 1 ? ' ' : ':\n';
|
|
|
|
|
errStack = `Uncaught${ln}${errStack}`;
|
|
|
|
|
}
|
|
|
|
|
// Normalize line endings.
|
|
|
|
|
errStack += StringPrototypeEndsWith(errStack, '\n') ? '' : '\n';
|
|
|
|
|
self.output.write(errStack);
|
2019-12-11 19:26:47 +01:00
|
|
|
self.clearBufferedCommand();
|
|
|
|
|
self.lines.level = [];
|
2025-06-28 19:51:41 +01:00
|
|
|
if (!self.closed) {
|
|
|
|
|
self.displayPrompt();
|
|
|
|
|
}
|
2018-05-17 16:25:36 +02:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
});
|
2012-03-27 17:39:14 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
self.clearBufferedCommand();
|
2019-01-19 15:34:00 +01:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
function completer(text, cb) {
|
2025-11-23 16:45:46 +08:00
|
|
|
FunctionPrototypeCall(complete, self, text, self.editorMode ? self.completeOnEditorMode(cb) : cb);
|
2019-01-19 15:34:00 +01:00
|
|
|
}
|
2010-09-16 21:07:22 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
self.resetContext();
|
|
|
|
|
|
|
|
|
|
this.commands = { __proto__: null };
|
|
|
|
|
defineDefaultCommands(this);
|
|
|
|
|
|
|
|
|
|
// Figure out which "writer" function to use
|
|
|
|
|
self.writer = options.writer || module.exports.writer;
|
|
|
|
|
|
|
|
|
|
if (self.writer === writer) {
|
|
|
|
|
// Conditionally turn on ANSI coloring.
|
|
|
|
|
writer.options.colors = self.useColors;
|
|
|
|
|
|
|
|
|
|
if (options[kStandaloneREPL]) {
|
|
|
|
|
ObjectDefineProperty(inspect, 'replDefaults', {
|
|
|
|
|
__proto__: null,
|
|
|
|
|
get() {
|
|
|
|
|
return writer.options;
|
|
|
|
|
},
|
|
|
|
|
set(options) {
|
|
|
|
|
validateObject(options, 'options');
|
|
|
|
|
return ObjectAssign(writer.options, options);
|
|
|
|
|
},
|
|
|
|
|
enumerable: true,
|
|
|
|
|
configurable: true,
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-07-13 14:17:33 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
function _parseREPLKeyword(keyword, rest) {
|
|
|
|
|
const cmd = this.commands[keyword];
|
|
|
|
|
if (cmd) {
|
2025-11-23 16:45:46 +08:00
|
|
|
FunctionPrototypeCall(cmd.action, this, rest);
|
2025-09-07 15:08:39 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2017-09-23 00:09:09 -07:00
|
|
|
}
|
2012-03-27 12:41:42 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
self.on('close', function emitExit() {
|
|
|
|
|
if (paused) {
|
|
|
|
|
ArrayPrototypePush(pausedBuffer, ['close']);
|
|
|
|
|
return;
|
2021-05-12 19:17:43 +02:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
self.emit('exit');
|
|
|
|
|
});
|
2017-09-23 00:10:47 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
let sawSIGINT = false;
|
|
|
|
|
let sawCtrlD = false;
|
|
|
|
|
const prioritizedSigintQueue = new SafeSet();
|
|
|
|
|
self.on('SIGINT', function onSigInt() {
|
|
|
|
|
if (prioritizedSigintQueue.size > 0) {
|
|
|
|
|
for (const task of prioritizedSigintQueue) {
|
|
|
|
|
task();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-03-12 17:29:21 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
const empty = self.line.length === 0;
|
|
|
|
|
self.clearLine();
|
|
|
|
|
_turnOffEditorMode(self);
|
|
|
|
|
|
|
|
|
|
const cmd = self[kBufferedCommandSymbol];
|
|
|
|
|
if (!(cmd && cmd.length > 0) && empty) {
|
|
|
|
|
if (sawSIGINT) {
|
|
|
|
|
self.close();
|
|
|
|
|
sawSIGINT = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self.output.write(
|
|
|
|
|
'(To exit, press Ctrl+C again or Ctrl+D or type .exit)\n',
|
|
|
|
|
);
|
|
|
|
|
sawSIGINT = true;
|
|
|
|
|
} else {
|
2012-03-12 18:05:16 -07:00
|
|
|
sawSIGINT = false;
|
|
|
|
|
}
|
2011-09-20 17:04:35 +07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
self.clearBufferedCommand();
|
|
|
|
|
self.lines.level = [];
|
|
|
|
|
self.displayPrompt();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
self.on('line', function onLine(cmd) {
|
|
|
|
|
debug('line %j', cmd);
|
|
|
|
|
cmd ||= '';
|
|
|
|
|
sawSIGINT = false;
|
2010-09-16 21:07:22 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (self.editorMode) {
|
|
|
|
|
self[kBufferedCommandSymbol] += cmd + '\n';
|
|
|
|
|
|
|
|
|
|
// code alignment
|
|
|
|
|
const matches = self._sawKeyPress && !self[kLoadingSymbol] ?
|
|
|
|
|
RegExpPrototypeExec(/^\s+/, cmd) : null;
|
|
|
|
|
if (matches) {
|
|
|
|
|
const prefix = matches[0];
|
|
|
|
|
self.write(prefix);
|
|
|
|
|
self.line = prefix;
|
|
|
|
|
self.cursor = prefix.length;
|
|
|
|
|
}
|
2025-11-23 16:45:46 +08:00
|
|
|
FunctionPrototypeCall(_memory, self, cmd);
|
2025-09-07 15:08:39 -04:00
|
|
|
return;
|
2016-08-24 00:05:59 +05:30
|
|
|
}
|
2016-06-12 13:37:33 +05:30
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// Check REPL keywords and empty lines against a trimmed line input.
|
|
|
|
|
const trimmedCmd = StringPrototypeTrim(cmd);
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// Check to see if a REPL keyword was used. If it returns true,
|
|
|
|
|
// display next prompt and return.
|
|
|
|
|
if (trimmedCmd) {
|
|
|
|
|
if (StringPrototypeCharAt(trimmedCmd, 0) === '.' &&
|
2020-09-28 16:42:29 +02:00
|
|
|
StringPrototypeCharAt(trimmedCmd, 1) !== '.' &&
|
|
|
|
|
NumberIsNaN(NumberParseFloat(trimmedCmd))) {
|
2025-09-07 15:08:39 -04:00
|
|
|
const matches = RegExpPrototypeExec(/^\.([^\s]+)\s*(.*)$/, trimmedCmd);
|
|
|
|
|
const keyword = matches?.[1];
|
|
|
|
|
const rest = matches?.[2];
|
2025-11-23 16:45:46 +08:00
|
|
|
if (FunctionPrototypeCall(_parseREPLKeyword, self, keyword, rest) === true) {
|
2025-09-07 15:08:39 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!self[kBufferedCommandSymbol]) {
|
|
|
|
|
self.output.write('Invalid REPL keyword\n');
|
|
|
|
|
finish(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-11-13 16:40:08 -08:00
|
|
|
}
|
|
|
|
|
}
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
const evalCmd = self[kBufferedCommandSymbol] + cmd + '\n';
|
2011-09-07 14:39:49 +07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
debug('eval %j', evalCmd);
|
|
|
|
|
self.eval(evalCmd, self.context, getREPLResourceName(), finish);
|
2016-04-05 17:17:33 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
function finish(e, ret) {
|
|
|
|
|
debug('finish', e, ret);
|
2025-11-23 16:45:46 +08:00
|
|
|
FunctionPrototypeCall(_memory, self, cmd);
|
2011-11-11 17:44:39 -08:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (e && !self[kBufferedCommandSymbol] &&
|
2024-09-15 22:25:19 +09:00
|
|
|
StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ') &&
|
2025-09-07 15:08:39 -04:00
|
|
|
!(e instanceof Recoverable)) {
|
|
|
|
|
self.output.write('npm should be run outside of the ' +
|
|
|
|
|
'Node.js REPL, in your normal shell.\n' +
|
|
|
|
|
'(Press Ctrl+D to exit.)\n');
|
2012-06-05 12:02:37 -07:00
|
|
|
self.displayPrompt();
|
2025-09-07 15:08:39 -04:00
|
|
|
return;
|
2025-04-13 04:58:01 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// If error was SyntaxError and not JSON.parse error
|
|
|
|
|
// We can start a multiline command
|
|
|
|
|
if (e instanceof Recoverable && !sawCtrlD) {
|
|
|
|
|
if (self.terminal) {
|
|
|
|
|
self[kAddNewLineOnTTY]();
|
|
|
|
|
} else {
|
|
|
|
|
self[kBufferedCommandSymbol] += cmd + '\n';
|
|
|
|
|
self.displayPrompt();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-09-07 17:31:29 +07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (e) {
|
|
|
|
|
self._domain.emit('error', e.err || e);
|
|
|
|
|
self[kLastCommandErrored] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear buffer if no SyntaxErrors
|
|
|
|
|
self.clearBufferedCommand();
|
|
|
|
|
sawCtrlD = false;
|
2011-09-08 16:03:27 +07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// If we got any output - print it (if no error)
|
|
|
|
|
if (!e &&
|
2015-07-12 00:53:39 +00:00
|
|
|
// When an invalid REPL command is used, error message is printed
|
|
|
|
|
// immediately. We don't have to print anything else. So, only when
|
|
|
|
|
// the second argument to this function is there, print it.
|
|
|
|
|
arguments.length === 2 &&
|
|
|
|
|
(!self.ignoreUndefined || ret !== undefined)) {
|
2025-09-07 15:08:39 -04:00
|
|
|
if (!self.underscoreAssigned) {
|
|
|
|
|
self.last = ret;
|
|
|
|
|
}
|
|
|
|
|
self.output.write(self.writer(ret) + '\n');
|
2016-03-02 16:45:16 -05:00
|
|
|
}
|
2011-09-07 17:31:29 +07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// If the REPL sever hasn't closed display prompt again (unless we already
|
|
|
|
|
// did by emitting the 'error' event on the domain instance).
|
|
|
|
|
if (!self.closed && !e) {
|
|
|
|
|
self[kLastCommandErrored] = false;
|
|
|
|
|
self.displayPrompt();
|
|
|
|
|
}
|
2021-04-20 16:26:24 +02:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
});
|
2010-05-31 11:50:35 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
self.on('SIGCONT', function onSigCont() {
|
|
|
|
|
if (self.editorMode) {
|
|
|
|
|
self.output.write(`${self._initialPrompt}.editor\n`);
|
|
|
|
|
self.output.write(
|
|
|
|
|
'// Entering editor mode (Ctrl+D to finish, Ctrl+C to cancel)\n');
|
|
|
|
|
self.output.write(`${self[kBufferedCommandSymbol]}\n`);
|
|
|
|
|
self.prompt(true);
|
|
|
|
|
} else {
|
|
|
|
|
self.displayPrompt(true);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-12-16 10:22:48 +01:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
const { reverseSearch } = setupReverseSearch(this);
|
2019-12-05 14:41:49 +01:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
const {
|
|
|
|
|
clearPreview, showPreview,
|
|
|
|
|
} = setupPreview(
|
|
|
|
|
this,
|
|
|
|
|
kContextId,
|
|
|
|
|
kBufferedCommandSymbol,
|
|
|
|
|
preview,
|
|
|
|
|
);
|
2016-06-12 13:37:33 +05:30
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// Wrap readline tty to enable editor mode and pausing.
|
|
|
|
|
const ttyWrite = FunctionPrototypeBind(self._ttyWrite, self);
|
|
|
|
|
self._ttyWrite = (d, key) => {
|
|
|
|
|
key ||= {};
|
|
|
|
|
if (paused && !(self.breakEvalOnSigint && key.ctrl && key.name === 'c')) {
|
|
|
|
|
ArrayPrototypePush(pausedBuffer,
|
|
|
|
|
['key', [d, key], self.isCompletionEnabled]);
|
|
|
|
|
return;
|
2016-06-12 13:37:33 +05:30
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
if (!self.editorMode || !self.terminal) {
|
|
|
|
|
// Before exiting, make sure to clear the line.
|
|
|
|
|
if (key.ctrl && key.name === 'd' &&
|
|
|
|
|
self.cursor === 0 && self.line.length === 0) {
|
|
|
|
|
self.clearLine();
|
|
|
|
|
}
|
|
|
|
|
clearPreview(key);
|
|
|
|
|
if (!reverseSearch(d, key)) {
|
2016-06-12 13:37:33 +05:30
|
|
|
ttyWrite(d, key);
|
2025-09-07 15:08:39 -04:00
|
|
|
const showCompletionPreview = key.name !== 'escape';
|
|
|
|
|
showPreview(showCompletionPreview);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2016-06-12 13:37:33 +05:30
|
|
|
}
|
|
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// Editor mode
|
|
|
|
|
if (key.ctrl && !key.shift) {
|
|
|
|
|
switch (key.name) {
|
|
|
|
|
// TODO(BridgeAR): There should not be a special mode necessary for full
|
|
|
|
|
// multiline support.
|
|
|
|
|
case 'd': // End editor mode
|
|
|
|
|
_turnOffEditorMode(self);
|
|
|
|
|
sawCtrlD = true;
|
|
|
|
|
ttyWrite(d, { name: 'return' });
|
|
|
|
|
break;
|
|
|
|
|
case 'n': // Override next history item
|
|
|
|
|
case 'p': // Override previous history item
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
ttyWrite(d, key);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
switch (key.name) {
|
|
|
|
|
case 'up': // Override previous history item
|
|
|
|
|
case 'down': // Override next history item
|
|
|
|
|
break;
|
|
|
|
|
case 'tab':
|
|
|
|
|
// Prevent double tab behavior
|
|
|
|
|
self._previousKey = null;
|
|
|
|
|
ttyWrite(d, key);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
ttyWrite(d, key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-05-28 00:38:39 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
self.displayPrompt();
|
2025-05-28 00:38:39 -07:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
setupHistory(historyConfig = {}, cb) {
|
|
|
|
|
// TODO(puskin94): necessary because historyConfig can be a string for backwards compatibility
|
|
|
|
|
const options = typeof historyConfig === 'string' ?
|
|
|
|
|
{ filePath: historyConfig } :
|
|
|
|
|
historyConfig;
|
|
|
|
|
|
|
|
|
|
if (typeof cb === 'function') {
|
|
|
|
|
options.onHistoryFileLoaded = cb;
|
|
|
|
|
}
|
2025-05-28 00:38:39 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
this.setupHistoryManager(options);
|
2015-10-06 23:05:53 -07:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
clearBufferedCommand() {
|
|
|
|
|
this[kBufferedCommandSymbol] = '';
|
|
|
|
|
}
|
|
|
|
|
close() {
|
|
|
|
|
if (this.terminal && this.historyManager.isFlushing && !this._closingOnFlush) {
|
|
|
|
|
this._closingOnFlush = true;
|
|
|
|
|
this.once('flushHistory', () => super.close());
|
2015-10-06 23:05:53 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
process.nextTick(() => super.close());
|
|
|
|
|
}
|
|
|
|
|
createContext() {
|
|
|
|
|
let context;
|
|
|
|
|
if (this.useGlobal) {
|
|
|
|
|
context = globalThis;
|
|
|
|
|
} else {
|
|
|
|
|
sendInspectorCommand((session) => {
|
|
|
|
|
session.post('Runtime.enable');
|
|
|
|
|
session.once('Runtime.executionContextCreated', ({ params }) => {
|
|
|
|
|
this[kContextId] = params.context.id;
|
|
|
|
|
});
|
|
|
|
|
context = vm.createContext();
|
|
|
|
|
session.post('Runtime.disable');
|
|
|
|
|
}, () => {
|
|
|
|
|
context = vm.createContext();
|
2017-10-29 18:19:24 +01:00
|
|
|
});
|
2025-09-07 15:08:39 -04:00
|
|
|
ArrayPrototypeForEach(ObjectGetOwnPropertyNames(globalThis), (name) => {
|
|
|
|
|
// Only set properties that do not already exist as a global builtin.
|
|
|
|
|
if (!globalBuiltins.has(name)) {
|
|
|
|
|
ObjectDefineProperty(context, name,
|
|
|
|
|
{
|
|
|
|
|
__proto__: null,
|
|
|
|
|
...ObjectGetOwnPropertyDescriptor(globalThis, name),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
context.global = context;
|
|
|
|
|
const _console = new Console(this.output);
|
|
|
|
|
ObjectDefineProperty(context, 'console', {
|
|
|
|
|
__proto__: null,
|
|
|
|
|
configurable: true,
|
|
|
|
|
writable: true,
|
|
|
|
|
value: _console,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const replModule = new CJSModule('<repl>');
|
|
|
|
|
replModule.paths = CJSModule._resolveLookupPaths('<repl>', parentModule);
|
|
|
|
|
|
|
|
|
|
ObjectDefineProperty(context, 'module', {
|
|
|
|
|
__proto__: null,
|
|
|
|
|
configurable: true,
|
|
|
|
|
writable: true,
|
|
|
|
|
value: replModule,
|
2020-12-28 20:04:04 +05:30
|
|
|
});
|
2025-09-07 15:08:39 -04:00
|
|
|
ObjectDefineProperty(context, 'require', {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2016-06-22 13:07:59 -04:00
|
|
|
configurable: true,
|
2018-05-14 14:51:50 +02:00
|
|
|
writable: true,
|
2025-09-07 15:08:39 -04:00
|
|
|
value: makeRequireFunction(replModule),
|
2016-06-22 13:07:59 -04:00
|
|
|
});
|
2011-10-20 08:54:50 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
addBuiltinLibsToObject(context, '<REPL>');
|
2015-12-09 21:55:29 +01:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
resetContext() {
|
|
|
|
|
this.context = this.createContext();
|
|
|
|
|
this.underscoreAssigned = false;
|
|
|
|
|
this.underscoreErrAssigned = false;
|
|
|
|
|
// TODO(BridgeAR): Deprecate the lines.
|
|
|
|
|
this.lines = [];
|
|
|
|
|
this.lines.level = [];
|
|
|
|
|
|
|
|
|
|
ObjectDefineProperty(this.context, '_', {
|
|
|
|
|
__proto__: null,
|
|
|
|
|
configurable: true,
|
|
|
|
|
get: () => this.last,
|
|
|
|
|
set: (value) => {
|
|
|
|
|
this.last = value;
|
|
|
|
|
if (!this.underscoreAssigned) {
|
|
|
|
|
this.underscoreAssigned = true;
|
|
|
|
|
this.output.write('Expression assignment to _ now disabled.\n');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
2011-10-20 08:54:50 -07:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
ObjectDefineProperty(this.context, '_error', {
|
|
|
|
|
__proto__: null,
|
|
|
|
|
configurable: true,
|
|
|
|
|
get: () => this.lastError,
|
|
|
|
|
set: (value) => {
|
|
|
|
|
this.lastError = value;
|
|
|
|
|
if (!this.underscoreErrAssigned) {
|
|
|
|
|
this.underscoreErrAssigned = true;
|
|
|
|
|
this.output.write(
|
|
|
|
|
'Expression assignment to _error now disabled.\n');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
2017-07-14 23:23:02 -04:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// Allow REPL extensions to extend the new context
|
|
|
|
|
this.emit('reset', this.context);
|
|
|
|
|
}
|
|
|
|
|
displayPrompt(preserveCursor) {
|
|
|
|
|
let prompt = this._initialPrompt;
|
|
|
|
|
if (this[kBufferedCommandSymbol].length) {
|
|
|
|
|
prompt = kMultilinePrompt.description;
|
|
|
|
|
}
|
2016-03-02 16:45:16 -05:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// Do not overwrite `_initialPrompt` here
|
|
|
|
|
super.setPrompt(prompt);
|
|
|
|
|
this.prompt(preserveCursor);
|
|
|
|
|
}
|
|
|
|
|
// When invoked as an API method, overwrite _initialPrompt
|
|
|
|
|
setPrompt(prompt) {
|
|
|
|
|
this._initialPrompt = prompt;
|
|
|
|
|
super.setPrompt(prompt);
|
|
|
|
|
}
|
|
|
|
|
complete() {
|
|
|
|
|
ReflectApply(this.completer, this, arguments);
|
|
|
|
|
}
|
|
|
|
|
completeOnEditorMode(callback) {
|
|
|
|
|
return (err, results) => {
|
|
|
|
|
if (err) return callback(err);
|
2016-03-02 16:45:16 -05:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
const { 0: completions, 1: completeOn = '' } = results;
|
|
|
|
|
let result = ArrayPrototypeFilter(completions, Boolean);
|
2018-02-21 21:50:20 +01:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
if (completeOn && result.length !== 0) {
|
|
|
|
|
result = [commonPrefix(result)];
|
|
|
|
|
}
|
2011-03-29 14:08:39 -04:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
callback(null, [result, completeOn]);
|
|
|
|
|
};
|
2012-02-18 15:01:35 -08:00
|
|
|
}
|
2025-09-07 15:08:39 -04:00
|
|
|
defineCommand(keyword, cmd) {
|
|
|
|
|
if (typeof cmd === 'function') {
|
|
|
|
|
cmd = { action: cmd };
|
|
|
|
|
} else {
|
|
|
|
|
validateFunction(cmd.action, 'cmd.action');
|
|
|
|
|
}
|
|
|
|
|
this.commands[keyword] = cmd;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-31 12:30:46 +04:00
|
|
|
|
2025-09-07 15:08:39 -04:00
|
|
|
// Prompt is a string to print on each line for the prompt,
|
|
|
|
|
// source is a stream to use for I/O, defaulting to stdin/stdout.
|
|
|
|
|
function start(prompt, source, eval_, useGlobal, ignoreUndefined, replMode) {
|
|
|
|
|
return new REPLServer(
|
|
|
|
|
prompt, source, eval_, useGlobal, ignoreUndefined, replMode);
|
|
|
|
|
}
|
2009-09-24 00:56:24 +02:00
|
|
|
|
2019-12-11 19:24:39 +01:00
|
|
|
// TODO(BridgeAR): This should be replaced with acorn to build an AST. The
|
|
|
|
|
// language became more complex and using a simple approach like this is not
|
|
|
|
|
// sufficient anymore.
|
2017-10-16 17:24:17 -04:00
|
|
|
function _memory(cmd) {
|
|
|
|
|
const self = this;
|
2024-10-09 02:42:16 -04:00
|
|
|
self.lines ||= [];
|
|
|
|
|
self.lines.level ||= [];
|
2011-11-11 17:44:39 -08:00
|
|
|
|
2017-12-30 03:20:51 +01:00
|
|
|
// Save the line so I can do magic later
|
2011-11-11 17:44:39 -08:00
|
|
|
if (cmd) {
|
2015-11-18 06:59:43 -06:00
|
|
|
const len = self.lines.level.length ? self.lines.level.length - 1 : 0;
|
2020-11-16 11:12:25 +01:00
|
|
|
ArrayPrototypePush(self.lines, StringPrototypeRepeat(' ', len) + cmd);
|
2011-11-11 17:44:39 -08:00
|
|
|
} else {
|
|
|
|
|
// I don't want to not change the format too much...
|
2020-11-16 11:12:25 +01:00
|
|
|
ArrayPrototypePush(self.lines, '');
|
2011-11-11 17:44:39 -08:00
|
|
|
}
|
|
|
|
|
|
2019-12-11 19:24:39 +01:00
|
|
|
if (!cmd) {
|
|
|
|
|
self.lines.level = [];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-11 17:44:39 -08:00
|
|
|
// I need to know "depth."
|
|
|
|
|
// Because I can not tell the difference between a } that
|
|
|
|
|
// closes an object literal and a } that closes a function
|
2022-06-27 17:16:06 +02:00
|
|
|
const countMatches = (regex, str) => {
|
|
|
|
|
let count = 0;
|
|
|
|
|
while (RegExpPrototypeExec(regex, str) !== null) count++;
|
|
|
|
|
return count;
|
|
|
|
|
};
|
2019-12-11 19:24:39 +01:00
|
|
|
|
|
|
|
|
// Going down is { and ( e.g. function() {
|
|
|
|
|
// going up is } and )
|
2022-06-27 17:16:06 +02:00
|
|
|
const dw = countMatches(/[{(]/g, cmd);
|
|
|
|
|
const up = countMatches(/[})]/g, cmd);
|
|
|
|
|
let depth = dw.length - up.length;
|
2019-12-11 19:24:39 +01:00
|
|
|
|
|
|
|
|
if (depth) {
|
|
|
|
|
(function workIt() {
|
|
|
|
|
if (depth > 0) {
|
|
|
|
|
// Going... down.
|
|
|
|
|
// Push the line#, depth count, and if the line is a function.
|
|
|
|
|
// Since JS only has functional scope I only need to remove
|
|
|
|
|
// "function() {" lines, clearly this will not work for
|
|
|
|
|
// "function()
|
|
|
|
|
// {" but nothing should break, only tab completion for local
|
|
|
|
|
// scope will not work for this function.
|
2020-11-16 11:12:25 +01:00
|
|
|
ArrayPrototypePush(self.lines.level, {
|
2019-12-11 19:24:39 +01:00
|
|
|
line: self.lines.length - 1,
|
2023-02-24 09:43:27 +01:00
|
|
|
depth: depth,
|
2019-12-11 19:24:39 +01:00
|
|
|
});
|
|
|
|
|
} else if (depth < 0) {
|
|
|
|
|
// Going... up.
|
2020-11-16 11:12:25 +01:00
|
|
|
const curr = ArrayPrototypePop(self.lines.level);
|
2019-12-11 19:24:39 +01:00
|
|
|
if (curr) {
|
|
|
|
|
const tmp = curr.depth + depth;
|
|
|
|
|
if (tmp < 0) {
|
|
|
|
|
// More to go, recurse
|
|
|
|
|
depth += curr.depth;
|
|
|
|
|
workIt();
|
|
|
|
|
} else if (tmp > 0) {
|
|
|
|
|
// Remove and push back
|
|
|
|
|
curr.depth += depth;
|
2020-11-16 11:12:25 +01:00
|
|
|
ArrayPrototypePush(self.lines.level, curr);
|
2011-11-11 17:44:39 -08:00
|
|
|
}
|
|
|
|
|
}
|
2019-12-11 19:24:39 +01:00
|
|
|
}
|
|
|
|
|
}());
|
2011-11-11 17:44:39 -08:00
|
|
|
}
|
2017-10-16 17:24:17 -04:00
|
|
|
}
|
2011-11-11 17:44:39 -08:00
|
|
|
|
2017-09-01 12:52:18 -04:00
|
|
|
function _turnOnEditorMode(repl) {
|
|
|
|
|
repl.editorMode = true;
|
2025-11-23 16:45:46 +08:00
|
|
|
FunctionPrototypeCall(Interface.prototype.setPrompt, repl, '');
|
2017-09-01 12:52:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _turnOffEditorMode(repl) {
|
|
|
|
|
repl.editorMode = false;
|
|
|
|
|
repl.setPrompt(repl._initialPrompt);
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-13 07:51:53 -07:00
|
|
|
function defineDefaultCommands(repl) {
|
|
|
|
|
repl.defineCommand('break', {
|
|
|
|
|
help: 'Sometimes you get stuck, this gets you out',
|
|
|
|
|
action: function() {
|
2017-06-14 15:52:15 -04:00
|
|
|
this.clearBufferedCommand();
|
2010-10-13 07:51:53 -07:00
|
|
|
this.displayPrompt();
|
2023-02-24 09:43:27 +01:00
|
|
|
},
|
2010-10-13 07:51:53 -07:00
|
|
|
});
|
|
|
|
|
|
2019-09-16 07:09:26 -04:00
|
|
|
let clearMessage;
|
2011-11-07 16:10:21 -08:00
|
|
|
if (repl.useGlobal) {
|
|
|
|
|
clearMessage = 'Alias for .break';
|
|
|
|
|
} else {
|
|
|
|
|
clearMessage = 'Break, and also clear the local context';
|
|
|
|
|
}
|
2011-10-20 08:54:50 -07:00
|
|
|
repl.defineCommand('clear', {
|
2011-11-07 16:10:21 -08:00
|
|
|
help: clearMessage,
|
2011-10-20 08:54:50 -07:00
|
|
|
action: function() {
|
2017-06-14 15:52:15 -04:00
|
|
|
this.clearBufferedCommand();
|
2011-11-07 16:10:21 -08:00
|
|
|
if (!this.useGlobal) {
|
2020-05-08 00:20:08 +02:00
|
|
|
this.output.write('Clearing context...\n');
|
2012-10-12 16:34:36 -07:00
|
|
|
this.resetContext();
|
2011-11-07 16:10:21 -08:00
|
|
|
}
|
2011-10-20 08:54:50 -07:00
|
|
|
this.displayPrompt();
|
2023-02-24 09:43:27 +01:00
|
|
|
},
|
2011-10-20 08:54:50 -07:00
|
|
|
});
|
|
|
|
|
|
2010-10-13 07:51:53 -07:00
|
|
|
repl.defineCommand('exit', {
|
2020-08-05 22:03:54 -07:00
|
|
|
help: 'Exit the REPL',
|
2010-10-13 07:51:53 -07:00
|
|
|
action: function() {
|
2014-02-15 22:21:26 +08:00
|
|
|
this.close();
|
2023-02-24 09:43:27 +01:00
|
|
|
},
|
2010-10-13 07:51:53 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
repl.defineCommand('help', {
|
2016-09-13 21:46:57 +02:00
|
|
|
help: 'Print this help message',
|
2010-10-13 07:51:53 -07:00
|
|
|
action: function() {
|
2020-11-16 11:12:25 +01:00
|
|
|
const names = ArrayPrototypeSort(ObjectKeys(this.commands));
|
2021-02-12 12:59:21 +01:00
|
|
|
const longestNameLength = MathMaxApply(
|
2023-02-14 18:45:16 +01:00
|
|
|
ArrayPrototypeMap(names, (name) => name.length),
|
2017-01-12 12:52:20 -08:00
|
|
|
);
|
2020-12-28 20:04:04 +05:30
|
|
|
ArrayPrototypeForEach(names, (name) => {
|
2019-09-16 07:09:26 -04:00
|
|
|
const cmd = this.commands[name];
|
2020-11-16 11:12:25 +01:00
|
|
|
const spaces =
|
|
|
|
|
StringPrototypeRepeat(' ', longestNameLength - name.length + 3);
|
2019-09-16 07:09:26 -04:00
|
|
|
const line = `.${name}${cmd.help ? spaces + cmd.help : ''}\n`;
|
2020-05-08 00:20:08 +02:00
|
|
|
this.output.write(line);
|
2020-12-28 20:04:04 +05:30
|
|
|
});
|
2020-09-19 08:35:27 -07:00
|
|
|
this.output.write('\nPress Ctrl+C to abort current expression, ' +
|
|
|
|
|
'Ctrl+D to exit the REPL\n');
|
2010-10-13 07:51:53 -07:00
|
|
|
this.displayPrompt();
|
2023-02-24 09:43:27 +01:00
|
|
|
},
|
2010-10-13 07:51:53 -07:00
|
|
|
});
|
2011-11-11 17:44:39 -08:00
|
|
|
|
|
|
|
|
repl.defineCommand('save', {
|
|
|
|
|
help: 'Save all evaluated commands in this REPL session to a file',
|
|
|
|
|
action: function(file) {
|
|
|
|
|
try {
|
2024-04-06 04:12:19 +02:00
|
|
|
if (file === '') {
|
|
|
|
|
throw new ERR_MISSING_ARGS('file');
|
|
|
|
|
}
|
2020-11-16 11:12:25 +01:00
|
|
|
fs.writeFileSync(file, ArrayPrototypeJoin(this.lines, '\n'));
|
2020-05-08 00:20:08 +02:00
|
|
|
this.output.write(`Session saved to: ${file}\n`);
|
2024-04-06 04:12:19 +02:00
|
|
|
} catch (error) {
|
|
|
|
|
if (error instanceof ERR_MISSING_ARGS) {
|
|
|
|
|
this.output.write(`${error.message}\n`);
|
|
|
|
|
} else {
|
|
|
|
|
this.output.write(`Failed to save: ${file}\n`);
|
|
|
|
|
}
|
2011-11-11 17:44:39 -08:00
|
|
|
}
|
|
|
|
|
this.displayPrompt();
|
2023-02-24 09:43:27 +01:00
|
|
|
},
|
2011-11-11 17:44:39 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
repl.defineCommand('load', {
|
|
|
|
|
help: 'Load JS from a file into the REPL session',
|
|
|
|
|
action: function(file) {
|
|
|
|
|
try {
|
2024-04-06 04:12:19 +02:00
|
|
|
if (file === '') {
|
|
|
|
|
throw new ERR_MISSING_ARGS('file');
|
|
|
|
|
}
|
2019-07-09 13:36:57 +02:00
|
|
|
const stats = fs.statSync(file);
|
2011-11-11 17:44:39 -08:00
|
|
|
if (stats && stats.isFile()) {
|
2017-09-01 12:52:18 -04:00
|
|
|
_turnOnEditorMode(this);
|
2023-09-21 06:37:41 -07:00
|
|
|
this[kLoadingSymbol] = true;
|
2019-07-09 13:36:57 +02:00
|
|
|
const data = fs.readFileSync(file, 'utf8');
|
|
|
|
|
this.write(data);
|
2023-09-21 06:37:41 -07:00
|
|
|
this[kLoadingSymbol] = false;
|
2017-09-01 12:52:18 -04:00
|
|
|
_turnOffEditorMode(this);
|
2017-08-16 11:31:57 -04:00
|
|
|
this.write('\n');
|
2015-12-06 14:46:52 +05:30
|
|
|
} else {
|
2020-05-08 00:20:08 +02:00
|
|
|
this.output.write(
|
2023-02-14 18:45:16 +01:00
|
|
|
`Failed to load: ${file} is not a valid file\n`,
|
2019-07-09 13:36:57 +02:00
|
|
|
);
|
2011-11-11 17:44:39 -08:00
|
|
|
}
|
2024-04-06 04:12:19 +02:00
|
|
|
} catch (error) {
|
|
|
|
|
if (error instanceof ERR_MISSING_ARGS) {
|
|
|
|
|
this.output.write(`${error.message}\n`);
|
|
|
|
|
} else {
|
|
|
|
|
this.output.write(`Failed to load: ${file}\n`);
|
|
|
|
|
}
|
2011-11-11 17:44:39 -08:00
|
|
|
}
|
|
|
|
|
this.displayPrompt();
|
2023-02-24 09:43:27 +01:00
|
|
|
},
|
2011-11-11 17:44:39 -08:00
|
|
|
});
|
2019-02-21 15:33:46 +01:00
|
|
|
if (repl.terminal) {
|
|
|
|
|
repl.defineCommand('editor', {
|
|
|
|
|
help: 'Enter editor mode',
|
|
|
|
|
action() {
|
|
|
|
|
_turnOnEditorMode(this);
|
2020-05-08 00:20:08 +02:00
|
|
|
this.output.write(
|
2020-09-19 08:35:27 -07:00
|
|
|
'// Entering editor mode (Ctrl+D to finish, Ctrl+C to cancel)\n');
|
2023-02-24 09:43:27 +01:00
|
|
|
},
|
2019-02-21 15:33:46 +01:00
|
|
|
});
|
|
|
|
|
}
|
2010-10-13 07:51:53 -07:00
|
|
|
}
|
|
|
|
|
|
2020-05-08 00:35:15 +02:00
|
|
|
module.exports = {
|
|
|
|
|
start,
|
|
|
|
|
writer,
|
|
|
|
|
REPLServer,
|
|
|
|
|
REPL_MODE_SLOPPY,
|
|
|
|
|
REPL_MODE_STRICT,
|
2023-02-24 09:43:27 +01:00
|
|
|
Recoverable,
|
2025-08-28 01:32:01 +08:00
|
|
|
isValidSyntax,
|
2020-05-08 00:35:15 +02:00
|
|
|
};
|
|
|
|
|
|
2020-05-08 02:39:59 +02:00
|
|
|
ObjectDefineProperty(module.exports, 'builtinModules', {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2025-04-05 14:40:18 +01:00
|
|
|
get: pendingDeprecation ? deprecate(
|
2025-10-08 08:37:22 +01:00
|
|
|
() => getReplBuiltinLibs(),
|
2025-04-05 14:40:18 +01:00
|
|
|
'repl.builtinModules is deprecated. Check module.builtinModules instead',
|
|
|
|
|
'DEP0191',
|
2025-10-08 08:37:22 +01:00
|
|
|
) : () => getReplBuiltinLibs(),
|
2025-04-05 14:40:18 +01:00
|
|
|
set: pendingDeprecation ? deprecate(
|
2025-10-08 08:37:22 +01:00
|
|
|
(val) => setReplBuiltinLibs(val),
|
2025-04-05 14:40:18 +01:00
|
|
|
'repl.builtinModules is deprecated. Check module.builtinModules instead',
|
|
|
|
|
'DEP0191',
|
2025-10-08 08:37:22 +01:00
|
|
|
) : (val) => setReplBuiltinLibs(val),
|
2025-04-05 14:40:18 +01:00
|
|
|
enumerable: false,
|
2023-02-24 09:43:27 +01:00
|
|
|
configurable: true,
|
2020-05-08 02:39:59 +02:00
|
|
|
});
|
|
|
|
|
|
2020-05-08 00:35:15 +02:00
|
|
|
ObjectDefineProperty(module.exports, '_builtinLibs', {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-05-08 00:35:15 +02:00
|
|
|
get: pendingDeprecation ? deprecate(
|
2025-10-08 08:37:22 +01:00
|
|
|
() => getReplBuiltinLibs(),
|
2020-05-08 00:35:15 +02:00
|
|
|
'repl._builtinLibs is deprecated. Check module.builtinModules instead',
|
2023-02-14 18:45:16 +01:00
|
|
|
'DEP0142',
|
2025-10-08 08:37:22 +01:00
|
|
|
) : () => getReplBuiltinLibs(),
|
2020-05-08 00:35:15 +02:00
|
|
|
set: pendingDeprecation ? deprecate(
|
2025-10-08 08:37:22 +01:00
|
|
|
(val) => setReplBuiltinLibs(val),
|
2020-05-08 00:35:15 +02:00
|
|
|
'repl._builtinLibs is deprecated. Check module.builtinModules instead',
|
2023-02-14 18:45:16 +01:00
|
|
|
'DEP0142',
|
2025-10-08 08:37:22 +01:00
|
|
|
) : (val) => setReplBuiltinLibs(val),
|
2020-05-08 00:35:15 +02:00
|
|
|
enumerable: false,
|
2023-02-24 09:43:27 +01:00
|
|
|
configurable: true,
|
2020-05-08 00:35:15 +02:00
|
|
|
});
|