repl: make built-in modules available by default
authorFelix Böhm <me@feedic.com>
Sat, 12 Jan 2013 20:07:06 +0000 (12:07 -0800)
committerNathan Rajlich <nathan@tootallnate.net>
Sat, 12 Jan 2013 20:10:29 +0000 (12:10 -0800)
Closes #3564.
Closes #4578.

lib/repl.js
test/simple/test-repl.js

index 4b10f6b..c349bbc 100644 (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;
 };
 
index 944d7a2..7933467 100644 (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' }
   ]);
 }