Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: fix global object in repl #28049

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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