repl: support non-array .scope, document it

REPL evaluate `.scope` when it needs to get a list of the variable names
available in the current scope. Do not throw if the output of such
evaluation is not array, just ignore it.

PR-URL: https://github.com/nodejs/io.js/pull/1682
Reviewed-By: Fedor Indutny <fedor@indutny.com>
This commit is contained in:
Yazhong Liu 2015-05-12 16:00:35 +08:00 committed by Fedor Indutny
parent 7c52e1c1f4
commit 6edc900b95
3 changed files with 23 additions and 1 deletions

View File

@ -90,6 +90,9 @@ You can use your own `eval` function if it has following signature:
callback(null, result); callback(null, result);
} }
On tab completion - `eval` will be called with `.scope` as an input string. It
is expected to return an array of scope names to be used for the auto-completion.
Multiple REPLs may be started against the same running instance of io.js. Each Multiple REPLs may be started against the same running instance of io.js. Each
will share the same global object but will have unique I/O. will share the same global object but will have unique I/O.
@ -232,4 +235,5 @@ The following key combinations in the REPL have these special effects:
- `<ctrl>C` - Similar to the `.break` keyword. Terminates the current - `<ctrl>C` - Similar to the `.break` keyword. Terminates the current
command. Press twice on a blank line to forcibly exit. command. Press twice on a blank line to forcibly exit.
- `<ctrl>D` - Similar to the `.exit` keyword. - `<ctrl>D` - Similar to the `.exit` keyword.
- `<tab>` - Show both global and local(scope) variables

View File

@ -624,7 +624,7 @@ REPLServer.prototype.complete = function(line, callback) {
completionGroupsLoaded(); completionGroupsLoaded();
} else { } else {
this.eval('.scope', this.context, 'repl', function(err, globals) { this.eval('.scope', this.context, 'repl', function(err, globals) {
if (err || !globals) { if (err || !globals || !Array.isArray(globals)) {
addStandardGlobals(completionGroups, filter); addStandardGlobals(completionGroups, filter);
} else if (Array.isArray(globals[0])) { } else if (Array.isArray(globals[0])) {
// Add grouped globals // Add grouped globals

View File

@ -0,0 +1,18 @@
var assert = require('assert');
var util = require('util');
var repl = require('repl');
var zlib = require('zlib');
// just use builtin stream inherited from Duplex
var putIn = zlib.createGzip();
var testMe = repl.start('', putIn, function(cmd, context, filename, callback) {
callback(null, cmd);
});
testMe._domain.on('error', function (e) {
assert.fail();
});
testMe.complete('', function(err, results) {
assert.equal(err, null);
});