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');
|
|
|
|
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();
|
|
|
|
|
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
|
|
|
|
2012-03-19 17:08:25 +01:00
|
|
|
exports._builtinLibs = ['assert', 'buffer', 'child_process', 'cluster',
|
2012-12-11 11:04:34 +01:00
|
|
|
'crypto', 'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'https', 'net',
|
2012-12-11 11:14:49 +01:00
|
|
|
'os', 'path', 'punycode', 'querystring', 'readline', 'stream',
|
2015-05-28 16:20:57 +02:00
|
|
|
'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib'];
|
2011-12-25 05:39:57 +01:00
|
|
|
|
2011-01-02 06:41:07 +01:00
|
|
|
|
2015-04-23 09:35:53 +02:00
|
|
|
const BLOCK_SCOPED_ERROR = 'Block-scoped declarations (let, ' +
|
|
|
|
'const, function, class) not yet supported outside strict mode';
|
|
|
|
|
|
|
|
|
2015-10-24 09:46:08 +02:00
|
|
|
class LineParser {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this._literal = null;
|
|
|
|
this.shouldFail = false;
|
|
|
|
this.blockComment = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previous === '/' && current === '*') {
|
|
|
|
this.blockComment = true;
|
|
|
|
previous = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.blockComment) continue;
|
|
|
|
|
|
|
|
if (current === this._literal) {
|
|
|
|
this._literal = null;
|
|
|
|
} else if (current === '\'' || current === '"') {
|
|
|
|
this._literal = this._literal || current;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 === '\\') ||
|
|
|
|
(this._literal && lastChar !== '\\'));
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-03-14 22:16:13 +01:00
|
|
|
var options, input, output, dom;
|
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;
|
2015-01-29 02:05:53 +01:00
|
|
|
} else if (typeof prompt !== 'string') {
|
2012-03-27 00:21:25 +02:00
|
|
|
throw new Error('An options Object, or a prompt String are required');
|
|
|
|
} else {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
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;
|
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;
|
|
|
|
|
|
|
|
function defaultEval(code, context, file, cb) {
|
2015-04-23 09:35:53 +02:00
|
|
|
var err, result, retry = false;
|
2013-08-28 03:53:39 +02:00
|
|
|
// first, create the Script object to check the syntax
|
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.
|
|
|
|
code = `'use strict'; void 0; ${code}`;
|
|
|
|
}
|
|
|
|
var script = vm.createScript(code, {
|
|
|
|
filename: file,
|
|
|
|
displayErrors: false
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
debug('parse error %j', code, e);
|
|
|
|
if (self.replMode === exports.REPL_MODE_MAGIC &&
|
|
|
|
e.message === BLOCK_SCOPED_ERROR &&
|
|
|
|
!retry) {
|
|
|
|
retry = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isRecoverableError(e, self))
|
|
|
|
err = new Recoverable(e);
|
|
|
|
else
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
try {
|
|
|
|
if (self.useGlobal) {
|
|
|
|
result = script.runInThisContext({ displayErrors: false });
|
|
|
|
} else {
|
|
|
|
result = script.runInContext(context, { displayErrors: false });
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
err = 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
|
|
|
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`
|
|
|
|
for (let idx = 1; idx < savedRegExMatches.length; idx += 1) {
|
|
|
|
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_);
|
|
|
|
|
|
|
|
self._domain.on('error', function(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);
|
2015-10-14 06:39:16 +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
|
|
|
|
2011-09-07 09:39:49 +02:00
|
|
|
function complete(text, callback) {
|
|
|
|
self.complete(text, callback);
|
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,
|
|
|
|
completer: complete,
|
|
|
|
terminal: options.terminal,
|
|
|
|
historySize: options.historySize
|
|
|
|
});
|
2014-02-15 15:21:26 +01:00
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
self.setPrompt(prompt !== undefined ? prompt : '> ');
|
2010-09-17 06:07:22 +02: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
|
|
|
|
2014-02-15 15:21:26 +01:00
|
|
|
self.setPrompt(self._prompt);
|
2010-05-31 20:50:35 +02:00
|
|
|
|
2014-02-15 15:21:26 +01:00
|
|
|
self.on('close', function() {
|
2012-03-27 21:41:42 +02:00
|
|
|
self.emit('exit');
|
|
|
|
});
|
|
|
|
|
2011-04-21 21:17:21 +02:00
|
|
|
var sawSIGINT = false;
|
2014-02-15 15:21:26 +01:00
|
|
|
self.on('SIGINT', function() {
|
|
|
|
var empty = self.line.length === 0;
|
|
|
|
self.clearLine();
|
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;
|
|
|
|
}
|
2015-11-07 09:18:40 +01: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
|
|
|
});
|
|
|
|
|
2014-02-15 15:21:26 +01:00
|
|
|
self.on('line', function(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;
|
2010-12-03 13:45:00 +01:00
|
|
|
var skipCatchall = false;
|
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) {
|
2011-01-06 22:42:30 +01:00
|
|
|
self.outputStream.write('Invalid REPL keyword\n');
|
2010-12-03 13:45:00 +01:00
|
|
|
skipCatchall = true;
|
|
|
|
}
|
2010-10-13 16:51:53 +02:00
|
|
|
}
|
2010-05-31 20:50:35 +02:00
|
|
|
|
2015-07-12 23:48:50 +02:00
|
|
|
if (!skipCatchall && (cmd || (!cmd && self.bufferedCommand))) {
|
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
|
|
|
var evalCmd = self.bufferedCommand + cmd;
|
|
|
|
if (/^\s*\{/.test(evalCmd) && /\}\s*$/.test(evalCmd)) {
|
|
|
|
// 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.
|
|
|
|
evalCmd = '(' + evalCmd + ')\n';
|
|
|
|
} else {
|
|
|
|
// otherwise we just append a \n so that it will be either
|
|
|
|
// terminated, or continued onto the next expression if it's an
|
|
|
|
// unexpected end of input.
|
|
|
|
evalCmd = evalCmd + '\n';
|
|
|
|
}
|
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
|
|
|
debug('eval %j', evalCmd);
|
2015-10-24 09:46:08 +02:00
|
|
|
self.eval(evalCmd, self.context, 'repl', finish);
|
2011-09-08 11:03:27 +02:00
|
|
|
} else {
|
2015-10-24 09:46:08 +02:00
|
|
|
finish(null);
|
2011-09-07 09:39:49 +02:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
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 && !self.bufferedCommand && cmd.trim().match(/^npm /)) {
|
|
|
|
self.outputStream.write('npm should be run outside of the ' +
|
|
|
|
'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) {
|
2015-10-24 09:46:08 +02:00
|
|
|
if (e instanceof Recoverable && !self.lineParser.shouldFail) {
|
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
|
|
|
|
// ... }
|
|
|
|
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 = '';
|
|
|
|
|
|
|
|
// 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)) {
|
2011-10-20 17:54:50 +02:00
|
|
|
self.context._ = ret;
|
2012-03-28 02:39:14 +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
|
|
|
});
|
|
|
|
|
2014-02-15 15:21:26 +01:00
|
|
|
self.on('SIGCONT', function() {
|
2012-05-22 00:46:57 +02:00
|
|
|
self.displayPrompt(true);
|
2010-04-12 01:13:32 +02:00
|
|
|
});
|
2010-05-31 20:50:35 +02:00
|
|
|
|
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
|
|
|
|
2015-10-07 08:05:53 +02:00
|
|
|
REPLServer.prototype.close = function replClose() {
|
|
|
|
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();
|
2011-10-20 19:00:15 +02:00
|
|
|
for (var i in global) context[i] = global[i];
|
2012-08-24 22:18:19 +02:00
|
|
|
context.console = new Console(this.outputStream);
|
|
|
|
context.global = context;
|
|
|
|
context.global.global = context;
|
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;
|
|
|
|
|
2011-11-12 02:44:39 +01:00
|
|
|
this.lines = [];
|
|
|
|
this.lines.level = [];
|
|
|
|
|
2013-01-12 21:07:06 +01:00
|
|
|
// make built-in modules available directly
|
|
|
|
// (loaded lazily)
|
2013-01-12 21:14:39 +01:00
|
|
|
exports._builtinLibs.forEach(function(name) {
|
2013-01-12 21:07:06 +01:00
|
|
|
Object.defineProperty(context, name, {
|
2013-01-12 21:14:39 +01:00
|
|
|
get: function() {
|
2013-01-12 21:07:06 +01:00
|
|
|
var lib = require(name);
|
|
|
|
context._ = context[name] = lib;
|
|
|
|
return lib;
|
|
|
|
},
|
|
|
|
// allow the creation of other globals with this name
|
2013-01-12 21:14:39 +01:00
|
|
|
set: function(val) {
|
2013-01-12 21:07:06 +01:00
|
|
|
delete context[name];
|
|
|
|
context[name] = val;
|
|
|
|
},
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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 = '...';
|
|
|
|
var levelInd = new Array(this.lines.level.length).join('..');
|
|
|
|
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
|
|
|
|
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) {
|
2012-04-06 21:54:48 +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
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const requireRE = /\brequire\s*\(['"](([\w\.\/-]+\/)?([\w\.\/-]*))/;
|
|
|
|
const simpleExpressionRE =
|
2011-01-02 06:41:07 +01:00
|
|
|
/(([a-zA-Z_$](?:\w|\$)*)\.)*([a-zA-Z_$](?:\w|\$)*)\.?$/;
|
|
|
|
|
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
|
|
|
|
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.
|
2011-09-07 09:39:49 +02:00
|
|
|
REPLServer.prototype.complete = function(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
|
|
|
|
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) {
|
2015-10-14 06:39:16 +02:00
|
|
|
replMap.set(magic, replMap.get(this));
|
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
|
|
|
|
2013-08-15 23:55:05 +02:00
|
|
|
var completeOn, match, filter, 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;
|
|
|
|
match = line.match(/^\s*(\.\w*)$/);
|
|
|
|
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];
|
|
|
|
if (match[1].length > 1) {
|
|
|
|
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 {
|
|
|
|
this.eval('.scope', this.context, 'repl', function(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 {
|
2011-10-20 17:54:50 +02:00
|
|
|
this.eval(expr, this.context, 'repl', function(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) {
|
|
|
|
return elem.indexOf(filter) == 0;
|
|
|
|
});
|
|
|
|
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]);
|
|
|
|
}
|
2010-08-09 11:18:32 +02:00
|
|
|
};
|
|
|
|
|
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?
|
|
|
|
self.lines.push(new Array(self.lines.level.length).join(' ') + cmd);
|
|
|
|
} 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?
|
|
|
|
// TODO? if a log was kept, then I could clear the bufferedComand and
|
|
|
|
// 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)
|
|
|
|
completionGroups.push(['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']);
|
|
|
|
// Common keywords. Exclude for completion on the empty string, b/c
|
|
|
|
// they just get in the way.
|
|
|
|
if (filter) {
|
|
|
|
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']);
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
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', {
|
|
|
|
help: 'Show repl options',
|
|
|
|
action: function() {
|
|
|
|
var self = this;
|
|
|
|
Object.keys(this.commands).sort().forEach(function(name) {
|
|
|
|
var cmd = self.commands[name];
|
2011-01-06 22:42:30 +01:00
|
|
|
self.outputStream.write(name + '\t' + (cmd.help || '') + '\n');
|
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 {
|
|
|
|
fs.writeFileSync(file, this.lines.join('\n') + '\n');
|
|
|
|
this.outputStream.write('Session saved to:' + file + '\n');
|
|
|
|
} catch (e) {
|
2012-02-19 00:01:35 +01: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');
|
|
|
|
var lines = data.split('\n');
|
|
|
|
this.displayPrompt();
|
2012-02-19 00:01:35 +01:00
|
|
|
lines.forEach(function(line) {
|
2011-11-12 02:44:39 +01:00
|
|
|
if (line) {
|
2014-02-15 15:21:26 +01:00
|
|
|
self.write(line + '\n');
|
2011-11-12 02:44:39 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.outputStream.write('Failed to load:' + file + '\n');
|
|
|
|
}
|
|
|
|
this.displayPrompt();
|
|
|
|
}
|
|
|
|
});
|
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.
|
|
|
|
*/
|
|
|
|
REPLServer.prototype.convertToContext = function(cmd) {
|
2015-01-21 17:36:59 +01:00
|
|
|
const scopeVar = /^\s*var\s*([_\w\$]+)(.*)$/m;
|
|
|
|
const scopeFunc = /^\s*function\s*([_\w\$]+)/;
|
|
|
|
var self = this, 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() {};
|
|
|
|
matches = scopeFunc.exec(self.bufferedCommand);
|
|
|
|
if (matches && matches.length === 2) {
|
|
|
|
return matches[1] + ' = ' + self.bufferedCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd;
|
|
|
|
};
|
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;
|
|
|
|
}
|
2014-12-15 21:21:54 +01:00
|
|
|
return /^(Unexpected end of input|Unexpected token)/.test(message);
|
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);
|