Skip to content

Commit d611f5a

Browse files
BridgeARtargos
authored andcommitted
repl: fix some repl context issues
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>
1 parent cbd586a commit d611f5a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/repl.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,11 @@ REPLServer.prototype.createContext = function() {
875875
context = vm.createContext();
876876
});
877877
for (const name of Object.getOwnPropertyNames(global)) {
878-
Object.defineProperty(context, name,
879-
Object.getOwnPropertyDescriptor(global, name));
878+
// Only set properties on the context that do not exist as primordial.
879+
if (!(name in primordials)) {
880+
Object.defineProperty(context, name,
881+
Object.getOwnPropertyDescriptor(global, name));
882+
}
880883
}
881884
context.global = context;
882885
const _console = new Console(this.outputStream);

test/parallel/test-repl-context.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ const stream = new ArrayStream();
1616
useGlobal: false
1717
});
1818

19+
let output = '';
20+
stream.write = function(d) {
21+
output += d;
22+
};
23+
1924
// Ensure that the repl context gets its own "console" instance.
2025
assert(r.context.console);
2126

2227
// Ensure that the repl console instance is not the global one.
2328
assert.notStrictEqual(r.context.console, console);
29+
assert.notStrictEqual(r.context.Object, Object);
30+
31+
stream.run(['({} instanceof Object)']);
32+
33+
assert.strictEqual(output, 'true\n> ');
2434

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

0 commit comments

Comments
 (0)