Skip to content

Commit 0609c09

Browse files
committed
repl: fix global object in repl
The global object in repl conetxt is copied from main context, which breaks the behavior of `instanceof`. This change reverts nodejs#25731. Fixes: nodejs#27859 Refs: nodejs#25731
1 parent b323658 commit 0609c09

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

lib/repl.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ const kContextId = Symbol('contextId');
109109

110110
let addedNewListener = false;
111111

112+
const GLOBAL_OBJECT_PROPERTIES = [
113+
'NaN', 'Infinity', 'undefined', 'eval', 'parseInt', 'parseFloat', 'isNaN',
114+
'isFinite', 'decodeURI', 'decodeURIComponent', 'encodeURI',
115+
'encodeURIComponent', 'Object', 'Function', 'Array', 'String', 'Boolean',
116+
'Number', 'Date', 'RegExp', 'Error', 'EvalError', 'RangeError',
117+
'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', 'Math', 'JSON'
118+
];
119+
const GLOBAL_OBJECT_PROPERTY_MAP = {};
120+
for (var n = 0; n < GLOBAL_OBJECT_PROPERTIES.length; n++) {
121+
GLOBAL_OBJECT_PROPERTY_MAP[GLOBAL_OBJECT_PROPERTIES[n]] =
122+
GLOBAL_OBJECT_PROPERTIES[n];
123+
}
124+
112125
try {
113126
// Hack for require.resolve("./relative") to work properly.
114127
module.filename = path.resolve('repl');
@@ -874,17 +887,22 @@ REPLServer.prototype.createContext = function() {
874887
}, () => {
875888
context = vm.createContext();
876889
});
877-
for (const name of Object.getOwnPropertyNames(global)) {
878-
Object.defineProperty(context, name,
879-
Object.getOwnPropertyDescriptor(global, name));
880-
}
881890
context.global = context;
882891
const _console = new Console(this.outputStream);
883892
Object.defineProperty(context, 'console', {
884893
configurable: true,
885894
writable: true,
886895
value: _console
887896
});
897+
898+
for (const name of Object.getOwnPropertyNames(global)) {
899+
if (name === 'console' || name === 'global')
900+
continue;
901+
if (GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined) {
902+
Object.defineProperty(context, name,
903+
Object.getOwnPropertyDescriptor(global, name));
904+
}
905+
}
888906
}
889907

890908
const module = new CJSModule('<repl>');
@@ -1455,6 +1473,10 @@ function _memory(cmd) {
14551473
}
14561474

14571475
function addCommonWords(completionGroups) {
1476+
// Global object properties
1477+
// (http://www.ecma-international.org/publications/standards/Ecma-262.htm)
1478+
completionGroups.push(GLOBAL_OBJECT_PROPERTIES);
1479+
14581480
// Only words which do not yet exist as global property should be added to
14591481
// this list.
14601482
completionGroups.push([

test/parallel/test-repl-context.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const stream = new ArrayStream();
2121

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

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

test/parallel/test-repl-tab-complete.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ const testNonGlobal = repl.start({
496496
useGlobal: false
497497
});
498498

499-
const builtins = [['Infinity', 'Int16Array', 'Int32Array',
499+
const builtins = [['Infinity', '', 'Int16Array', 'Int32Array',
500500
'Int8Array'], 'I'];
501501

502502
if (common.hasIntl) {

0 commit comments

Comments
 (0)