2010-12-02 03:07:20 +01:00
|
|
|
|
/* A repl library that you can include in your own code to get a runtime
|
|
|
|
|
* interface to your program.
|
|
|
|
|
*
|
2012-12-11 11:13:22 +01:00
|
|
|
|
* var repl = require("repl");
|
2010-12-02 03:07:20 +01:00
|
|
|
|
* // start repl on stdin
|
|
|
|
|
* repl.start("prompt> ");
|
|
|
|
|
*
|
|
|
|
|
* // listen for unix socket connections and start repl on them
|
2012-02-19 00:01:35 +01:00
|
|
|
|
* net.createServer(function(socket) {
|
2010-12-02 03:07:20 +01: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-19 00:01:35 +01:00
|
|
|
|
* net.createServer(function(socket) {
|
2010-12-02 03:07:20 +01: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';
|
|
|
|
|
|
2015-11-25 22:27:29 +01:00
|
|
|
|
const internalModule = require('internal/module');
|
2015-11-25 22:37:43 +01:00
|
|
|
|
const internalUtil = require('internal/util');
|
2015-01-21 17:36:59 +01:00
|
|
|
|
const util = require('util');
|
2016-05-08 03:30:23 +02:00
|
|
|
|
const utilBinding = process.binding('util');
|
2015-01-21 17:36:59 +01:00
|
|
|
|
const inherits = util.inherits;
|
|
|
|
|
const Stream = require('stream');
|
|
|
|
|
const vm = require('vm');
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const fs = require('fs');
|
2015-10-07 08:05:53 +02:00
|
|
|
|
const Interface = require('readline').Interface;
|
2015-01-21 17:36:59 +01:00
|
|
|
|
const Console = require('console').Console;
|
2015-11-25 22:27:29 +01:00
|
|
|
|
const Module = require('module');
|
2015-01-21 17:36:59 +01:00
|
|
|
|
const domain = require('domain');
|
|
|
|
|
const debug = util.debuglog('repl');
|
2010-05-10 02:02:02 +02:00
|
|
|
|
|
2015-12-09 21:55:29 +01:00
|
|
|
|
const parentModule = module;
|
2015-10-14 06:39:16 +02:00
|
|
|
|
const replMap = new WeakMap();
|
|
|
|
|
|
2017-01-01 06:39:57 +01:00
|
|
|
|
const GLOBAL_OBJECT_PROPERTIES = [
|
|
|
|
|
'NaN', 'Infinity', 'undefined', 'eval', 'parseInt', 'parseFloat', 'isNaN',
|
|
|
|
|
'isFinite', 'decodeURI', 'decodeURIComponent', 'encodeURI',
|
|
|
|
|
'encodeURIComponent', 'Object', 'Function', 'Array', 'String', 'Boolean',
|
|
|
|
|
'Number', 'Date', 'RegExp', 'Error', 'EvalError', 'RangeError',
|
|
|
|
|
'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', 'Math', 'JSON'
|
|
|
|
|
];
|
2016-06-22 19:07:59 +02:00
|
|
|
|
const GLOBAL_OBJECT_PROPERTY_MAP = {};
|
|
|
|
|
GLOBAL_OBJECT_PROPERTIES.forEach((p) => GLOBAL_OBJECT_PROPERTY_MAP[p] = p);
|
|
|
|
|
|
2015-05-04 10:40:40 +02:00
|
|
|
|
try {
|
|
|
|
|
// hack for require.resolve("./relative") to work properly.
|
|
|
|
|
module.filename = path.resolve('repl');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// path.resolve('repl') fails when the current working directory has been
|
|
|
|
|
// deleted. Fall back to the directory name of the (absolute) executable
|
|
|
|
|
// path. It's not really correct but what are the alternatives?
|
|
|
|
|
const dirname = path.dirname(process.execPath);
|
|
|
|
|
module.filename = path.resolve(dirname, 'repl');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// hack for repl require to work properly with node_modules folders
|
|
|
|
|
module.paths = require('module')._nodeModulePaths(module.filename);
|
|
|
|
|
|
2011-09-15 19:38:25 +02:00
|
|
|
|
// If obj.hasOwnProperty has been overridden, then calling
|
|
|
|
|
// obj.hasOwnProperty(prop) will break.
|
|
|
|
|
// See: https://github.com/joyent/node/issues/1707
|
|
|
|
|
function hasOwnProperty(obj, prop) {
|
2011-09-15 18:46:30 +02:00
|
|
|
|
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-20 17:54:50 +02:00
|
|
|
|
|
2012-03-28 02:39:14 +02:00
|
|
|
|
// Can overridden with custom print functions, such as `probe` or `eyes.js`.
|
|
|
|
|
// This is the default "writer" value if none is passed in the REPL options.
|
2010-10-11 23:04:09 +02:00
|
|
|
|
exports.writer = util.inspect;
|
2010-04-12 01:13:32 +02:00
|
|
|
|
|
2016-04-15 02:03:12 +02:00
|
|
|
|
exports._builtinLibs = internalModule.builtinLibs;
|
2011-12-25 05:39:57 +01:00
|
|
|
|
|
2011-01-02 06:41:07 +01:00
|
|
|
|
|
2016-04-16 06:21:12 +02:00
|
|
|
|
const BLOCK_SCOPED_ERROR = 'Block-scoped declarations (let, const, function, ' +
|
|
|
|
|
'class) not yet supported outside strict mode';
|
2015-04-23 09:35:53 +02:00
|
|
|
|
|
|
|
|
|
|
2015-10-24 09:46:08 +02:00
|
|
|
|
class LineParser {
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
this.reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
|
this._literal = null;
|
|
|
|
|
this.shouldFail = false;
|
|
|
|
|
this.blockComment = false;
|
2016-02-06 07:44:27 +01:00
|
|
|
|
this.regExpLiteral = false;
|
2016-12-03 09:03:27 +01:00
|
|
|
|
this.prevTokenChar = null;
|
2015-10-24 09:46:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parseLine(line) {
|
|
|
|
|
var previous = null;
|
|
|
|
|
this.shouldFail = false;
|
|
|
|
|
const wasWithinStrLiteral = this._literal !== null;
|
|
|
|
|
|
|
|
|
|
for (const current of line) {
|
|
|
|
|
if (previous === '\\') {
|
|
|
|
|
// valid escaping, skip processing. previous doesn't matter anymore
|
|
|
|
|
previous = null;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this._literal) {
|
2016-02-06 07:44:27 +01:00
|
|
|
|
if (this.regExpLiteral && current === '/') {
|
|
|
|
|
this.regExpLiteral = false;
|
|
|
|
|
previous = null;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-10-24 09:46:08 +02:00
|
|
|
|
if (previous === '*' && current === '/') {
|
|
|
|
|
if (this.blockComment) {
|
|
|
|
|
this.blockComment = false;
|
|
|
|
|
previous = null;
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
this.shouldFail = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ignore rest of the line if `current` and `previous` are `/`s
|
|
|
|
|
if (previous === current && previous === '/' && !this.blockComment) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-06 07:44:27 +01:00
|
|
|
|
if (previous === '/') {
|
|
|
|
|
if (current === '*') {
|
|
|
|
|
this.blockComment = true;
|
2016-12-03 09:03:27 +01:00
|
|
|
|
} else if (
|
|
|
|
|
// Distinguish between a division operator and the start of a regex
|
|
|
|
|
// by examining the non-whitespace character that precedes the /
|
|
|
|
|
[null, '(', '[', '{', '}', ';'].includes(this.prevTokenChar)
|
|
|
|
|
) {
|
2016-02-06 07:44:27 +01:00
|
|
|
|
this.regExpLiteral = true;
|
|
|
|
|
}
|
2015-10-24 09:46:08 +02:00
|
|
|
|
previous = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-06 07:44:27 +01:00
|
|
|
|
if (this.blockComment || this.regExpLiteral) continue;
|
2015-10-24 09:46:08 +02:00
|
|
|
|
|
|
|
|
|
if (current === this._literal) {
|
|
|
|
|
this._literal = null;
|
|
|
|
|
} else if (current === '\'' || current === '"') {
|
|
|
|
|
this._literal = this._literal || current;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-03 09:03:27 +01:00
|
|
|
|
if (current.trim() && current !== '/') this.prevTokenChar = current;
|
|
|
|
|
|
2015-10-24 09:46:08 +02:00
|
|
|
|
previous = current;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const isWithinStrLiteral = this._literal !== null;
|
|
|
|
|
|
|
|
|
|
if (!wasWithinStrLiteral && !isWithinStrLiteral) {
|
|
|
|
|
// Current line has nothing to do with String literals, trim both ends
|
|
|
|
|
line = line.trim();
|
|
|
|
|
} else if (wasWithinStrLiteral && !isWithinStrLiteral) {
|
|
|
|
|
// was part of a string literal, but it is over now, trim only the end
|
|
|
|
|
line = line.trimRight();
|
|
|
|
|
} else if (isWithinStrLiteral && !wasWithinStrLiteral) {
|
|
|
|
|
// was not part of a string literal, but it is now, trim only the start
|
|
|
|
|
line = line.trimLeft();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const lastChar = line.charAt(line.length - 1);
|
|
|
|
|
|
|
|
|
|
this.shouldFail = this.shouldFail ||
|
|
|
|
|
((!this._literal && lastChar === '\\') ||
|
2016-05-09 08:04:17 +02:00
|
|
|
|
(this._literal && lastChar !== '\\'));
|
2015-10-24 09:46:08 +02:00
|
|
|
|
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-04-23 09:35:53 +02:00
|
|
|
|
function REPLServer(prompt,
|
|
|
|
|
stream,
|
|
|
|
|
eval_,
|
|
|
|
|
useGlobal,
|
|
|
|
|
ignoreUndefined,
|
|
|
|
|
replMode) {
|
2012-03-27 00:21:25 +02:00
|
|
|
|
if (!(this instanceof REPLServer)) {
|
2015-04-23 09:35:53 +02:00
|
|
|
|
return new REPLServer(prompt,
|
|
|
|
|
stream,
|
|
|
|
|
eval_,
|
|
|
|
|
useGlobal,
|
|
|
|
|
ignoreUndefined,
|
|
|
|
|
replMode);
|
2012-03-27 00:21:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-08 03:30:23 +02:00
|
|
|
|
var options, input, output, dom, breakEvalOnSigint;
|
2015-01-29 02:05:53 +01:00
|
|
|
|
if (prompt !== null && typeof prompt === 'object') {
|
2012-03-27 00:21:25 +02:00
|
|
|
|
// an options object was given
|
|
|
|
|
options = prompt;
|
|
|
|
|
stream = options.stream || options.socket;
|
|
|
|
|
input = options.input;
|
|
|
|
|
output = options.output;
|
2012-07-04 23:14:27 +02:00
|
|
|
|
eval_ = options.eval;
|
2012-03-27 00:21:25 +02:00
|
|
|
|
useGlobal = options.useGlobal;
|
|
|
|
|
ignoreUndefined = options.ignoreUndefined;
|
|
|
|
|
prompt = options.prompt;
|
2013-03-14 22:16:13 +01:00
|
|
|
|
dom = options.domain;
|
2015-04-23 09:35:53 +02:00
|
|
|
|
replMode = options.replMode;
|
2016-05-08 03:30:23 +02:00
|
|
|
|
breakEvalOnSigint = options.breakEvalOnSigint;
|
2012-03-27 00:21:25 +02:00
|
|
|
|
} else {
|
|
|
|
|
options = {};
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-08 03:30:23 +02:00
|
|
|
|
if (breakEvalOnSigint && eval_) {
|
|
|
|
|
// Allowing this would not reflect user expectations.
|
|
|
|
|
// breakEvalOnSigint affects only the behaviour of the default eval().
|
|
|
|
|
throw new Error('Cannot specify both breakEvalOnSigint and eval for REPL');
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-12 01:13:32 +02:00
|
|
|
|
var self = this;
|
2011-03-29 20:08:39 +02:00
|
|
|
|
|
2013-03-14 22:16:13 +01:00
|
|
|
|
self._domain = dom || domain.create();
|
|
|
|
|
|
2012-03-28 02:34:55 +02:00
|
|
|
|
self.useGlobal = !!useGlobal;
|
2012-03-28 02:35:33 +02:00
|
|
|
|
self.ignoreUndefined = !!ignoreUndefined;
|
2015-04-23 09:35:53 +02:00
|
|
|
|
self.replMode = replMode || exports.REPL_MODE_SLOPPY;
|
2016-03-02 22:45:16 +01:00
|
|
|
|
self.underscoreAssigned = false;
|
|
|
|
|
self.last = undefined;
|
2016-05-08 03:30:23 +02:00
|
|
|
|
self.breakEvalOnSigint = !!breakEvalOnSigint;
|
2016-06-12 10:07:33 +02:00
|
|
|
|
self.editorMode = false;
|
2011-10-20 19:00:15 +02:00
|
|
|
|
|
2015-01-19 06:42:05 +01:00
|
|
|
|
self._inTemplateLiteral = false;
|
|
|
|
|
|
2014-02-15 15:21:26 +01:00
|
|
|
|
// just for backwards compat, see github.com/joyent/node/pull/7127
|
|
|
|
|
self.rli = this;
|
|
|
|
|
|
2015-07-08 23:53:48 +02:00
|
|
|
|
const savedRegExMatches = ['', '', '', '', '', '', '', '', '', ''];
|
|
|
|
|
const sep = '\u0000\u0000\u0000';
|
|
|
|
|
const regExMatcher = new RegExp(`^${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` +
|
|
|
|
|
`${sep}(.*)${sep}(.*)${sep}(.*)${sep}(.*)` +
|
|
|
|
|
`${sep}(.*)$`);
|
|
|
|
|
|
2013-03-14 22:16:13 +01:00
|
|
|
|
eval_ = eval_ || defaultEval;
|
|
|
|
|
|
2016-11-22 23:16:25 +01:00
|
|
|
|
function preprocess(code) {
|
|
|
|
|
let cmd = code;
|
|
|
|
|
if (/^\s*\{/.test(cmd) && /\}\s*$/.test(cmd)) {
|
|
|
|
|
// It's confusing for `{ a : 1 }` to be interpreted as a block
|
|
|
|
|
// statement rather than an object literal. So, we first try
|
|
|
|
|
// to wrap it in parentheses, so that it will be interpreted as
|
|
|
|
|
// an expression.
|
|
|
|
|
cmd = `(${cmd})`;
|
|
|
|
|
self.wrappedCmd = true;
|
|
|
|
|
}
|
|
|
|
|
// Append a \n so that it will be either
|
|
|
|
|
// terminated, or continued onto the next expression if it's an
|
|
|
|
|
// unexpected end of input.
|
|
|
|
|
return `${cmd}\n`;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-14 22:16:13 +01:00
|
|
|
|
function defaultEval(code, context, file, cb) {
|
2016-11-22 23:16:25 +01:00
|
|
|
|
// Remove trailing new line
|
|
|
|
|
code = code.replace(/\n$/, '');
|
|
|
|
|
code = preprocess(code);
|
|
|
|
|
|
2016-03-07 06:01:33 +01:00
|
|
|
|
var err, result, retry = false, input = code, wrappedErr;
|
2013-08-28 03:53:39 +02:00
|
|
|
|
// first, create the Script object to check the syntax
|
2016-04-06 02:17:33 +02:00
|
|
|
|
|
2016-08-17 16:47:13 +02:00
|
|
|
|
if (code === '\n')
|
2016-04-06 02:17:33 +02:00
|
|
|
|
return cb(null);
|
|
|
|
|
|
2015-04-23 09:35:53 +02:00
|
|
|
|
while (true) {
|
|
|
|
|
try {
|
|
|
|
|
if (!/^\s*$/.test(code) &&
|
|
|
|
|
(self.replMode === exports.REPL_MODE_STRICT || retry)) {
|
|
|
|
|
// "void 0" keeps the repl from returning "use strict" as the
|
|
|
|
|
// result value for let/const statements.
|
2016-08-17 16:47:13 +02:00
|
|
|
|
code = `'use strict'; void 0;\n${code}`;
|
2015-04-23 09:35:53 +02:00
|
|
|
|
}
|
|
|
|
|
var script = vm.createScript(code, {
|
|
|
|
|
filename: file,
|
2016-07-07 19:49:22 +02:00
|
|
|
|
displayErrors: true
|
2015-04-23 09:35:53 +02:00
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
debug('parse error %j', code, e);
|
|
|
|
|
if (self.replMode === exports.REPL_MODE_MAGIC &&
|
|
|
|
|
e.message === BLOCK_SCOPED_ERROR &&
|
2016-03-07 06:01:33 +01:00
|
|
|
|
!retry || self.wrappedCmd) {
|
|
|
|
|
if (self.wrappedCmd) {
|
|
|
|
|
self.wrappedCmd = false;
|
|
|
|
|
// unwrap and try again
|
2016-08-17 16:47:13 +02:00
|
|
|
|
code = `${input.substring(1, input.length - 2)}\n`;
|
2016-03-07 06:01:33 +01:00
|
|
|
|
wrappedErr = e;
|
|
|
|
|
} else {
|
|
|
|
|
retry = true;
|
|
|
|
|
}
|
2015-04-23 09:35:53 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-03-07 06:01:33 +01:00
|
|
|
|
// preserve original error for wrapped command
|
|
|
|
|
const error = wrappedErr || e;
|
|
|
|
|
if (isRecoverableError(error, self))
|
|
|
|
|
err = new Recoverable(error);
|
2015-04-23 09:35:53 +02:00
|
|
|
|
else
|
2016-03-07 06:01:33 +01:00
|
|
|
|
err = error;
|
2015-04-23 09:35:53 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
2011-09-07 09:39:49 +02: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 23:48:24 +02:00
|
|
|
|
|
2015-07-08 23:53:48 +02:00
|
|
|
|
// This will set the values from `savedRegExMatches` to corresponding
|
|
|
|
|
// predefined RegExp properties `RegExp.$1`, `RegExp.$2` ... `RegExp.$9`
|
|
|
|
|
regExMatcher.test(savedRegExMatches.join(sep));
|
|
|
|
|
|
2013-08-28 03:53:39 +02:00
|
|
|
|
if (!err) {
|
2016-05-08 03:30:23 +02:00
|
|
|
|
// Unset raw mode during evaluation so that Ctrl+C raises a signal.
|
|
|
|
|
let previouslyInRawMode;
|
2016-09-18 11:16:45 +02:00
|
|
|
|
if (self.breakEvalOnSigint) {
|
2016-05-08 03:30:23 +02:00
|
|
|
|
// Start the SIGINT watchdog before entering raw mode so that a very
|
|
|
|
|
// quick Ctrl+C doesn’t lead to aborting the process completely.
|
|
|
|
|
utilBinding.startSigintWatchdog();
|
|
|
|
|
previouslyInRawMode = self._setRawMode(false);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-28 03:53:39 +02:00
|
|
|
|
try {
|
2016-05-08 03:30:23 +02:00
|
|
|
|
try {
|
|
|
|
|
const scriptOptions = {
|
2016-07-07 19:49:22 +02:00
|
|
|
|
displayErrors: true,
|
2016-05-08 03:30:23 +02:00
|
|
|
|
breakOnSigint: self.breakEvalOnSigint
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (self.useGlobal) {
|
|
|
|
|
result = script.runInThisContext(scriptOptions);
|
|
|
|
|
} else {
|
|
|
|
|
result = script.runInContext(context, scriptOptions);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
2016-09-18 11:16:45 +02:00
|
|
|
|
if (self.breakEvalOnSigint) {
|
2016-05-08 03:30:23 +02:00
|
|
|
|
// 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 (utilBinding.stopSigintWatchdog()) {
|
|
|
|
|
self.emit('SIGINT');
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-28 03:53:39 +02:00
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
err = e;
|
2016-05-08 03:30:23 +02:00
|
|
|
|
if (err.message === 'Script execution interrupted.') {
|
|
|
|
|
// The stack trace for this case is not very useful anyway.
|
|
|
|
|
Object.defineProperty(err, 'stack', { value: '' });
|
|
|
|
|
}
|
|
|
|
|
|
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 23:48:24 +02:00
|
|
|
|
if (err && process.domain) {
|
|
|
|
|
debug('not recoverable, send to domain');
|
|
|
|
|
process.domain.emit('error', err);
|
|
|
|
|
process.domain.exit();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-08-28 03:53:39 +02: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 23:48:24 +02:00
|
|
|
|
|
2015-07-08 23:53:48 +02:00
|
|
|
|
// After executing the current expression, store the values of RegExp
|
|
|
|
|
// predefined properties back in `savedRegExMatches`
|
2016-10-01 00:31:47 +02:00
|
|
|
|
for (var idx = 1; idx < savedRegExMatches.length; idx += 1) {
|
2015-07-08 23:53:48 +02:00
|
|
|
|
savedRegExMatches[idx] = RegExp[`$${idx}`];
|
|
|
|
|
}
|
|
|
|
|
|
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 23:48:24 +02:00
|
|
|
|
cb(err, result);
|
2013-03-14 22:16:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.eval = self._domain.bind(eval_);
|
|
|
|
|
|
2016-10-29 17:04:54 +02:00
|
|
|
|
self._domain.on('error', function debugDomainError(e) {
|
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 23:48:24 +02:00
|
|
|
|
debug('domain error');
|
2015-10-14 06:39:16 +02:00
|
|
|
|
const top = replMap.get(self);
|
2015-11-25 22:37:43 +01:00
|
|
|
|
internalUtil.decorateErrorStack(e);
|
2016-07-07 19:49:22 +02:00
|
|
|
|
if (e instanceof SyntaxError && e.stack) {
|
|
|
|
|
// remove repl:line-number and stack trace
|
|
|
|
|
e.stack = e.stack
|
|
|
|
|
.replace(/^repl:\d+\r?\n/, '')
|
|
|
|
|
.replace(/^\s+at\s.*\n?/gm, '');
|
|
|
|
|
} else if (e.stack && self.replMode === exports.REPL_MODE_STRICT) {
|
2016-02-24 19:56:41 +01:00
|
|
|
|
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
|
|
|
|
|
(_, pre, line) => pre + (line - 1));
|
|
|
|
|
}
|
2016-08-17 16:47:13 +02:00
|
|
|
|
top.outputStream.write((e.stack || e) + '\n');
|
2015-10-24 09:46:08 +02:00
|
|
|
|
top.lineParser.reset();
|
2015-10-14 06:39:16 +02:00
|
|
|
|
top.bufferedCommand = '';
|
|
|
|
|
top.lines.level = [];
|
|
|
|
|
top.displayPrompt();
|
2013-03-14 22:16:13 +01:00
|
|
|
|
});
|
2011-09-07 09:39:49 +02:00
|
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
|
if (!input && !output) {
|
|
|
|
|
// legacy API, passing a 'stream'/'socket' option
|
|
|
|
|
if (!stream) {
|
|
|
|
|
// use stdin and stdout as the default streams if none were given
|
|
|
|
|
stream = process;
|
|
|
|
|
}
|
|
|
|
|
if (stream.stdin && stream.stdout) {
|
|
|
|
|
// We're given custom object with 2 streams, or the `process` object
|
|
|
|
|
input = stream.stdin;
|
|
|
|
|
output = stream.stdout;
|
2011-09-22 18:49:59 +02:00
|
|
|
|
} else {
|
2012-03-27 00:21:25 +02:00
|
|
|
|
// We're given a duplex readable/writable Stream, like a `net.Socket`
|
|
|
|
|
input = stream;
|
|
|
|
|
output = stream;
|
2011-09-22 18:49:59 +02:00
|
|
|
|
}
|
2011-01-06 22:42:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
|
self.inputStream = input;
|
|
|
|
|
self.outputStream = output;
|
|
|
|
|
|
2012-08-24 22:18:19 +02:00
|
|
|
|
self.resetContext();
|
2015-10-24 09:46:08 +02:00
|
|
|
|
self.lineParser = new LineParser();
|
2012-08-24 22:18:19 +02:00
|
|
|
|
self.bufferedCommand = '';
|
2013-08-28 03:53:39 +02:00
|
|
|
|
self.lines.level = [];
|
2012-08-24 22:18:19 +02:00
|
|
|
|
|
2016-07-04 00:29:34 +02:00
|
|
|
|
// Figure out which "complete" function to use.
|
2016-12-10 11:08:56 +01:00
|
|
|
|
self.completer = (typeof options.completer === 'function') ?
|
|
|
|
|
options.completer : completer;
|
2016-06-12 10:07:33 +02:00
|
|
|
|
|
|
|
|
|
function completer(text, cb) {
|
2016-12-10 11:08:56 +01:00
|
|
|
|
complete.call(self, text, self.editorMode ?
|
|
|
|
|
self.completeOnEditorMode(cb) : cb);
|
2016-06-12 10:07:33 +02:00
|
|
|
|
}
|
2011-01-24 19:55:30 +01:00
|
|
|
|
|
2015-10-07 08:05:53 +02:00
|
|
|
|
Interface.call(this, {
|
2015-04-23 09:35:53 +02:00
|
|
|
|
input: self.inputStream,
|
|
|
|
|
output: self.outputStream,
|
2016-07-04 00:29:34 +02:00
|
|
|
|
completer: self.completer,
|
2015-04-23 09:35:53 +02:00
|
|
|
|
terminal: options.terminal,
|
2016-06-03 03:31:23 +02:00
|
|
|
|
historySize: options.historySize,
|
|
|
|
|
prompt
|
2015-04-23 09:35:53 +02:00
|
|
|
|
});
|
2014-02-15 15:21:26 +01:00
|
|
|
|
|
2015-07-12 02:58:20 +02:00
|
|
|
|
this.commands = Object.create(null);
|
2010-10-13 16:51:53 +02:00
|
|
|
|
defineDefaultCommands(this);
|
|
|
|
|
|
2012-03-28 02:39:14 +02:00
|
|
|
|
// figure out which "writer" function to use
|
|
|
|
|
self.writer = options.writer || exports.writer;
|
|
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
|
if (options.useColors === undefined) {
|
2014-02-15 15:21:26 +01:00
|
|
|
|
options.useColors = self.terminal;
|
2012-03-28 03:00:59 +02:00
|
|
|
|
}
|
|
|
|
|
self.useColors = !!options.useColors;
|
|
|
|
|
|
|
|
|
|
if (self.useColors && self.writer === util.inspect) {
|
2010-09-04 06:25:35 +02:00
|
|
|
|
// Turn on ANSI coloring.
|
2012-03-28 02:39:14 +02:00
|
|
|
|
self.writer = function(obj, showHidden, depth) {
|
2010-10-11 23:04:09 +02:00
|
|
|
|
return util.inspect(obj, showHidden, depth, true);
|
2010-10-07 05:05:23 +02:00
|
|
|
|
};
|
2010-09-04 06:25:35 +02:00
|
|
|
|
}
|
2010-09-17 06:07:22 +02:00
|
|
|
|
|
2016-10-29 17:04:54 +02:00
|
|
|
|
self.on('close', function emitExit() {
|
2012-03-27 21:41:42 +02:00
|
|
|
|
self.emit('exit');
|
|
|
|
|
});
|
|
|
|
|
|
2011-04-21 21:17:21 +02:00
|
|
|
|
var sawSIGINT = false;
|
2016-06-12 10:07:33 +02:00
|
|
|
|
var sawCtrlD = false;
|
2016-10-29 17:04:54 +02:00
|
|
|
|
self.on('SIGINT', function onSigInt() {
|
2014-02-15 15:21:26 +01:00
|
|
|
|
var empty = self.line.length === 0;
|
|
|
|
|
self.clearLine();
|
2016-06-12 10:07:33 +02:00
|
|
|
|
self.turnOffEditorMode();
|
2012-03-13 01:29:21 +01:00
|
|
|
|
|
2012-03-20 23:14:40 +01:00
|
|
|
|
if (!(self.bufferedCommand && self.bufferedCommand.length > 0) && empty) {
|
2012-03-13 02:05:16 +01:00
|
|
|
|
if (sawSIGINT) {
|
2014-02-15 15:21:26 +01:00
|
|
|
|
self.close();
|
2012-03-13 02:05:16 +01:00
|
|
|
|
sawSIGINT = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-08-17 16:47:13 +02:00
|
|
|
|
self.output.write('(To exit, press ^C again or type .exit)\n');
|
2012-03-13 02:05:16 +01:00
|
|
|
|
sawSIGINT = true;
|
|
|
|
|
} else {
|
2012-03-13 01:28:31 +01:00
|
|
|
|
sawSIGINT = false;
|
2010-09-17 06:07:22 +02:00
|
|
|
|
}
|
2011-09-20 12:04:35 +02:00
|
|
|
|
|
2015-10-24 09:46:08 +02:00
|
|
|
|
self.lineParser.reset();
|
2011-04-21 21:17:21 +02:00
|
|
|
|
self.bufferedCommand = '';
|
2013-08-28 03:53:39 +02:00
|
|
|
|
self.lines.level = [];
|
2011-04-21 21:17:21 +02:00
|
|
|
|
self.displayPrompt();
|
2010-09-17 06:07:22 +02:00
|
|
|
|
});
|
|
|
|
|
|
2016-10-29 17:04:54 +02:00
|
|
|
|
self.on('line', function onLine(cmd) {
|
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 23:48:24 +02:00
|
|
|
|
debug('line %j', cmd);
|
2011-04-21 21:17:21 +02:00
|
|
|
|
sawSIGINT = false;
|
2015-01-19 06:42:05 +01:00
|
|
|
|
|
2016-06-12 10:07:33 +02:00
|
|
|
|
if (self.editorMode) {
|
2016-08-17 16:47:13 +02:00
|
|
|
|
self.bufferedCommand += cmd + '\n';
|
2016-08-23 20:35:59 +02:00
|
|
|
|
|
|
|
|
|
// code alignment
|
|
|
|
|
const matches = self._sawKeyPress ? cmd.match(/^\s+/) : null;
|
|
|
|
|
if (matches) {
|
|
|
|
|
const prefix = matches[0];
|
2016-10-20 16:57:40 +02:00
|
|
|
|
self.write(prefix);
|
2016-08-23 20:35:59 +02:00
|
|
|
|
self.line = prefix;
|
|
|
|
|
self.cursor = prefix.length;
|
|
|
|
|
}
|
2016-08-17 17:34:52 +02:00
|
|
|
|
self.memory(cmd);
|
2016-06-12 10:07:33 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-19 06:42:05 +01:00
|
|
|
|
// leading whitespaces in template literals should not be trimmed.
|
|
|
|
|
if (self._inTemplateLiteral) {
|
|
|
|
|
self._inTemplateLiteral = false;
|
|
|
|
|
} else {
|
2015-10-24 09:46:08 +02:00
|
|
|
|
cmd = self.lineParser.parseLine(cmd);
|
2015-01-19 06:42:05 +01:00
|
|
|
|
}
|
2010-05-31 20:50:35 +02:00
|
|
|
|
|
|
|
|
|
// Check to see if a REPL keyword was used. If it returns true,
|
|
|
|
|
// display next prompt and return.
|
2013-01-03 15:27:55 +01:00
|
|
|
|
if (cmd && cmd.charAt(0) === '.' && isNaN(parseFloat(cmd))) {
|
2014-02-17 23:59:28 +01:00
|
|
|
|
var matches = cmd.match(/^\.([^\s]+)\s*(.*)$/);
|
2010-10-13 16:51:53 +02:00
|
|
|
|
var keyword = matches && matches[1];
|
|
|
|
|
var rest = matches && matches[2];
|
2010-12-03 13:45:00 +01:00
|
|
|
|
if (self.parseREPLKeyword(keyword, rest) === true) {
|
|
|
|
|
return;
|
2015-11-15 15:46:17 +01:00
|
|
|
|
} else if (!self.bufferedCommand) {
|
2016-08-17 16:47:13 +02:00
|
|
|
|
self.outputStream.write('Invalid REPL keyword\n');
|
2016-04-06 02:17:33 +02:00
|
|
|
|
finish(null);
|
|
|
|
|
return;
|
2010-12-03 13:45:00 +01:00
|
|
|
|
}
|
2010-10-13 16:51:53 +02:00
|
|
|
|
}
|
2010-05-31 20:50:35 +02:00
|
|
|
|
|
2016-11-22 23:16:25 +01:00
|
|
|
|
const evalCmd = self.bufferedCommand + cmd + '\n';
|
2011-09-07 09:39:49 +02:00
|
|
|
|
|
2016-04-06 02:17:33 +02:00
|
|
|
|
debug('eval %j', evalCmd);
|
|
|
|
|
self.eval(evalCmd, self.context, 'repl', finish);
|
|
|
|
|
|
2011-09-11 09:05:00 +02:00
|
|
|
|
function finish(e, ret) {
|
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 23:48:24 +02:00
|
|
|
|
debug('finish', e, ret);
|
2011-11-12 02:44:39 +01:00
|
|
|
|
self.memory(cmd);
|
|
|
|
|
|
2016-03-07 06:01:33 +01:00
|
|
|
|
self.wrappedCmd = false;
|
2016-03-17 05:23:52 +01:00
|
|
|
|
if (e && !self.bufferedCommand && cmd.trim().startsWith('npm ')) {
|
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 23:48:24 +02:00
|
|
|
|
self.outputStream.write('npm should be run outside of the ' +
|
2016-08-17 16:47:13 +02:00
|
|
|
|
'node repl, in your normal shell.\n' +
|
|
|
|
|
'(Press Control-D to exit.)\n');
|
2015-10-24 09:46:08 +02:00
|
|
|
|
self.lineParser.reset();
|
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 23:48:24 +02:00
|
|
|
|
self.bufferedCommand = '';
|
|
|
|
|
self.displayPrompt();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-08 11:03:27 +02:00
|
|
|
|
// If error was SyntaxError and not JSON.parse error
|
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 23:48:24 +02:00
|
|
|
|
if (e) {
|
2016-06-12 10:07:33 +02:00
|
|
|
|
if (e instanceof Recoverable && !self.lineParser.shouldFail &&
|
|
|
|
|
!sawCtrlD) {
|
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 23:48:24 +02:00
|
|
|
|
// Start buffering data like that:
|
|
|
|
|
// {
|
|
|
|
|
// ... x: 1
|
|
|
|
|
// ... }
|
2016-08-17 16:47:13 +02:00
|
|
|
|
self.bufferedCommand += cmd + '\n';
|
2012-06-05 21:02:37 +02:00
|
|
|
|
self.displayPrompt();
|
|
|
|
|
return;
|
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 23:48:24 +02:00
|
|
|
|
} else {
|
2015-10-24 09:46:08 +02:00
|
|
|
|
self._domain.emit('error', e.err || e);
|
2012-06-05 21:02:37 +02:00
|
|
|
|
}
|
2010-05-31 20:50:35 +02:00
|
|
|
|
}
|
2011-09-07 12:31:29 +02:00
|
|
|
|
|
2011-09-08 11:03:27 +02:00
|
|
|
|
// Clear buffer if no SyntaxErrors
|
2015-10-24 09:46:08 +02:00
|
|
|
|
self.lineParser.reset();
|
2011-09-08 11:03:27 +02:00
|
|
|
|
self.bufferedCommand = '';
|
2016-06-12 10:07:33 +02:00
|
|
|
|
sawCtrlD = false;
|
2011-09-08 11:03:27 +02:00
|
|
|
|
|
|
|
|
|
// If we got any output - print it (if no error)
|
2015-07-12 02:53:39 +02:00
|
|
|
|
if (!e &&
|
|
|
|
|
// 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)) {
|
2016-03-02 22:45:16 +01:00
|
|
|
|
if (!self.underscoreAssigned) {
|
|
|
|
|
self.last = ret;
|
|
|
|
|
}
|
2016-08-17 16:47:13 +02:00
|
|
|
|
self.outputStream.write(self.writer(ret) + '\n');
|
2011-09-07 12:31:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-08 11:03:27 +02:00
|
|
|
|
// Display prompt again
|
2011-09-07 09:39:49 +02:00
|
|
|
|
self.displayPrompt();
|
2015-04-28 19:46:14 +02:00
|
|
|
|
}
|
2010-05-31 20:50:35 +02:00
|
|
|
|
});
|
|
|
|
|
|
2016-10-29 17:04:54 +02:00
|
|
|
|
self.on('SIGCONT', function onSigCont() {
|
2016-06-12 10:07:33 +02:00
|
|
|
|
if (self.editorMode) {
|
2016-08-17 16:47:13 +02:00
|
|
|
|
self.outputStream.write(`${self._initialPrompt}.editor\n`);
|
2016-06-12 10:07:33 +02:00
|
|
|
|
self.outputStream.write(
|
2016-08-17 16:47:13 +02:00
|
|
|
|
'// Entering editor mode (^D to finish, ^C to cancel)\n');
|
|
|
|
|
self.outputStream.write(`${self.bufferedCommand}\n`);
|
2016-06-12 10:07:33 +02:00
|
|
|
|
self.prompt(true);
|
|
|
|
|
} else {
|
|
|
|
|
self.displayPrompt(true);
|
|
|
|
|
}
|
2010-04-12 01:13:32 +02:00
|
|
|
|
});
|
2010-05-31 20:50:35 +02:00
|
|
|
|
|
2016-06-12 10:07:33 +02:00
|
|
|
|
// Wrap readline tty to enable editor mode
|
|
|
|
|
const ttyWrite = self._ttyWrite.bind(self);
|
|
|
|
|
self._ttyWrite = (d, key) => {
|
2016-10-20 16:47:33 +02:00
|
|
|
|
key = key || {};
|
2016-06-12 10:07:33 +02:00
|
|
|
|
if (!self.editorMode || !self.terminal) {
|
|
|
|
|
ttyWrite(d, key);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// editor mode
|
|
|
|
|
if (key.ctrl && !key.shift) {
|
|
|
|
|
switch (key.name) {
|
|
|
|
|
case 'd': // End editor mode
|
|
|
|
|
self.turnOffEditorMode();
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2010-04-12 01:13:32 +02:00
|
|
|
|
self.displayPrompt();
|
|
|
|
|
}
|
2015-10-07 08:05:53 +02:00
|
|
|
|
inherits(REPLServer, Interface);
|
2010-04-12 01:13:32 +02:00
|
|
|
|
exports.REPLServer = REPLServer;
|
2010-03-15 23:11:40 +01:00
|
|
|
|
|
2015-04-23 09:35:53 +02:00
|
|
|
|
exports.REPL_MODE_SLOPPY = Symbol('repl-sloppy');
|
|
|
|
|
exports.REPL_MODE_STRICT = Symbol('repl-strict');
|
|
|
|
|
exports.REPL_MODE_MAGIC = Symbol('repl-magic');
|
2011-01-02 06:41:07 +01:00
|
|
|
|
|
2010-04-12 01:13:32 +02: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.
|
2015-04-23 09:35:53 +02:00
|
|
|
|
exports.start = function(prompt,
|
|
|
|
|
source,
|
|
|
|
|
eval_,
|
|
|
|
|
useGlobal,
|
|
|
|
|
ignoreUndefined,
|
|
|
|
|
replMode) {
|
|
|
|
|
var repl = new REPLServer(prompt,
|
|
|
|
|
source,
|
|
|
|
|
eval_,
|
|
|
|
|
useGlobal,
|
|
|
|
|
ignoreUndefined,
|
|
|
|
|
replMode);
|
2011-03-29 20:08:39 +02:00
|
|
|
|
if (!exports.repl) exports.repl = repl;
|
2015-10-14 06:39:16 +02:00
|
|
|
|
replMap.set(repl, repl);
|
2011-03-29 20:08:39 +02:00
|
|
|
|
return repl;
|
2010-04-12 01:13:32 +02:00
|
|
|
|
};
|
2010-03-15 23:11:40 +01:00
|
|
|
|
|
2016-10-16 00:02:43 +02:00
|
|
|
|
REPLServer.prototype.close = function close() {
|
2015-10-07 08:05:53 +02:00
|
|
|
|
if (this.terminal && this._flushing && !this._closingOnFlush) {
|
|
|
|
|
this._closingOnFlush = true;
|
|
|
|
|
this.once('flushHistory', () =>
|
|
|
|
|
Interface.prototype.close.call(this)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
process.nextTick(() =>
|
|
|
|
|
Interface.prototype.close.call(this)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2011-10-20 17:54:50 +02:00
|
|
|
|
REPLServer.prototype.createContext = function() {
|
2012-10-13 01:34:36 +02:00
|
|
|
|
var context;
|
|
|
|
|
if (this.useGlobal) {
|
|
|
|
|
context = global;
|
2011-10-20 19:00:15 +02:00
|
|
|
|
} else {
|
2012-10-13 01:34:36 +02:00
|
|
|
|
context = vm.createContext();
|
2012-08-24 22:18:19 +02:00
|
|
|
|
context.global = context;
|
2016-06-22 19:07:59 +02:00
|
|
|
|
const _console = new Console(this.outputStream);
|
|
|
|
|
Object.defineProperty(context, 'console', {
|
|
|
|
|
configurable: true,
|
|
|
|
|
enumerable: true,
|
|
|
|
|
get: () => _console
|
|
|
|
|
});
|
|
|
|
|
Object.getOwnPropertyNames(global).filter((name) => {
|
|
|
|
|
if (name === 'console' || name === 'global') return false;
|
|
|
|
|
return GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined;
|
|
|
|
|
}).forEach((name) => {
|
|
|
|
|
Object.defineProperty(context, name,
|
2016-09-18 05:23:27 +02:00
|
|
|
|
Object.getOwnPropertyDescriptor(global, name));
|
2016-06-22 19:07:59 +02:00
|
|
|
|
});
|
2011-10-20 19:00:15 +02:00
|
|
|
|
}
|
2011-10-20 17:54:50 +02:00
|
|
|
|
|
2015-11-25 22:27:29 +01:00
|
|
|
|
const module = new Module('<repl>');
|
2015-12-09 21:55:29 +01:00
|
|
|
|
module.paths = Module._resolveLookupPaths('<repl>', parentModule)[1];
|
|
|
|
|
|
2015-11-25 22:27:29 +01:00
|
|
|
|
const require = internalModule.makeRequireFunction.call(module);
|
2011-10-20 17:54:50 +02:00
|
|
|
|
context.module = module;
|
|
|
|
|
context.require = require;
|
|
|
|
|
|
2016-03-02 22:45:16 +01:00
|
|
|
|
|
|
|
|
|
this.underscoreAssigned = false;
|
2011-11-12 02:44:39 +01:00
|
|
|
|
this.lines = [];
|
|
|
|
|
this.lines.level = [];
|
|
|
|
|
|
2016-04-15 02:03:12 +02:00
|
|
|
|
internalModule.addBuiltinLibsToObject(context);
|
2013-01-12 21:07:06 +01:00
|
|
|
|
|
2016-03-02 22:45:16 +01:00
|
|
|
|
Object.defineProperty(context, '_', {
|
|
|
|
|
configurable: true,
|
2017-01-12 21:52:20 +01:00
|
|
|
|
get: () => this.last,
|
2016-03-02 22:45:16 +01:00
|
|
|
|
set: (value) => {
|
|
|
|
|
this.last = value;
|
|
|
|
|
if (!this.underscoreAssigned) {
|
|
|
|
|
this.underscoreAssigned = true;
|
2016-08-17 16:47:13 +02:00
|
|
|
|
this.outputStream.write('Expression assignment to _ now disabled.\n');
|
2016-03-02 22:45:16 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2011-10-20 17:54:50 +02:00
|
|
|
|
return context;
|
|
|
|
|
};
|
|
|
|
|
|
2012-10-13 01:34:36 +02:00
|
|
|
|
REPLServer.prototype.resetContext = function() {
|
|
|
|
|
this.context = this.createContext();
|
2013-03-14 21:49:14 +01:00
|
|
|
|
|
|
|
|
|
// Allow REPL extensions to extend the new context
|
|
|
|
|
this.emit('reset', this.context);
|
2011-03-29 20:08:39 +02:00
|
|
|
|
};
|
|
|
|
|
|
2011-12-09 10:24:15 +01:00
|
|
|
|
REPLServer.prototype.displayPrompt = function(preserveCursor) {
|
2014-07-31 10:30:46 +02:00
|
|
|
|
var prompt = this._initialPrompt;
|
2012-02-19 00:01:35 +01:00
|
|
|
|
if (this.bufferedCommand.length) {
|
|
|
|
|
prompt = '...';
|
2015-11-18 13:59:43 +01:00
|
|
|
|
const len = this.lines.level.length ? this.lines.level.length - 1 : 0;
|
|
|
|
|
const levelInd = '..'.repeat(len);
|
2012-02-19 00:01:35 +01:00
|
|
|
|
prompt += levelInd + ' ';
|
|
|
|
|
}
|
2014-07-31 10:30:46 +02:00
|
|
|
|
|
|
|
|
|
// Do not overwrite `_initialPrompt` here
|
|
|
|
|
REPLServer.super_.prototype.setPrompt.call(this, prompt);
|
2014-02-15 15:21:26 +01:00
|
|
|
|
this.prompt(preserveCursor);
|
2014-07-31 10:30:46 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// When invoked as an API method, overwrite _initialPrompt
|
|
|
|
|
REPLServer.prototype.setPrompt = function setPrompt(prompt) {
|
|
|
|
|
this._initialPrompt = prompt;
|
|
|
|
|
REPLServer.super_.prototype.setPrompt.call(this, prompt);
|
2010-04-12 01:13:32 +02:00
|
|
|
|
};
|
2009-09-24 00:56:24 +02:00
|
|
|
|
|
2016-06-12 10:07:33 +02:00
|
|
|
|
REPLServer.prototype.turnOffEditorMode = function() {
|
|
|
|
|
this.editorMode = false;
|
|
|
|
|
this.setPrompt(this._initialPrompt);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2011-11-12 02:44:39 +01:00
|
|
|
|
// A stream to push an array into a REPL
|
|
|
|
|
// used in REPLServer.complete
|
|
|
|
|
function ArrayStream() {
|
2012-06-12 21:53:08 +02:00
|
|
|
|
Stream.call(this);
|
|
|
|
|
|
2012-02-19 00:01:35 +01:00
|
|
|
|
this.run = function(data) {
|
2011-11-12 02:44:39 +01:00
|
|
|
|
var self = this;
|
2012-02-19 00:01:35 +01:00
|
|
|
|
data.forEach(function(line) {
|
2016-08-17 16:47:13 +02:00
|
|
|
|
self.emit('data', line + '\n');
|
2011-11-12 02:44:39 +01:00
|
|
|
|
});
|
2015-01-15 18:55:29 +01:00
|
|
|
|
};
|
2011-11-12 02:44:39 +01:00
|
|
|
|
}
|
2012-06-12 21:53:08 +02:00
|
|
|
|
util.inherits(ArrayStream, Stream);
|
2011-11-12 02:44:39 +01:00
|
|
|
|
ArrayStream.prototype.readable = true;
|
|
|
|
|
ArrayStream.prototype.writable = true;
|
2012-02-19 00:01:35 +01:00
|
|
|
|
ArrayStream.prototype.resume = function() {};
|
|
|
|
|
ArrayStream.prototype.write = function() {};
|
2011-01-02 06:41:07 +01:00
|
|
|
|
|
2016-12-16 00:24:17 +01:00
|
|
|
|
const requireRE = /\brequire\s*\(['"](([\w@./-]+\/)?([\w@./-]*))/;
|
2015-01-21 17:36:59 +01:00
|
|
|
|
const simpleExpressionRE =
|
2016-04-21 11:20:12 +02:00
|
|
|
|
/(([a-zA-Z_$](?:\w|\$)*)\.)*([a-zA-Z_$](?:\w|\$)*)\.?$/;
|
2011-01-02 06:41:07 +01:00
|
|
|
|
|
2015-08-17 21:03:32 +02:00
|
|
|
|
function intFilter(item) {
|
|
|
|
|
// filters out anything not starting with A-Z, a-z, $ or _
|
|
|
|
|
return /^[A-Za-z_$]/.test(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function filteredOwnPropertyNames(obj) {
|
|
|
|
|
if (!obj) return [];
|
|
|
|
|
return Object.getOwnPropertyNames(obj).filter(intFilter);
|
|
|
|
|
}
|
2011-01-02 06:41:07 +01:00
|
|
|
|
|
2016-07-04 00:29:34 +02:00
|
|
|
|
REPLServer.prototype.complete = function() {
|
|
|
|
|
this.completer.apply(this, arguments);
|
|
|
|
|
};
|
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
|
// Provide a list of completions for the given leading text. This is
|
|
|
|
|
// given to the readline interface for handling tab completion.
|
|
|
|
|
//
|
|
|
|
|
// Example:
|
|
|
|
|
// complete('var foo = util.')
|
2015-08-24 23:53:46 +02:00
|
|
|
|
// -> [['util.print', 'util.debug', 'util.log', 'util.inspect'],
|
2010-12-02 03:07:20 +01:00
|
|
|
|
// 'util.' ]
|
|
|
|
|
//
|
|
|
|
|
// Warning: This eval's code like "foo.bar.baz", so it will run property
|
|
|
|
|
// getter code.
|
2016-07-04 00:29:34 +02:00
|
|
|
|
function complete(line, callback) {
|
2011-11-12 02:44:39 +01:00
|
|
|
|
// There may be local variables to evaluate, try a nested REPL
|
2015-01-29 02:05:53 +01:00
|
|
|
|
if (this.bufferedCommand !== undefined && this.bufferedCommand.length) {
|
2011-11-12 02:44:39 +01:00
|
|
|
|
// Get a new array of inputed lines
|
|
|
|
|
var tmp = this.lines.slice();
|
|
|
|
|
// Kill off all function declarations to push all local variables into
|
|
|
|
|
// global scope
|
2012-02-19 00:01:35 +01:00
|
|
|
|
this.lines.level.forEach(function(kill) {
|
2011-11-12 02:44:39 +01:00
|
|
|
|
if (kill.isFunction) {
|
|
|
|
|
tmp[kill.line] = '';
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
var flat = new ArrayStream(); // make a new "input" stream
|
|
|
|
|
var magic = new REPLServer('', flat); // make a nested REPL
|
2016-07-22 14:11:56 +02:00
|
|
|
|
replMap.set(magic, replMap.get(this));
|
2011-11-12 02:44:39 +01:00
|
|
|
|
magic.context = magic.createContext();
|
|
|
|
|
flat.run(tmp); // eval the flattened code
|
|
|
|
|
// all this is only profitable if the nested REPL
|
|
|
|
|
// does not have a bufferedCommand
|
|
|
|
|
if (!magic.bufferedCommand) {
|
|
|
|
|
return magic.complete(line, callback);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
|
var completions;
|
|
|
|
|
|
|
|
|
|
// list of completion lists, one for each inheritance "level"
|
|
|
|
|
var completionGroups = [];
|
2010-08-09 11:18:32 +02:00
|
|
|
|
|
2016-01-31 00:01:06 +01:00
|
|
|
|
var completeOn, i, group, c;
|
2010-08-09 11:18:32 +02:00
|
|
|
|
|
2010-08-17 17:39:54 +02:00
|
|
|
|
// REPL commands (e.g. ".break").
|
2010-08-09 11:18:32 +02:00
|
|
|
|
var match = null;
|
2016-06-22 12:54:52 +02:00
|
|
|
|
match = line.match(/^\s*\.(\w*)$/);
|
2010-08-09 11:18:32 +02:00
|
|
|
|
if (match) {
|
2010-10-13 16:51:53 +02:00
|
|
|
|
completionGroups.push(Object.keys(this.commands));
|
2010-08-09 11:18:32 +02:00
|
|
|
|
completeOn = match[1];
|
2016-06-22 12:54:52 +02:00
|
|
|
|
if (match[1].length) {
|
2010-08-09 11:18:32 +02:00
|
|
|
|
filter = match[1];
|
|
|
|
|
}
|
2011-01-02 06:41:07 +01:00
|
|
|
|
|
2011-09-08 11:03:27 +02:00
|
|
|
|
completionGroupsLoaded();
|
2011-01-02 06:41:07 +01:00
|
|
|
|
} else if (match = line.match(requireRE)) {
|
2010-12-02 03:07:20 +01:00
|
|
|
|
// require('...<Tab>')
|
2015-11-25 22:27:29 +01:00
|
|
|
|
const exts = Object.keys(this.context.require.extensions);
|
2010-12-02 03:07:20 +01:00
|
|
|
|
var indexRe = new RegExp('^index(' + exts.map(regexpEscape).join('|') +
|
|
|
|
|
')$');
|
2010-08-17 17:39:54 +02:00
|
|
|
|
|
|
|
|
|
completeOn = match[1];
|
2010-12-02 03:07:20 +01:00
|
|
|
|
var subdir = match[2] || '';
|
2010-08-17 17:39:54 +02:00
|
|
|
|
var filter = match[1];
|
|
|
|
|
var dir, files, f, name, base, ext, abs, subfiles, s;
|
|
|
|
|
group = [];
|
2011-07-29 19:03:05 +02:00
|
|
|
|
var paths = module.paths.concat(require('module').globalPaths);
|
2011-07-25 03:04:45 +02:00
|
|
|
|
for (i = 0; i < paths.length; i++) {
|
|
|
|
|
dir = path.resolve(paths[i], subdir);
|
2010-08-17 17:39:54 +02:00
|
|
|
|
try {
|
|
|
|
|
files = fs.readdirSync(dir);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
for (f = 0; f < files.length; f++) {
|
|
|
|
|
name = files[f];
|
|
|
|
|
ext = path.extname(name);
|
|
|
|
|
base = name.slice(0, -ext.length);
|
2010-12-02 03:07:20 +01:00
|
|
|
|
if (base.match(/-\d+\.\d+(\.\d+)?/) || name === '.npm') {
|
2010-08-17 17:39:54 +02:00
|
|
|
|
// Exclude versioned names that 'npm' installs.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (exts.indexOf(ext) !== -1) {
|
2010-12-02 03:07:20 +01:00
|
|
|
|
if (!subdir || base !== 'index') {
|
2010-08-17 17:39:54 +02:00
|
|
|
|
group.push(subdir + base);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2011-01-06 21:06:29 +01:00
|
|
|
|
abs = path.resolve(dir, name);
|
2010-08-17 17:39:54 +02:00
|
|
|
|
try {
|
|
|
|
|
if (fs.statSync(abs).isDirectory()) {
|
|
|
|
|
group.push(subdir + name + '/');
|
|
|
|
|
subfiles = fs.readdirSync(abs);
|
|
|
|
|
for (s = 0; s < subfiles.length; s++) {
|
|
|
|
|
if (indexRe.test(subfiles[s])) {
|
|
|
|
|
group.push(subdir + name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-12-02 03:07:20 +01:00
|
|
|
|
} catch (e) {}
|
2010-08-17 17:39:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (group.length) {
|
|
|
|
|
completionGroups.push(group);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!subdir) {
|
2012-03-19 17:08:25 +01:00
|
|
|
|
completionGroups.push(exports._builtinLibs);
|
2010-08-17 17:39:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-08 11:03:27 +02:00
|
|
|
|
completionGroupsLoaded();
|
|
|
|
|
|
2010-08-09 11:18:32 +02:00
|
|
|
|
// Handle variable member lookup.
|
|
|
|
|
// We support simple chained expressions like the following (no function
|
|
|
|
|
// calls, etc.). That is for simplicity and also because we *eval* that
|
|
|
|
|
// leading expression so for safety (see WARNING above) don't want to
|
|
|
|
|
// eval function calls.
|
|
|
|
|
//
|
|
|
|
|
// foo.bar<|> # completions for 'foo' with filter 'bar'
|
|
|
|
|
// spam.eggs.<|> # completions for 'spam.eggs' with filter ''
|
|
|
|
|
// foo<|> # all scope vars with filter 'foo'
|
|
|
|
|
// foo.<|> # completions for 'foo' with filter ''
|
2011-01-02 06:41:07 +01:00
|
|
|
|
} else if (line.length === 0 || line[line.length - 1].match(/\w|\.|\$/)) {
|
|
|
|
|
match = simpleExpressionRE.exec(line);
|
2010-08-09 11:18:32 +02:00
|
|
|
|
if (line.length === 0 || match) {
|
|
|
|
|
var expr;
|
2010-12-02 03:07:20 +01:00
|
|
|
|
completeOn = (match ? match[0] : '');
|
2010-08-09 11:18:32 +02:00
|
|
|
|
if (line.length === 0) {
|
2010-12-02 03:07:20 +01:00
|
|
|
|
filter = '';
|
|
|
|
|
expr = '';
|
|
|
|
|
} else if (line[line.length - 1] === '.') {
|
|
|
|
|
filter = '';
|
|
|
|
|
expr = match[0].slice(0, match[0].length - 1);
|
2010-08-09 11:18:32 +02:00
|
|
|
|
} else {
|
|
|
|
|
var bits = match[0].split('.');
|
|
|
|
|
filter = bits.pop();
|
|
|
|
|
expr = bits.join('.');
|
|
|
|
|
}
|
2010-12-02 03:07:20 +01:00
|
|
|
|
|
2010-08-09 11:18:32 +02:00
|
|
|
|
// Resolve expr and get its completions.
|
2014-06-11 21:16:48 +02:00
|
|
|
|
var memberGroups = [];
|
2010-08-09 11:18:32 +02:00
|
|
|
|
if (!expr) {
|
2011-09-08 11:03:27 +02:00
|
|
|
|
// If context is instance of vm.ScriptContext
|
|
|
|
|
// Get global vars synchronously
|
2015-05-27 11:10:45 +02:00
|
|
|
|
if (this.useGlobal || vm.isContext(this.context)) {
|
2012-04-26 05:08:30 +02:00
|
|
|
|
var contextProto = this.context;
|
|
|
|
|
while (contextProto = Object.getPrototypeOf(contextProto)) {
|
2015-08-17 21:03:32 +02:00
|
|
|
|
completionGroups.push(filteredOwnPropertyNames(contextProto));
|
2012-04-26 05:08:30 +02:00
|
|
|
|
}
|
2015-08-17 21:03:32 +02:00
|
|
|
|
completionGroups.push(filteredOwnPropertyNames(this.context));
|
2012-07-04 23:14:27 +02:00
|
|
|
|
addStandardGlobals(completionGroups, filter);
|
2011-10-20 17:54:50 +02:00
|
|
|
|
completionGroupsLoaded();
|
|
|
|
|
} else {
|
2016-10-29 17:04:54 +02:00
|
|
|
|
this.eval('.scope', this.context, 'repl', function ev(err, globals) {
|
2015-05-17 19:25:19 +02:00
|
|
|
|
if (err || !Array.isArray(globals)) {
|
2012-07-04 23:14:27 +02:00
|
|
|
|
addStandardGlobals(completionGroups, filter);
|
2015-01-29 02:05:53 +01:00
|
|
|
|
} else if (Array.isArray(globals[0])) {
|
2011-10-20 17:54:50 +02:00
|
|
|
|
// Add grouped globals
|
|
|
|
|
globals.forEach(function(group) {
|
|
|
|
|
completionGroups.push(group);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
completionGroups.push(globals);
|
2012-07-04 23:14:27 +02:00
|
|
|
|
addStandardGlobals(completionGroups, filter);
|
2011-10-20 17:54:50 +02:00
|
|
|
|
}
|
|
|
|
|
completionGroupsLoaded();
|
|
|
|
|
});
|
|
|
|
|
}
|
2010-08-09 11:18:32 +02:00
|
|
|
|
} else {
|
2016-04-21 11:20:12 +02:00
|
|
|
|
const evalExpr = `try { ${expr} } catch (e) {}`;
|
2016-10-29 17:04:54 +02:00
|
|
|
|
this.eval(evalExpr, this.context, 'repl', function doEval(e, obj) {
|
2011-09-07 09:39:49 +02:00
|
|
|
|
// if (e) console.log(e);
|
|
|
|
|
|
|
|
|
|
if (obj != null) {
|
2015-01-29 02:05:53 +01:00
|
|
|
|
if (typeof obj === 'object' || typeof obj === 'function') {
|
2015-07-07 19:01:21 +02:00
|
|
|
|
try {
|
2015-08-17 21:03:32 +02:00
|
|
|
|
memberGroups.push(filteredOwnPropertyNames(obj));
|
2015-07-07 19:01:21 +02:00
|
|
|
|
} catch (ex) {
|
|
|
|
|
// Probably a Proxy object without `getOwnPropertyNames` trap.
|
|
|
|
|
// We simply ignore it here, as we don't want to break the
|
|
|
|
|
// autocompletion. Fixes the bug
|
2015-08-13 18:14:34 +02:00
|
|
|
|
// https://github.com/nodejs/node/issues/2119
|
2015-07-07 19:01:21 +02:00
|
|
|
|
}
|
2011-09-07 09:39:49 +02:00
|
|
|
|
}
|
|
|
|
|
// works for non-objects
|
|
|
|
|
try {
|
|
|
|
|
var sentinel = 5;
|
2012-02-17 01:33:40 +01:00
|
|
|
|
var p;
|
2015-01-29 02:05:53 +01:00
|
|
|
|
if (typeof obj === 'object' || typeof obj === 'function') {
|
2012-02-17 01:33:40 +01:00
|
|
|
|
p = Object.getPrototypeOf(obj);
|
|
|
|
|
} else {
|
|
|
|
|
p = obj.constructor ? obj.constructor.prototype : null;
|
|
|
|
|
}
|
2015-01-29 02:05:53 +01:00
|
|
|
|
while (p !== null) {
|
2015-08-17 21:03:32 +02:00
|
|
|
|
memberGroups.push(filteredOwnPropertyNames(p));
|
2011-09-07 09:39:49 +02:00
|
|
|
|
p = Object.getPrototypeOf(p);
|
|
|
|
|
// Circular refs possible? Let's guard against that.
|
|
|
|
|
sentinel--;
|
|
|
|
|
if (sentinel <= 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2010-08-09 11:18:32 +02:00
|
|
|
|
}
|
2011-09-07 09:39:49 +02:00
|
|
|
|
} catch (e) {
|
|
|
|
|
//console.log("completion error walking prototype chain:" + e);
|
2010-08-09 11:18:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-07 09:39:49 +02:00
|
|
|
|
if (memberGroups.length) {
|
|
|
|
|
for (i = 0; i < memberGroups.length; i++) {
|
|
|
|
|
completionGroups.push(memberGroups[i].map(function(member) {
|
|
|
|
|
return expr + '.' + member;
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
if (filter) {
|
|
|
|
|
filter = expr + '.' + filter;
|
|
|
|
|
}
|
2010-08-09 11:18:32 +02:00
|
|
|
|
}
|
2011-09-07 10:12:10 +02:00
|
|
|
|
|
2011-09-08 11:03:27 +02:00
|
|
|
|
completionGroupsLoaded();
|
2011-09-07 09:39:49 +02:00
|
|
|
|
});
|
2010-08-09 11:18:32 +02:00
|
|
|
|
}
|
2011-09-08 11:03:27 +02:00
|
|
|
|
} else {
|
|
|
|
|
completionGroupsLoaded();
|
2010-08-09 11:18:32 +02:00
|
|
|
|
}
|
2012-03-20 02:44:22 +01:00
|
|
|
|
} else {
|
|
|
|
|
completionGroupsLoaded();
|
2010-08-09 11:18:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-08 11:03:27 +02:00
|
|
|
|
// Will be called when all completionGroups are in place
|
|
|
|
|
// Useful for async autocompletion
|
|
|
|
|
function completionGroupsLoaded(err) {
|
2011-09-07 09:39:49 +02:00
|
|
|
|
if (err) throw err;
|
|
|
|
|
|
|
|
|
|
// Filter, sort (within each group), uniq and merge the completion groups.
|
|
|
|
|
if (completionGroups.length && filter) {
|
|
|
|
|
var newCompletionGroups = [];
|
|
|
|
|
for (i = 0; i < completionGroups.length; i++) {
|
|
|
|
|
group = completionGroups[i].filter(function(elem) {
|
2016-10-31 04:12:25 +01:00
|
|
|
|
return elem.indexOf(filter) === 0;
|
2011-09-07 09:39:49 +02:00
|
|
|
|
});
|
|
|
|
|
if (group.length) {
|
|
|
|
|
newCompletionGroups.push(group);
|
|
|
|
|
}
|
2010-08-09 11:18:32 +02:00
|
|
|
|
}
|
2011-09-07 09:39:49 +02:00
|
|
|
|
completionGroups = newCompletionGroups;
|
2010-08-09 11:18:32 +02:00
|
|
|
|
}
|
2011-01-02 06:41:07 +01:00
|
|
|
|
|
2011-09-07 09:39:49 +02:00
|
|
|
|
if (completionGroups.length) {
|
|
|
|
|
var uniq = {}; // unique completions across all groups
|
|
|
|
|
completions = [];
|
|
|
|
|
// Completion group 0 is the "closest"
|
|
|
|
|
// (least far up the inheritance chain)
|
|
|
|
|
// so we put its completions last: to be closest in the REPL.
|
|
|
|
|
for (i = completionGroups.length - 1; i >= 0; i--) {
|
|
|
|
|
group = completionGroups[i];
|
|
|
|
|
group.sort();
|
|
|
|
|
for (var j = 0; j < group.length; j++) {
|
|
|
|
|
c = group[j];
|
2013-11-12 00:03:29 +01:00
|
|
|
|
if (!hasOwnProperty(uniq, c)) {
|
2011-09-07 09:39:49 +02:00
|
|
|
|
completions.push(c);
|
|
|
|
|
uniq[c] = true;
|
|
|
|
|
}
|
2010-08-09 11:18:32 +02:00
|
|
|
|
}
|
2011-09-07 09:39:49 +02:00
|
|
|
|
completions.push(''); // separator btwn groups
|
|
|
|
|
}
|
|
|
|
|
while (completions.length && completions[completions.length - 1] === '') {
|
|
|
|
|
completions.pop();
|
2010-08-09 11:18:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-07 09:39:49 +02:00
|
|
|
|
callback(null, [completions || [], completeOn]);
|
|
|
|
|
}
|
2016-07-04 00:29:34 +02:00
|
|
|
|
}
|
2010-08-09 11:18:32 +02:00
|
|
|
|
|
2016-06-12 10:07:33 +02:00
|
|
|
|
function longestCommonPrefix(arr = []) {
|
|
|
|
|
const cnt = arr.length;
|
|
|
|
|
if (cnt === 0) return '';
|
|
|
|
|
if (cnt === 1) return arr[0];
|
|
|
|
|
|
|
|
|
|
const first = arr[0];
|
|
|
|
|
// complexity: O(m * n)
|
2016-10-01 00:31:47 +02:00
|
|
|
|
for (var m = 0; m < first.length; m++) {
|
2016-06-12 10:07:33 +02:00
|
|
|
|
const c = first[m];
|
2016-10-01 00:31:47 +02:00
|
|
|
|
for (var n = 1; n < cnt; n++) {
|
2016-06-12 10:07:33 +02:00
|
|
|
|
const entry = arr[n];
|
|
|
|
|
if (m >= entry.length || c !== entry[m]) {
|
|
|
|
|
return first.substring(0, m);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return first;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
REPLServer.prototype.completeOnEditorMode = (callback) => (err, results) => {
|
|
|
|
|
if (err) return callback(err);
|
|
|
|
|
|
|
|
|
|
const [completions, completeOn = ''] = results;
|
|
|
|
|
const prefixLength = completeOn.length;
|
|
|
|
|
|
|
|
|
|
if (prefixLength === 0) return callback(null, [[], completeOn]);
|
|
|
|
|
|
|
|
|
|
const isNotEmpty = (v) => v.length > 0;
|
|
|
|
|
const trimCompleteOnPrefix = (v) => v.substring(prefixLength);
|
|
|
|
|
const data = completions.filter(isNotEmpty).map(trimCompleteOnPrefix);
|
|
|
|
|
|
|
|
|
|
callback(null, [[`${completeOn}${longestCommonPrefix(data)}`], completeOn]);
|
|
|
|
|
};
|
2011-01-02 06:41:07 +01:00
|
|
|
|
|
2009-09-25 03:56:37 +02:00
|
|
|
|
/**
|
|
|
|
|
* Used to parse and execute the Node REPL commands.
|
2010-06-30 08:12:46 +02:00
|
|
|
|
*
|
2010-12-02 03:07:20 +01:00
|
|
|
|
* @param {keyword} keyword The command entered to check.
|
|
|
|
|
* @return {Boolean} If true it means don't continue parsing the command.
|
2009-09-25 03:56:37 +02:00
|
|
|
|
*/
|
2010-12-02 03:07:20 +01:00
|
|
|
|
REPLServer.prototype.parseREPLKeyword = function(keyword, rest) {
|
2010-10-13 16:51:53 +02:00
|
|
|
|
var cmd = this.commands[keyword];
|
|
|
|
|
if (cmd) {
|
|
|
|
|
cmd.action.call(this, rest);
|
2009-09-25 03:56:37 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2009-09-28 17:37:34 +02:00
|
|
|
|
return false;
|
2010-04-12 01:13:32 +02:00
|
|
|
|
};
|
2009-09-25 03:56:37 +02:00
|
|
|
|
|
2011-01-02 06:41:07 +01:00
|
|
|
|
|
2010-10-13 16:51:53 +02:00
|
|
|
|
REPLServer.prototype.defineCommand = function(keyword, cmd) {
|
2015-01-29 02:05:53 +01:00
|
|
|
|
if (typeof cmd === 'function') {
|
2011-01-02 06:41:07 +01:00
|
|
|
|
cmd = {action: cmd};
|
2015-01-29 02:05:53 +01:00
|
|
|
|
} else if (typeof cmd.action !== 'function') {
|
2015-10-14 23:10:25 +02:00
|
|
|
|
throw new Error('Bad argument, "action" command must be a function');
|
2010-10-13 16:51:53 +02:00
|
|
|
|
}
|
2014-02-17 23:59:28 +01:00
|
|
|
|
this.commands[keyword] = cmd;
|
2010-10-13 16:51:53 +02:00
|
|
|
|
};
|
|
|
|
|
|
2012-02-19 00:01:35 +01:00
|
|
|
|
REPLServer.prototype.memory = function memory(cmd) {
|
2011-11-12 02:44:39 +01:00
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
self.lines = self.lines || [];
|
|
|
|
|
self.lines.level = self.lines.level || [];
|
|
|
|
|
|
|
|
|
|
// save the line so I can do magic later
|
|
|
|
|
if (cmd) {
|
|
|
|
|
// TODO should I tab the level?
|
2015-11-18 13:59:43 +01:00
|
|
|
|
const len = self.lines.level.length ? self.lines.level.length - 1 : 0;
|
|
|
|
|
self.lines.push(' '.repeat(len) + cmd);
|
2011-11-12 02:44:39 +01:00
|
|
|
|
} else {
|
|
|
|
|
// I don't want to not change the format too much...
|
|
|
|
|
self.lines.push('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
if (cmd) {
|
2012-02-19 00:01:35 +01:00
|
|
|
|
// going down is { and ( e.g. function() {
|
2011-11-12 02:44:39 +01:00
|
|
|
|
// going up is } and )
|
|
|
|
|
var dw = cmd.match(/{|\(/g);
|
|
|
|
|
var up = cmd.match(/}|\)/g);
|
|
|
|
|
up = up ? up.length : 0;
|
|
|
|
|
dw = dw ? dw.length : 0;
|
|
|
|
|
var depth = dw - up;
|
|
|
|
|
|
|
|
|
|
if (depth) {
|
2012-02-19 00:01:35 +01:00
|
|
|
|
(function workIt() {
|
2011-11-12 02:44:39 +01:00
|
|
|
|
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
|
2012-02-19 00:01:35 +01:00
|
|
|
|
// "function() {" lines, clearly this will not work for
|
|
|
|
|
// "function()
|
2011-11-12 02:44:39 +01:00
|
|
|
|
// {" but nothing should break, only tab completion for local
|
|
|
|
|
// scope will not work for this function.
|
2012-02-19 00:01:35 +01:00
|
|
|
|
self.lines.level.push({
|
|
|
|
|
line: self.lines.length - 1,
|
|
|
|
|
depth: depth,
|
|
|
|
|
isFunction: /\s*function\s*/.test(cmd)
|
|
|
|
|
});
|
2011-11-12 02:44:39 +01:00
|
|
|
|
} else if (depth < 0) {
|
|
|
|
|
// going... up.
|
|
|
|
|
var curr = self.lines.level.pop();
|
|
|
|
|
if (curr) {
|
|
|
|
|
var 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;
|
|
|
|
|
self.lines.level.push(curr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// it is possible to determine a syntax error at this point.
|
|
|
|
|
// if the REPL still has a bufferedCommand and
|
|
|
|
|
// self.lines.level.length === 0
|
|
|
|
|
// TODO? keep a log of level so that any syntax breaking lines can
|
|
|
|
|
// be cleared on .break and in the case of a syntax error?
|
2016-04-06 02:17:33 +02:00
|
|
|
|
// TODO? if a log was kept, then I could clear the bufferedCommand and
|
2011-11-12 02:44:39 +01:00
|
|
|
|
// eval these lines and throw the syntax error
|
|
|
|
|
} else {
|
|
|
|
|
self.lines.level = [];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-07-04 23:14:27 +02:00
|
|
|
|
function addStandardGlobals(completionGroups, filter) {
|
|
|
|
|
// Global object properties
|
|
|
|
|
// (http://www.ecma-international.org/publications/standards/Ecma-262.htm)
|
2016-06-22 19:07:59 +02:00
|
|
|
|
completionGroups.push(GLOBAL_OBJECT_PROPERTIES);
|
2012-07-04 23:14:27 +02:00
|
|
|
|
// Common keywords. Exclude for completion on the empty string, b/c
|
|
|
|
|
// they just get in the way.
|
|
|
|
|
if (filter) {
|
2017-01-01 06:39:57 +01:00
|
|
|
|
completionGroups.push([
|
|
|
|
|
'break', 'case', 'catch', 'const', 'continue', 'debugger', 'default',
|
|
|
|
|
'delete', 'do', 'else', 'export', 'false', 'finally', 'for', 'function',
|
|
|
|
|
'if', 'import', 'in', 'instanceof', 'let', 'new', 'null', 'return',
|
|
|
|
|
'switch', 'this', 'throw', 'true', 'try', 'typeof', 'undefined', 'var',
|
|
|
|
|
'void', 'while', 'with', 'yield'
|
|
|
|
|
]);
|
2012-07-04 23:14:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-02 06:41:07 +01:00
|
|
|
|
|
2010-10-13 16:51:53 +02:00
|
|
|
|
function defineDefaultCommands(repl) {
|
|
|
|
|
repl.defineCommand('break', {
|
|
|
|
|
help: 'Sometimes you get stuck, this gets you out',
|
|
|
|
|
action: function() {
|
2015-10-24 09:46:08 +02:00
|
|
|
|
this.lineParser.reset();
|
2011-01-02 06:41:07 +01:00
|
|
|
|
this.bufferedCommand = '';
|
2010-10-13 16:51:53 +02:00
|
|
|
|
this.displayPrompt();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2011-11-08 01:10:21 +01:00
|
|
|
|
var clearMessage;
|
|
|
|
|
if (repl.useGlobal) {
|
|
|
|
|
clearMessage = 'Alias for .break';
|
|
|
|
|
} else {
|
|
|
|
|
clearMessage = 'Break, and also clear the local context';
|
|
|
|
|
}
|
2011-10-20 17:54:50 +02:00
|
|
|
|
repl.defineCommand('clear', {
|
2011-11-08 01:10:21 +01:00
|
|
|
|
help: clearMessage,
|
2011-10-20 17:54:50 +02:00
|
|
|
|
action: function() {
|
2015-10-24 09:46:08 +02:00
|
|
|
|
this.lineParser.reset();
|
2011-10-20 17:54:50 +02:00
|
|
|
|
this.bufferedCommand = '';
|
2011-11-08 01:10:21 +01:00
|
|
|
|
if (!this.useGlobal) {
|
2016-08-17 16:47:13 +02:00
|
|
|
|
this.outputStream.write('Clearing context...\n');
|
2012-10-13 01:34:36 +02:00
|
|
|
|
this.resetContext();
|
2011-11-08 01:10:21 +01:00
|
|
|
|
}
|
2011-10-20 17:54:50 +02:00
|
|
|
|
this.displayPrompt();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2010-10-13 16:51:53 +02:00
|
|
|
|
repl.defineCommand('exit', {
|
|
|
|
|
help: 'Exit the repl',
|
|
|
|
|
action: function() {
|
2014-02-15 15:21:26 +01:00
|
|
|
|
this.close();
|
2010-10-13 16:51:53 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
repl.defineCommand('help', {
|
2016-09-13 21:46:57 +02:00
|
|
|
|
help: 'Print this help message',
|
2010-10-13 16:51:53 +02:00
|
|
|
|
action: function() {
|
2016-09-13 21:46:57 +02:00
|
|
|
|
const names = Object.keys(this.commands).sort();
|
2017-01-12 21:52:20 +01:00
|
|
|
|
const longestNameLength = names.reduce(
|
|
|
|
|
(max, name) => Math.max(max, name.length),
|
|
|
|
|
0
|
|
|
|
|
);
|
2016-09-13 21:46:57 +02:00
|
|
|
|
names.forEach((name) => {
|
|
|
|
|
const cmd = this.commands[name];
|
|
|
|
|
const spaces = ' '.repeat(longestNameLength - name.length + 3);
|
|
|
|
|
const line = '.' + name + (cmd.help ? spaces + cmd.help : '') + '\n';
|
|
|
|
|
this.outputStream.write(line);
|
2010-10-13 16:51:53 +02:00
|
|
|
|
});
|
|
|
|
|
this.displayPrompt();
|
|
|
|
|
}
|
|
|
|
|
});
|
2011-11-12 02:44:39 +01:00
|
|
|
|
|
|
|
|
|
repl.defineCommand('save', {
|
|
|
|
|
help: 'Save all evaluated commands in this REPL session to a file',
|
|
|
|
|
action: function(file) {
|
|
|
|
|
try {
|
2016-08-17 16:47:13 +02:00
|
|
|
|
fs.writeFileSync(file, this.lines.join('\n') + '\n');
|
|
|
|
|
this.outputStream.write('Session saved to:' + file + '\n');
|
2011-11-12 02:44:39 +01:00
|
|
|
|
} catch (e) {
|
2016-08-17 16:47:13 +02:00
|
|
|
|
this.outputStream.write('Failed to save:' + file + '\n');
|
2011-11-12 02:44:39 +01:00
|
|
|
|
}
|
|
|
|
|
this.displayPrompt();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
repl.defineCommand('load', {
|
|
|
|
|
help: 'Load JS from a file into the REPL session',
|
|
|
|
|
action: function(file) {
|
|
|
|
|
try {
|
|
|
|
|
var stats = fs.statSync(file);
|
|
|
|
|
if (stats && stats.isFile()) {
|
|
|
|
|
var self = this;
|
|
|
|
|
var data = fs.readFileSync(file, 'utf8');
|
2016-08-17 16:47:13 +02:00
|
|
|
|
var lines = data.split('\n');
|
2011-11-12 02:44:39 +01:00
|
|
|
|
this.displayPrompt();
|
2012-02-19 00:01:35 +01:00
|
|
|
|
lines.forEach(function(line) {
|
2011-11-12 02:44:39 +01:00
|
|
|
|
if (line) {
|
2016-08-17 16:47:13 +02:00
|
|
|
|
self.write(line + '\n');
|
2011-11-12 02:44:39 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
2015-12-06 10:16:52 +01:00
|
|
|
|
} else {
|
|
|
|
|
this.outputStream.write('Failed to load:' + file +
|
2016-08-17 16:47:13 +02:00
|
|
|
|
' is not a valid file\n');
|
2011-11-12 02:44:39 +01:00
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
2016-08-17 16:47:13 +02:00
|
|
|
|
this.outputStream.write('Failed to load:' + file + '\n');
|
2011-11-12 02:44:39 +01:00
|
|
|
|
}
|
|
|
|
|
this.displayPrompt();
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-06-12 10:07:33 +02:00
|
|
|
|
|
|
|
|
|
repl.defineCommand('editor', {
|
2016-09-13 21:46:57 +02:00
|
|
|
|
help: 'Enter editor mode',
|
2016-06-12 10:07:33 +02:00
|
|
|
|
action() {
|
|
|
|
|
if (!this.terminal) return;
|
|
|
|
|
this.editorMode = true;
|
|
|
|
|
REPLServer.super_.prototype.setPrompt.call(this, '');
|
|
|
|
|
this.outputStream.write(
|
2016-08-17 16:47:13 +02:00
|
|
|
|
'// Entering editor mode (^D to finish, ^C to cancel)\n');
|
2016-06-12 10:07:33 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
2010-10-13 16:51:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-08-17 17:39:54 +02:00
|
|
|
|
function regexpEscape(s) {
|
2010-12-02 03:07:20 +01:00
|
|
|
|
return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
|
2010-08-17 17:39:54 +02:00
|
|
|
|
}
|
2011-10-20 17:54:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts commands that use var and function <name>() to use the
|
|
|
|
|
* local exports.context when evaled. This provides a local context
|
|
|
|
|
* on the REPL.
|
|
|
|
|
*
|
|
|
|
|
* @param {String} cmd The cmd to convert.
|
|
|
|
|
* @return {String} The converted command.
|
|
|
|
|
*/
|
2016-07-22 00:43:45 +02:00
|
|
|
|
// TODO(princejwesley): Remove it prior to v8.0.0 release
|
|
|
|
|
// Reference: https://github.com/nodejs/node/pull/7829
|
|
|
|
|
REPLServer.prototype.convertToContext = util.deprecate(function(cmd) {
|
2016-10-31 04:12:25 +01:00
|
|
|
|
const scopeVar = /^\s*var\s*([\w$]+)(.*)$/m;
|
|
|
|
|
const scopeFunc = /^\s*function\s*([\w$]+)/;
|
2016-02-15 05:53:17 +01:00
|
|
|
|
var matches;
|
2011-10-20 17:54:50 +02:00
|
|
|
|
|
|
|
|
|
// Replaces: var foo = "bar"; with: self.context.foo = bar;
|
|
|
|
|
matches = scopeVar.exec(cmd);
|
|
|
|
|
if (matches && matches.length === 3) {
|
|
|
|
|
return 'self.context.' + matches[1] + matches[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Replaces: function foo() {}; with: foo = function foo() {};
|
2016-02-15 05:53:17 +01:00
|
|
|
|
matches = scopeFunc.exec(this.bufferedCommand);
|
2011-10-20 17:54:50 +02:00
|
|
|
|
if (matches && matches.length === 2) {
|
2016-02-15 05:53:17 +01:00
|
|
|
|
return matches[1] + ' = ' + this.bufferedCommand;
|
2011-10-20 17:54:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cmd;
|
2016-07-22 00:43:45 +02:00
|
|
|
|
}, 'replServer.convertToContext() is deprecated');
|
2012-10-06 03:33:28 +02:00
|
|
|
|
|
2016-06-02 08:13:45 +02:00
|
|
|
|
function bailOnIllegalToken(parser) {
|
|
|
|
|
return parser._literal === null &&
|
|
|
|
|
!parser.blockComment &&
|
|
|
|
|
!parser.regExpLiteral;
|
|
|
|
|
}
|
2012-10-06 03:33:28 +02: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 23:48:24 +02:00
|
|
|
|
// If the error is that we've unexpectedly ended the input,
|
|
|
|
|
// then let the user try to recover by adding more input.
|
2015-01-19 06:42:05 +01:00
|
|
|
|
function isRecoverableError(e, self) {
|
|
|
|
|
if (e && e.name === 'SyntaxError') {
|
|
|
|
|
var message = e.message;
|
|
|
|
|
if (message === 'Unterminated template literal' ||
|
|
|
|
|
message === 'Missing } in template expression') {
|
|
|
|
|
self._inTemplateLiteral = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-11-14 11:59:22 +01:00
|
|
|
|
|
2016-06-02 08:13:45 +02:00
|
|
|
|
if (message.startsWith('Unexpected end of input') ||
|
2016-03-04 23:13:13 +01:00
|
|
|
|
message.startsWith('missing ) after argument list') ||
|
|
|
|
|
message.startsWith('Unexpected token'))
|
2016-06-02 08:13:45 +02:00
|
|
|
|
return true;
|
|
|
|
|
|
2016-03-04 23:13:13 +01:00
|
|
|
|
if (message === 'Invalid or unexpected token')
|
|
|
|
|
return !bailOnIllegalToken(self.lineParser);
|
2015-01-19 06:42:05 +01:00
|
|
|
|
}
|
|
|
|
|
return false;
|
2014-06-07 08:00:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Recoverable(err) {
|
|
|
|
|
this.err = err;
|
2012-10-06 03:33:28 +02:00
|
|
|
|
}
|
2014-06-07 08:00:55 +02:00
|
|
|
|
inherits(Recoverable, SyntaxError);
|
2015-10-22 18:38:40 +02:00
|
|
|
|
exports.Recoverable = Recoverable;
|