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: #27859

PR-URL: #28561
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR authored and targos committed Jul 20, 2019
1 parent cbd586a commit d611f5a
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 d611f5a

Please sign in to comment.