Skip to content

Commit 521458a

Browse files
committed
repl: fix autocomplete when useGlobal is false
This fixes two issues in the REPL when it is started with a new context (useGlobal option set to `false`): - The `primordials` object does not contain all builtins, so the filtering based on property names from `primordials` was wrong. - The autocompleter did not take builtin names into account because they are not properties of the context object. A list of all global builtin names is created lazily when needed. It is used for filtering for the copy and for adding those names to the autocompleter list. Fixes: #30792 PR-URL: #30883 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: David Carlier <[email protected]>
1 parent 7c3ac42 commit 521458a

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lib/repl.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ const { setImmediate } = require('timers');
119119
// Lazy-loaded.
120120
let processTopLevelAwait;
121121

122+
const globalBuiltins =
123+
new Set(vm.runInNewContext('Object.getOwnPropertyNames(globalThis)'));
124+
122125
const parentModule = module;
123126
const replMap = new WeakMap();
124127
const domainSet = new WeakSet();
@@ -907,8 +910,8 @@ REPLServer.prototype.createContext = function() {
907910
context = vm.createContext();
908911
});
909912
for (const name of ObjectGetOwnPropertyNames(global)) {
910-
// Only set properties on the context that do not exist as primordial.
911-
if (!(name in primordials)) {
913+
// Only set properties that do not already exist as a global builtin.
914+
if (!globalBuiltins.has(name)) {
912915
ObjectDefineProperty(context, name,
913916
ObjectGetOwnPropertyDescriptor(global, name));
914917
}
@@ -1257,8 +1260,14 @@ function complete(line, callback) {
12571260
completionGroups.push(
12581261
filteredOwnPropertyNames.call(this, contextProto));
12591262
}
1260-
completionGroups.push(
1261-
filteredOwnPropertyNames.call(this, this.context));
1263+
const contextOwnNames =
1264+
filteredOwnPropertyNames.call(this, this.context);
1265+
if (!this.useGlobal) {
1266+
// When the context is not `global`, builtins are not own
1267+
// properties of it.
1268+
contextOwnNames.push(...globalBuiltins);
1269+
}
1270+
completionGroups.push(contextOwnNames);
12621271
if (filter !== '') addCommonWords(completionGroups);
12631272
completionGroupsLoaded();
12641273
} else {

0 commit comments

Comments
 (0)