diff --git a/lib/repl.js b/lib/repl.js index 4b10f6bb48..c349bbcb40 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -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; }; diff --git a/test/simple/test-repl.js b/test/simple/test-repl.js index 944d7a2385..793346775b 100644 --- a/test/simple/test-repl.js +++ b/test/simple/test-repl.js @@ -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' } ]); }