Skip to content

Commit

Permalink
repl: fix global object in repl
Browse files Browse the repository at this point in the history
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
  • Loading branch information
starkwang committed Jun 4, 2019
1 parent b323658 commit 0609c09
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
30 changes: 26 additions & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ const kContextId = Symbol('contextId');

let addedNewListener = false;

const GLOBAL_OBJECT_PROPERTIES = [
'NaN', 'Infinity', 'undefined', 'eval', 'parseInt', 'parseFloat', 'isNaN',
'isFinite', 'decodeURI', 'decodeURIComponent', 'encodeURI',
'encodeURIComponent', 'Object', 'Function', 'Array', 'String', 'Boolean',
'Number', 'Date', 'RegExp', 'Error', 'EvalError', 'RangeError',
'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', 'Math', 'JSON'
];
const GLOBAL_OBJECT_PROPERTY_MAP = {};
for (var n = 0; n < GLOBAL_OBJECT_PROPERTIES.length; n++) {
GLOBAL_OBJECT_PROPERTY_MAP[GLOBAL_OBJECT_PROPERTIES[n]] =
GLOBAL_OBJECT_PROPERTIES[n];
}

try {
// Hack for require.resolve("./relative") to work properly.
module.filename = path.resolve('repl');
Expand Down Expand Up @@ -874,17 +887,22 @@ REPLServer.prototype.createContext = function() {
}, () => {
context = vm.createContext();
});
for (const name of Object.getOwnPropertyNames(global)) {
Object.defineProperty(context, name,
Object.getOwnPropertyDescriptor(global, name));
}
context.global = context;
const _console = new Console(this.outputStream);
Object.defineProperty(context, 'console', {
configurable: true,
writable: true,
value: _console
});

for (const name of Object.getOwnPropertyNames(global)) {
if (name === 'console' || name === 'global')
continue;
if (GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined) {
Object.defineProperty(context, name,
Object.getOwnPropertyDescriptor(global, name));
}
}
}

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

function addCommonWords(completionGroups) {
// Global object properties
// (http://www.ecma-international.org/publications/standards/Ecma-262.htm)
completionGroups.push(GLOBAL_OBJECT_PROPERTIES);

// Only words which do not yet exist as global property should be added to
// this list.
completionGroups.push([
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-repl-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const stream = new ArrayStream();

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

const context = r.createContext();
// Ensure that the repl context gets its own "console" instance.
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-tab-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ const testNonGlobal = repl.start({
useGlobal: false
});

const builtins = [['Infinity', 'Int16Array', 'Int32Array',
const builtins = [['Infinity', '', 'Int16Array', 'Int32Array',
'Int8Array'], 'I'];

if (common.hasIntl) {
Expand Down

0 comments on commit 0609c09

Please sign in to comment.