Browse Source

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.
v0.8.13-release
Nathan Rajlich 12 years ago
parent
commit
b1e78cef09
  1. 23
      lib/repl.js
  2. 7
      test/simple/test-repl.js

23
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();
}

7
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() {

Loading…
Cancel
Save