From b1e78cef097c682ed63528ad7efe294b18a9fb1d Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 12 Oct 2012 16:34:36 -0700 Subject: 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. --- lib/repl.js | 23 +++++++++-------------- test/simple/test-repl.js | 7 ++++++- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index af0d422a4..ba334cfda 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 c673cd502..aeca81c09 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() { -- cgit v1.2.1