repl: make built-in modules available by default

Closes #3564.
Closes #4578.
This commit is contained in:
Felix Böhm
2013-01-12 12:07:06 -08:00
committed by Nathan Rajlich
parent 8e311d28b0
commit 9bce5e8f3e
2 changed files with 23 additions and 11 deletions

View File

@@ -218,16 +218,6 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
}
}
// Check if a builtin module name was used and then include it
// if there's no conflict.
if (!(cmd in self.context) && exports._builtinLibs.indexOf(cmd) !== -1) {
var lib = require(cmd);
self.context._ = self.context[cmd] = lib;
self.outputStream.write(self.writer(lib) + '\n');
self.displayPrompt();
return;
}
if (!skipCatchall) {
var evalCmd = self.bufferedCommand + cmd + '\n';
@@ -337,6 +327,24 @@ REPLServer.prototype.createContext = function() {
this.lines = [];
this.lines.level = [];
// make built-in modules available directly
// (loaded lazily)
exports._builtinLibs.forEach(function(name){
Object.defineProperty(context, name, {
get: function(){
var lib = require(name);
context._ = context[name] = lib;
return lib;
},
// allow the creation of other globals with this name
set: function(val){
delete context[name];
context[name] = val;
},
configurable: true
});
});
return context;
};

View File

@@ -172,7 +172,11 @@ function error_test() {
{ client: client_unix, send: '(function () {\n\nreturn 1;\n})()',
expect: '1' },
{ client: client_unix, send: '{\n\na: 1\n}',
expect: '{ a: 1 }' }
expect: '{ a: 1 }' },
{ client: client_unix, send: 'url.format("http://google.com")',
expect: 'http://google.com/' },
{ client: client_unix, send: 'var path = 42; path',
expect: '42' }
]);
}