Skip to content

Commit

Permalink
repl: fix some repl context issues
Browse files Browse the repository at this point in the history
This partially fixes contexts like `{} instanceof Object === false`
in the REPL. This does not fix all cases, since it's something
fundamental from the REPL's design that things like these can happen.

Refs: nodejs#27859
  • Loading branch information
BridgeAR committed Jul 5, 2019
1 parent bf7edaa commit 166f5d4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,11 @@ REPLServer.prototype.createContext = function() {
context = vm.createContext();
});
for (const name of Object.getOwnPropertyNames(global)) {
Object.defineProperty(context, name,
Object.getOwnPropertyDescriptor(global, name));
// Only set properties on the context that do not exist as primordial.
if (!(name in primordials)) {
Object.defineProperty(context, name,
Object.getOwnPropertyDescriptor(global, name));
}
}
context.global = context;
const _console = new Console(this.outputStream);
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-repl-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@ const stream = new ArrayStream();
useGlobal: false
});

let output = '';
stream.write = function(d) {
output += d;
};

// Ensure that the repl context gets its own "console" instance.
assert(r.context.console);

// Ensure that the repl console instance is not the global one.
assert.notStrictEqual(r.context.console, console);
assert.notStrictEqual(r.context.Object, Object);

stream.run(['({} instanceof Object)']);

assert.strictEqual(output, 'true\n> ');

const context = r.createContext();
// Ensure that the repl context gets its own "console" instance.
Expand Down

0 comments on commit 166f5d4

Please sign in to comment.