diff options
author | Nathan Rajlich <nathan@tootallnate.net> | 2012-10-12 16:34:36 -0700 |
---|---|---|
committer | Nathan Rajlich <nathan@tootallnate.net> | 2012-10-12 16:37:17 -0700 |
commit | b1e78cef097c682ed63528ad7efe294b18a9fb1d (patch) | |
tree | 8b1126a15cb418695073216af1c272acba3b1714 | |
parent | 323bbdb0cb2518ff15ad361ad3495cc4739f3bdf (diff) | |
download | node-new-b1e78cef097c682ed63528ad7efe294b18a9fb1d.tar.gz |
repl: ensure each REPL instance gets its own "context"
Before there was this weird module-scoped "context" variable which seemingly
shared the "context" of subsequent REPL instances, unless ".clear" was invoked
inside the REPL. To be proper, we need to ensure that each REPL gets its own
"context" object. I literally don't know why this "sharing" behavior was in place
before, but it was just plain wrong.
-rw-r--r-- | lib/repl.js | 23 | ||||
-rw-r--r-- | test/simple/test-repl.js | 7 |
2 files changed, 15 insertions, 15 deletions
diff --git a/lib/repl.js b/lib/repl.js index af0d422a44..ba334cfda9 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -57,8 +57,6 @@ function hasOwnProperty(obj, prop) { } -var context; - // hack for require.resolve("./relative") to work properly. module.filename = path.resolve('repl'); @@ -327,11 +325,12 @@ exports.start = function(prompt, source, eval_, useGlobal, ignoreUndefined) { REPLServer.prototype.createContext = function() { - if (!this.useGlobal) { - var context = vm.createContext(); - for (var i in global) context[i] = global[i]; + var context; + if (this.useGlobal) { + context = global; } else { - var context = global; + context = vm.createContext(); + for (var i in global) context[i] = global[i]; } context.module = module; @@ -345,13 +344,9 @@ REPLServer.prototype.createContext = function() { return context; }; -REPLServer.prototype.resetContext = function(force) { - if (!context || force) { - context = this.createContext(); - for (var i in require.cache) delete require.cache[i]; - } - - this.context = context; +REPLServer.prototype.resetContext = function() { + for (var i in require.cache) delete require.cache[i]; + this.context = this.createContext(); }; REPLServer.prototype.displayPrompt = function(preserveCursor) { @@ -800,7 +795,7 @@ function defineDefaultCommands(repl) { this.bufferedCommand = ''; if (!this.useGlobal) { this.outputStream.write('Clearing context...\n'); - this.resetContext(true); + this.resetContext(); } this.displayPrompt(); } diff --git a/test/simple/test-repl.js b/test/simple/test-repl.js index c673cd5024..aeca81c095 100644 --- a/test/simple/test-repl.js +++ b/test/simple/test-repl.js @@ -241,7 +241,12 @@ function unix_test() { socket.end(); }); - repl.start(prompt_unix, socket).context.message = message; + repl.start({ + prompt: prompt_unix, + input: socket, + output: socket, + useGlobal: true + }).context.message = message; }); server_unix.on('listening', function() { |