2017-01-03 22:16:48 +01: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.
|
|
|
|
|
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');
|
2017-09-28 09:16:41 +02:00
|
|
|
const { isTypedArray } = require('internal/util/types');
|
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');
|
2017-05-24 19:49:12 +02:00
|
|
|
const errors = require('internal/errors');
|
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 = {};
|
2017-02-27 18:28:15 +01:00
|
|
|
for (var n = 0; n < GLOBAL_OBJECT_PROPERTIES.length; n++) {
|
|
|
|
GLOBAL_OBJECT_PROPERTY_MAP[GLOBAL_OBJECT_PROPERTIES[n]] =
|
|
|
|
GLOBAL_OBJECT_PROPERTIES[n];
|
|
|
|
}
|
2017-06-14 21:52:15 +02:00
|
|
|
const kBufferedCommandSymbol = Symbol('bufferedCommand');
|
2016-06-22 19:07:59 +02:00
|
|
|
|
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
|
2017-07-15 02:30:28 +02:00
|
|
|
module.paths = Module._nodeModulePaths(module.filename);
|
2015-05-04 10:40:40 +02:00
|
|
|
|
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
|
|
|
|
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.
|
2017-06-17 15:11:45 +02:00
|
|
|
// breakEvalOnSigint affects only the behavior of the default eval().
|
2017-05-24 19:49:12 +02:00
|
|
|
throw new errors.Error('ERR_INVALID_REPL_EVAL_CONFIG');
|
2016-05-08 03:30:23 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
|
2015-11-14 01:40:08 +01:00
|
|
|
function defaultEval(code, context, file, cb) {
|
|
|
|
var err, result, script, wrappedErr;
|
|
|
|
var wrappedCmd = false;
|
|
|
|
var input = code;
|
|
|
|
|
|
|
|
if (/^\s*\{/.test(code) && /\}\s*$/.test(code)) {
|
2016-11-22 23:16:25 +01:00
|
|
|
// 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.
|
2015-11-14 01:40:08 +01:00
|
|
|
code = `(${code.trim()})\n`;
|
|
|
|
wrappedCmd = true;
|
2016-11-22 23:16:25 +01:00
|
|
|
}
|
|
|
|
|
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) &&
|
2017-02-28 02:57:44 +01:00
|
|
|
self.replMode === exports.REPL_MODE_STRICT) {
|
|
|
|
// "void 0" keeps the repl from returning "use strict" as the result
|
|
|
|
// value for statements and declarations that don't return a value.
|
2016-08-17 16:47:13 +02:00
|
|
|
code = `'use strict'; void 0;\n${code}`;
|
2015-04-23 09:35:53 +02:00
|
|
|
}
|
2015-11-14 01:40:08 +01:00
|
|
|
script = vm.createScript(code, {
|
2015-04-23 09:35:53 +02:00
|
|
|
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);
|
2015-11-14 01:40:08 +01:00
|
|
|
if (wrappedCmd) {
|
|
|
|
wrappedCmd = false;
|
2017-02-28 02:57:44 +01:00
|
|
|
// unwrap and try again
|
2015-11-14 01:40:08 +01:00
|
|
|
code = input;
|
2017-02-28 02:57:44 +01:00
|
|
|
wrappedErr = e;
|
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;
|
2015-11-14 01:40:08 +01:00
|
|
|
if (isRecoverableError(error, code))
|
2016-03-07 06:01:33 +01:00
|
|
|
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
|
2017-02-06 20:44:17 +01:00
|
|
|
// quick Ctrl+C doesn't lead to aborting the process completely.
|
2016-05-08 03:30:23 +02:00
|
|
|
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 = {
|
2017-05-18 23:53:20 +02:00
|
|
|
displayErrors: false,
|
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);
|
2017-07-16 17:12:57 +02:00
|
|
|
|
2015-11-25 22:37:43 +01:00
|
|
|
internalUtil.decorateErrorStack(e);
|
2017-07-16 17:12:57 +02:00
|
|
|
const isError = internalUtil.isError(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, '');
|
2017-07-16 17:12:57 +02:00
|
|
|
} else if (isError && 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));
|
|
|
|
}
|
2017-07-16 17:12:57 +02:00
|
|
|
if (isError && e.stack) {
|
|
|
|
top.outputStream.write(`${e.stack}\n`);
|
|
|
|
} else {
|
|
|
|
top.outputStream.write(`Thrown: ${String(e)}\n`);
|
|
|
|
}
|
2017-06-14 21:52:15 +02:00
|
|
|
top.clearBufferedCommand();
|
2015-10-14 06:39:16 +02:00
|
|
|
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();
|
2013-08-28 03:53:39 +02:00
|
|
|
self.lines.level = [];
|
2012-08-24 22:18:19 +02:00
|
|
|
|
2017-06-14 21:52:15 +02:00
|
|
|
self.clearBufferedCommand();
|
|
|
|
Object.defineProperty(this, 'bufferedCommand', {
|
|
|
|
get: util.deprecate(() => self[kBufferedCommandSymbol],
|
|
|
|
'REPLServer.bufferedCommand is deprecated', 'DEP0074'),
|
|
|
|
set: util.deprecate((val) => self[kBufferedCommandSymbol] = val,
|
|
|
|
'REPLServer.bufferedCommand is deprecated', 'DEP0074'),
|
|
|
|
enumerable: true
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
2017-07-13 20:17:33 +02:00
|
|
|
function _parseREPLKeyword(keyword, rest) {
|
|
|
|
var cmd = this.commands[keyword];
|
|
|
|
if (cmd) {
|
|
|
|
cmd.action.call(this, rest);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.parseREPLKeyword = util.deprecate(
|
|
|
|
_parseREPLKeyword,
|
|
|
|
'REPLServer.parseREPLKeyword() is deprecated',
|
|
|
|
'DEP0075');
|
|
|
|
|
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();
|
2017-09-01 18:52:18 +02:00
|
|
|
_turnOffEditorMode(self);
|
2012-03-13 01:29:21 +01:00
|
|
|
|
2017-06-14 21:52:15 +02:00
|
|
|
const cmd = self[kBufferedCommandSymbol];
|
|
|
|
if (!(cmd && cmd.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
|
|
|
|
2017-06-14 21:52:15 +02:00
|
|
|
self.clearBufferedCommand();
|
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);
|
2015-11-14 01:40:08 +01:00
|
|
|
cmd = 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) {
|
2017-06-14 21:52:15 +02:00
|
|
|
self[kBufferedCommandSymbol] += 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-11-14 01:40:08 +01:00
|
|
|
// Check REPL keywords and empty lines against a trimmed line input.
|
|
|
|
const trimmedCmd = cmd.trim();
|
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.
|
2015-11-14 01:40:08 +01:00
|
|
|
if (trimmedCmd) {
|
2017-07-23 18:17:07 +02:00
|
|
|
if (trimmedCmd.charAt(0) === '.' && trimmedCmd.charAt(1) !== '.' &&
|
|
|
|
isNaN(parseFloat(trimmedCmd))) {
|
2015-11-14 01:40:08 +01:00
|
|
|
const matches = trimmedCmd.match(/^\.([^\s]+)\s*(.*)$/);
|
|
|
|
const keyword = matches && matches[1];
|
|
|
|
const rest = matches && matches[2];
|
2017-07-13 20:17:33 +02:00
|
|
|
if (_parseREPLKeyword.call(self, keyword, rest) === true) {
|
2015-11-14 01:40:08 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-06-14 21:52:15 +02:00
|
|
|
if (!self[kBufferedCommandSymbol]) {
|
2015-11-14 01:40:08 +01:00
|
|
|
self.outputStream.write('Invalid REPL keyword\n');
|
|
|
|
finish(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-10-13 16:51:53 +02:00
|
|
|
}
|
2010-05-31 20:50:35 +02:00
|
|
|
|
2017-06-14 21:52:15 +02:00
|
|
|
const evalCmd = self[kBufferedCommandSymbol] + 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);
|
|
|
|
|
2017-06-14 21:52:15 +02:00
|
|
|
if (e && !self[kBufferedCommandSymbol] && 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');
|
2017-06-14 21:52:15 +02:00
|
|
|
self.clearBufferedCommand();
|
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.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) {
|
2015-11-14 01:40:08 +01:00
|
|
|
if (e instanceof Recoverable && !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
|
|
|
|
// ... }
|
2017-06-14 21:52:15 +02:00
|
|
|
self[kBufferedCommandSymbol] += 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
|
2017-06-14 21:52:15 +02:00
|
|
|
self.clearBufferedCommand();
|
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');
|
2017-06-14 21:52:15 +02:00
|
|
|
self.outputStream.write(`${self[kBufferedCommandSymbol]}\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
|
2017-09-01 18:52:18 +02:00
|
|
|
_turnOffEditorMode(self);
|
2016-06-12 10:07:33 +02:00
|
|
|
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');
|
2017-02-28 03:45:53 +01:00
|
|
|
exports.REPL_MODE_MAGIC = exports.REPL_MODE_SLOPPY;
|
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
|
|
|
|
2017-06-14 21:52:15 +02:00
|
|
|
REPLServer.prototype.clearBufferedCommand = function clearBufferedCommand() {
|
|
|
|
this[kBufferedCommandSymbol] = '';
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
});
|
2017-02-27 18:28:15 +01:00
|
|
|
|
|
|
|
var names = Object.getOwnPropertyNames(global);
|
|
|
|
for (var n = 0; n < names.length; n++) {
|
|
|
|
var name = names[n];
|
|
|
|
if (name === 'console' || name === 'global')
|
|
|
|
continue;
|
|
|
|
if (GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined) {
|
|
|
|
Object.defineProperty(context, name,
|
|
|
|
Object.getOwnPropertyDescriptor(global, name));
|
|
|
|
}
|
|
|
|
}
|
2011-10-20 19:00:15 +02:00
|
|
|
}
|
2011-10-20 17:54:50 +02:00
|
|
|
|
2017-04-24 08:21:35 +02:00
|
|
|
var module = new Module('<repl>');
|
2017-01-13 11:22:27 +01:00
|
|
|
module.paths = Module._resolveLookupPaths('<repl>', parentModule, true) || [];
|
2015-12-09 21:55:29 +01:00
|
|
|
|
2017-04-24 08:21:35 +02:00
|
|
|
var require = internalModule.makeRequireFunction(module);
|
2011-10-20 17:54:50 +02:00
|
|
|
context.module = module;
|
|
|
|
context.require = require;
|
|
|
|
|
2017-07-15 05:23:02 +02:00
|
|
|
internalModule.addBuiltinLibsToObject(context);
|
|
|
|
|
|
|
|
return context;
|
|
|
|
};
|
2016-03-02 22:45:16 +01:00
|
|
|
|
2017-07-15 05:23:02 +02:00
|
|
|
REPLServer.prototype.resetContext = function() {
|
|
|
|
this.context = this.createContext();
|
2016-03-02 22:45:16 +01:00
|
|
|
this.underscoreAssigned = false;
|
2011-11-12 02:44:39 +01:00
|
|
|
this.lines = [];
|
|
|
|
this.lines.level = [];
|
|
|
|
|
2017-07-15 05:23:02 +02:00
|
|
|
Object.defineProperty(this.context, '_', {
|
2016-03-02 22:45:16 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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;
|
2017-06-14 21:52:15 +02:00
|
|
|
if (this[kBufferedCommandSymbol].length) {
|
2012-02-19 00:01:35 +01:00
|
|
|
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
|
|
|
|
2017-09-01 18:52:18 +02:00
|
|
|
REPLServer.prototype.turnOffEditorMode = util.deprecate(
|
|
|
|
function() { _turnOffEditorMode(this); },
|
|
|
|
'REPLServer.turnOffEditorMode() is deprecated',
|
2017-09-28 23:35:05 +02:00
|
|
|
'DEP0078');
|
2016-06-12 10:07:33 +02:00
|
|
|
|
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) {
|
2017-02-27 18:28:15 +01:00
|
|
|
for (var n = 0; n < data.length; n++)
|
|
|
|
this.emit('data', `${data[n]}\n`);
|
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
|
|
|
|
2017-06-16 19:14:58 +02:00
|
|
|
const requireRE = /\brequire\s*\(['"](([\w@./-]+\/)?(?:[\w@./-]*))/;
|
2015-01-21 17:36:59 +01:00
|
|
|
const simpleExpressionRE =
|
2017-06-16 19:14:58 +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);
|
|
|
|
}
|
|
|
|
|
2017-06-20 12:49:03 +02:00
|
|
|
const ARRAY_LENGTH_THRESHOLD = 1e6;
|
|
|
|
|
|
|
|
function mayBeLargeObject(obj) {
|
|
|
|
if (Array.isArray(obj)) {
|
|
|
|
return obj.length > ARRAY_LENGTH_THRESHOLD ? ['length'] : null;
|
2017-09-28 09:16:41 +02:00
|
|
|
} else if (isTypedArray(obj)) {
|
2017-06-20 12:49:03 +02:00
|
|
|
return obj.length > ARRAY_LENGTH_THRESHOLD ? [] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-08-17 21:03:32 +02:00
|
|
|
function filteredOwnPropertyNames(obj) {
|
|
|
|
if (!obj) return [];
|
2017-06-20 12:49:03 +02:00
|
|
|
const fakeProperties = mayBeLargeObject(obj);
|
|
|
|
if (fakeProperties !== null) {
|
|
|
|
this.outputStream.write('\r\n');
|
|
|
|
process.emitWarning(
|
|
|
|
'The current array, Buffer or TypedArray has too many entries. ' +
|
|
|
|
'Certain properties may be missing from completion output.',
|
|
|
|
'REPLWarning',
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
true);
|
|
|
|
|
|
|
|
return fakeProperties;
|
|
|
|
}
|
2015-08-17 21:03:32 +02:00
|
|
|
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
|
2017-06-14 21:52:15 +02:00
|
|
|
if (this[kBufferedCommandSymbol] !== undefined &&
|
|
|
|
this[kBufferedCommandSymbol].length) {
|
2017-06-17 15:11:45 +02:00
|
|
|
// Get a new array of inputted lines
|
2011-11-12 02:44:39 +01:00
|
|
|
var tmp = this.lines.slice();
|
|
|
|
// Kill off all function declarations to push all local variables into
|
|
|
|
// global scope
|
2017-02-27 18:28:15 +01:00
|
|
|
for (var n = 0; n < this.lines.level.length; n++) {
|
|
|
|
var kill = this.lines.level[n];
|
|
|
|
if (kill.isFunction)
|
2011-11-12 02:44:39 +01:00
|
|
|
tmp[kill.line] = '';
|
2017-02-27 18:28:15 +01:00
|
|
|
}
|
2011-11-12 02:44:39 +01:00
|
|
|
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));
|
2017-07-15 05:23:02 +02:00
|
|
|
magic.resetContext();
|
2011-11-12 02:44:39 +01:00
|
|
|
flat.run(tmp); // eval the flattened code
|
|
|
|
// all this is only profitable if the nested REPL
|
|
|
|
// does not have a bufferedCommand
|
2017-06-14 21:52:15 +02:00
|
|
|
if (!magic[kBufferedCommandSymbol]) {
|
2011-11-12 02:44:39 +01:00
|
|
|
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").
|
2017-07-01 20:51:22 +02:00
|
|
|
var filter;
|
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);
|
2017-06-16 19:14:58 +02:00
|
|
|
var indexRe = new RegExp('^index(?:' + exts.map(regexpEscape).join('|') +
|
2010-12-02 03:07:20 +01:00
|
|
|
')$');
|
2017-06-08 01:00:33 +02:00
|
|
|
var versionedFileNamesRe = /-\d+\.\d+/;
|
2010-08-17 17:39:54 +02:00
|
|
|
|
|
|
|
completeOn = match[1];
|
2010-12-02 03:07:20 +01:00
|
|
|
var subdir = match[2] || '';
|
2017-07-01 20:51:22 +02:00
|
|
|
filter = match[1];
|
2017-08-10 06:51:56 +02:00
|
|
|
var dir, files, f, name, base, ext, abs, subfiles, s, isDirectory;
|
2010-08-17 17:39:54 +02:00
|
|
|
group = [];
|
2017-07-21 10:30:07 +02:00
|
|
|
let paths = [];
|
|
|
|
|
|
|
|
if (completeOn === '.') {
|
|
|
|
group = ['./', '../'];
|
|
|
|
} else if (completeOn === '..') {
|
|
|
|
group = ['../'];
|
|
|
|
} else if (/^\.\.?\//.test(completeOn)) {
|
|
|
|
paths = [process.cwd()];
|
|
|
|
} else {
|
|
|
|
paths = module.paths.concat(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);
|
2017-06-08 01:00:33 +02:00
|
|
|
if (versionedFileNamesRe.test(base) || name === '.npm') {
|
2010-08-17 17:39:54 +02:00
|
|
|
// Exclude versioned names that 'npm' installs.
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-10 06:51:56 +02:00
|
|
|
abs = path.resolve(dir, name);
|
|
|
|
try {
|
|
|
|
isDirectory = fs.statSync(abs).isDirectory();
|
|
|
|
} catch (e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isDirectory) {
|
|
|
|
group.push(subdir + name + '/');
|
2010-08-17 17:39:54 +02:00
|
|
|
try {
|
2017-08-10 06:51:56 +02:00
|
|
|
subfiles = fs.readdirSync(abs);
|
|
|
|
} catch (e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (s = 0; s < subfiles.length; s++) {
|
|
|
|
if (indexRe.test(subfiles[s])) {
|
|
|
|
group.push(subdir + name);
|
2010-08-17 17:39:54 +02:00
|
|
|
}
|
2017-08-10 06:51:56 +02:00
|
|
|
}
|
|
|
|
} else if (exts.includes(ext) && (!subdir || base !== 'index')) {
|
|
|
|
group.push(subdir + base);
|
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 ''
|
2017-06-08 01:00:33 +02:00
|
|
|
} else if (line.length === 0 || /\w|\.|\$/.test(line[line.length - 1])) {
|
2011-01-02 06:41:07 +01:00
|
|
|
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)) {
|
2017-06-20 12:49:03 +02:00
|
|
|
completionGroups.push(
|
|
|
|
filteredOwnPropertyNames.call(this, contextProto));
|
2012-04-26 05:08:30 +02:00
|
|
|
}
|
2017-06-20 12:49:03 +02:00
|
|
|
completionGroups.push(
|
|
|
|
filteredOwnPropertyNames.call(this, 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
|
2017-02-27 18:28:15 +01:00
|
|
|
for (var n = 0; n < globals.length; n++)
|
|
|
|
completionGroups.push(globals[n]);
|
2011-10-20 17:54:50 +02:00
|
|
|
} 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) {}`;
|
2017-06-20 12:49:03 +02:00
|
|
|
this.eval(evalExpr, this.context, 'repl', (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 {
|
2017-06-20 12:49:03 +02:00
|
|
|
memberGroups.push(filteredOwnPropertyNames.call(this, 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) {
|
2017-06-20 12:49:03 +02:00
|
|
|
memberGroups.push(filteredOwnPropertyNames.call(this, 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
|
|
|
|
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') {
|
2017-07-11 02:55:21 +02:00
|
|
|
cmd = { action: cmd };
|
2015-01-29 02:05:53 +01:00
|
|
|
} else if (typeof cmd.action !== 'function') {
|
2017-05-24 19:49:12 +02:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
|
2017-07-20 23:35:51 +02:00
|
|
|
'action',
|
|
|
|
'function',
|
|
|
|
cmd.action);
|
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,
|
2017-06-08 01:00:33 +02:00
|
|
|
isFunction: /\bfunction\b/.test(cmd)
|
2012-02-19 00:01:35 +01:00
|
|
|
});
|
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
|
|
|
|
2017-09-01 18:52:18 +02:00
|
|
|
function _turnOnEditorMode(repl) {
|
|
|
|
repl.editorMode = true;
|
|
|
|
REPLServer.super_.prototype.setPrompt.call(repl, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
function _turnOffEditorMode(repl) {
|
|
|
|
repl.editorMode = false;
|
|
|
|
repl.setPrompt(repl._initialPrompt);
|
|
|
|
}
|
|
|
|
|
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() {
|
2017-06-14 21:52:15 +02:00
|
|
|
this.clearBufferedCommand();
|
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() {
|
2017-06-14 21:52:15 +02:00
|
|
|
this.clearBufferedCommand();
|
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
|
|
|
|
);
|
2017-02-27 18:28:15 +01:00
|
|
|
for (var n = 0; n < names.length; n++) {
|
|
|
|
var name = names[n];
|
|
|
|
var cmd = this.commands[name];
|
|
|
|
var spaces = ' '.repeat(longestNameLength - name.length + 3);
|
|
|
|
var line = `.${name}${cmd.help ? spaces + cmd.help : ''}\n`;
|
2016-09-13 21:46:57 +02:00
|
|
|
this.outputStream.write(line);
|
2017-02-27 18:28:15 +01:00
|
|
|
}
|
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()) {
|
2017-09-01 18:52:18 +02:00
|
|
|
_turnOnEditorMode(this);
|
2011-11-12 02:44:39 +01:00
|
|
|
var data = fs.readFileSync(file, 'utf8');
|
2016-08-17 16:47:13 +02:00
|
|
|
var lines = data.split('\n');
|
2017-02-27 18:28:15 +01:00
|
|
|
for (var n = 0; n < lines.length; n++) {
|
|
|
|
if (lines[n])
|
|
|
|
this.write(`${lines[n]}\n`);
|
|
|
|
}
|
2017-09-01 18:52:18 +02:00
|
|
|
_turnOffEditorMode(this);
|
2017-08-16 17:31:57 +02:00
|
|
|
this.write('\n');
|
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;
|
2017-09-01 18:52:18 +02:00
|
|
|
_turnOnEditorMode(this);
|
2016-06-12 10:07:33 +02:00
|
|
|
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
|
|
|
|
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-11-14 01:40:08 +01:00
|
|
|
function isRecoverableError(e, code) {
|
2015-01-19 06:42:05 +01:00
|
|
|
if (e && e.name === 'SyntaxError') {
|
|
|
|
var message = e.message;
|
|
|
|
if (message === 'Unterminated template literal' ||
|
|
|
|
message === 'Missing } in template expression') {
|
|
|
|
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')
|
2015-11-14 01:40:08 +01:00
|
|
|
return isCodeRecoverable(code);
|
2015-01-19 06:42:05 +01:00
|
|
|
}
|
|
|
|
return false;
|
2014-06-07 08:00:55 +02:00
|
|
|
}
|
|
|
|
|
2015-11-14 01:40:08 +01:00
|
|
|
// Check whether a code snippet should be forced to fail in the REPL.
|
|
|
|
function isCodeRecoverable(code) {
|
|
|
|
var current, previous, stringLiteral;
|
|
|
|
var isBlockComment = false;
|
|
|
|
var isSingleComment = false;
|
|
|
|
var isRegExpLiteral = false;
|
|
|
|
var lastChar = code.charAt(code.length - 2);
|
|
|
|
var prevTokenChar = null;
|
|
|
|
|
|
|
|
for (var i = 0; i < code.length; i++) {
|
|
|
|
previous = current;
|
|
|
|
current = code[i];
|
|
|
|
|
|
|
|
if (previous === '\\' && (stringLiteral || isRegExpLiteral)) {
|
|
|
|
current = null;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stringLiteral) {
|
|
|
|
if (stringLiteral === current) {
|
|
|
|
stringLiteral = null;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (isRegExpLiteral && current === '/') {
|
|
|
|
isRegExpLiteral = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isBlockComment && previous === '*' && current === '/') {
|
|
|
|
isBlockComment = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSingleComment && current === '\n') {
|
|
|
|
isSingleComment = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isBlockComment || isRegExpLiteral || isSingleComment) continue;
|
|
|
|
|
|
|
|
if (current === '/' && previous === '/') {
|
|
|
|
isSingleComment = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previous === '/') {
|
|
|
|
if (current === '*') {
|
|
|
|
isBlockComment = true;
|
|
|
|
} else if (
|
|
|
|
// Distinguish between a division operator and the start of a regex
|
|
|
|
// by examining the non-whitespace character that precedes the /
|
|
|
|
[null, '(', '[', '{', '}', ';'].includes(prevTokenChar)
|
|
|
|
) {
|
|
|
|
isRegExpLiteral = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current.trim()) prevTokenChar = current;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current === '\'' || current === '"') {
|
|
|
|
stringLiteral = current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringLiteral ? lastChar === '\\' : isBlockComment;
|
|
|
|
}
|
|
|
|
|
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;
|