Skip to content

Commit c0e48bf

Browse files
committed
repl: Enable tab completion for global properties
When `useGlobal` is false, tab completion in the repl does not enumerate global properties. Instead of just setting these properties blindly on the global context, e.g. context[prop] = global[prop] Use `Object.defineProperty` and the property descriptor found on `global` for the new property in `context`. Also addresses a previously unnoticed issue where `console` is writable when `useGlobal` is false. If the binary has been built with `./configure --without-intl` then the `Intl` builtin type will not be available in a repl runtime. Check for this in the test. Fixes: #7353 PR-URL: #7369 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent a1059af commit c0e48bf

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

lib/repl.js

+24-10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ const debug = util.debuglog('repl');
3939
const parentModule = module;
4040
const replMap = new WeakMap();
4141

42+
const GLOBAL_OBJECT_PROPERTIES = ['NaN', 'Infinity', 'undefined',
43+
'eval', 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'decodeURI',
44+
'decodeURIComponent', 'encodeURI', 'encodeURIComponent',
45+
'Object', 'Function', 'Array', 'String', 'Boolean', 'Number',
46+
'Date', 'RegExp', 'Error', 'EvalError', 'RangeError',
47+
'ReferenceError', 'SyntaxError', 'TypeError', 'URIError',
48+
'Math', 'JSON'];
49+
const GLOBAL_OBJECT_PROPERTY_MAP = {};
50+
GLOBAL_OBJECT_PROPERTIES.forEach((p) => GLOBAL_OBJECT_PROPERTY_MAP[p] = p);
51+
4252
try {
4353
// hack for require.resolve("./relative") to work properly.
4454
module.filename = path.resolve('repl');
@@ -582,10 +592,20 @@ REPLServer.prototype.createContext = function() {
582592
context = global;
583593
} else {
584594
context = vm.createContext();
585-
for (var i in global) context[i] = global[i];
586-
context.console = new Console(this.outputStream);
587595
context.global = context;
588-
context.global.global = context;
596+
const _console = new Console(this.outputStream);
597+
Object.defineProperty(context, 'console', {
598+
configurable: true,
599+
enumerable: true,
600+
get: () => _console
601+
});
602+
Object.getOwnPropertyNames(global).filter((name) => {
603+
if (name === 'console' || name === 'global') return false;
604+
return GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined;
605+
}).forEach((name) => {
606+
Object.defineProperty(context, name,
607+
Object.getOwnPropertyDescriptor(global, name));
608+
});
589609
}
590610

591611
const module = new Module('<repl>');
@@ -1052,13 +1072,7 @@ REPLServer.prototype.memory = function memory(cmd) {
10521072
function addStandardGlobals(completionGroups, filter) {
10531073
// Global object properties
10541074
// (http://www.ecma-international.org/publications/standards/Ecma-262.htm)
1055-
completionGroups.push(['NaN', 'Infinity', 'undefined',
1056-
'eval', 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'decodeURI',
1057-
'decodeURIComponent', 'encodeURI', 'encodeURIComponent',
1058-
'Object', 'Function', 'Array', 'String', 'Boolean', 'Number',
1059-
'Date', 'RegExp', 'Error', 'EvalError', 'RangeError',
1060-
'ReferenceError', 'SyntaxError', 'TypeError', 'URIError',
1061-
'Math', 'JSON']);
1075+
completionGroups.push(GLOBAL_OBJECT_PROPERTIES);
10621076
// Common keywords. Exclude for completion on the empty string, b/c
10631077
// they just get in the way.
10641078
if (filter) {

test/parallel/test-repl-console.js

+3
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ assert(r.context.console);
1818

1919
// ensure that the repl console instance is not the global one
2020
assert.notStrictEqual(r.context.console, console);
21+
22+
// ensure that the repl console instance does not have a setter
23+
assert.throws(() => r.context.console = 'foo', TypeError);

test/parallel/test-repl-context.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const repl = require('repl');
5+
6+
// Create a dummy stream that does nothing
7+
const stream = new common.ArrayStream();
8+
9+
// Test when useGlobal is false
10+
testContext(repl.start({
11+
input: stream,
12+
output: stream,
13+
useGlobal: false
14+
}));
15+
16+
function testContext(repl) {
17+
const context = repl.createContext();
18+
// ensure that the repl context gets its own "console" instance
19+
assert(context.console instanceof require('console').Console);
20+
21+
// ensure that the repl's global property is the context
22+
assert(context.global === context);
23+
24+
// ensure that the repl console instance does not have a setter
25+
assert.throws(() => context.console = 'foo');
26+
}

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

+16
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,19 @@ putIn.run(['.clear']);
267267
testMe.complete('.b', common.mustCall((error, data) => {
268268
assert.deepStrictEqual(data, [['break'], 'b']);
269269
}));
270+
271+
const testNonGlobal = repl.start({
272+
input: putIn,
273+
output: putIn,
274+
useGlobal: false
275+
});
276+
277+
const builtins = [['Infinity', '', 'Int16Array', 'Int32Array',
278+
'Int8Array'], 'I'];
279+
280+
if (typeof Intl === 'object') {
281+
builtins[0].push('Intl');
282+
}
283+
testNonGlobal.complete('I', common.mustCall((error, data) => {
284+
assert.deepStrictEqual(data, builtins);
285+
}));

0 commit comments

Comments
 (0)