Skip to content

Commit e11acc5

Browse files
targosMylesBorins
authored andcommitted
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 <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com>
1 parent a326309 commit e11acc5

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();
@@ -935,8 +938,8 @@ REPLServer.prototype.createContext = function() {
935938
context = vm.createContext();
936939
});
937940
for (const name of ObjectGetOwnPropertyNames(global)) {
938-
// Only set properties on the context that do not exist as primordial.
939-
if (!(name in primordials)) {
941+
// Only set properties that do not already exist as a global builtin.
942+
if (!globalBuiltins.has(name)) {
940943
ObjectDefineProperty(context, name,
941944
ObjectGetOwnPropertyDescriptor(global, name));
942945
}
@@ -1283,8 +1286,14 @@ function complete(line, callback) {
12831286
completionGroups.push(
12841287
filteredOwnPropertyNames.call(this, contextProto));
12851288
}
1286-
completionGroups.push(
1287-
filteredOwnPropertyNames.call(this, this.context));
1289+
const contextOwnNames =
1290+
filteredOwnPropertyNames.call(this, this.context);
1291+
if (!this.useGlobal) {
1292+
// When the context is not `global`, builtins are not own
1293+
// properties of it.
1294+
contextOwnNames.push(...globalBuiltins);
1295+
}
1296+
completionGroups.push(contextOwnNames);
12881297
if (filter !== '') addCommonWords(completionGroups);
12891298
completionGroupsLoaded();
12901299
} else {

0 commit comments

Comments
 (0)