Skip to content

Commit 5ee105c

Browse files
targosBethGriggs
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 <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: David Carlier <[email protected]>
1 parent 3348fe4 commit 5ee105c

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

‎lib/repl.js‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ const{setImmediate } = require('timers');
119119
// Lazy-loaded.
120120
letprocessTopLevelAwait;
121121

122+
constglobalBuiltins=
123+
newSet(vm.runInNewContext('Object.getOwnPropertyNames(globalThis)'));
124+
122125
constparentModule=module;
123126
constreplMap=newWeakMap();
124127
constdomainSet=newWeakSet();
@@ -907,8 +910,8 @@ REPLServer.prototype.createContext = function(){
907910
context=vm.createContext();
908911
});
909912
for(constnameofObjectGetOwnPropertyNames(global)){
910-
// Only set properties on the context that do not exist as primordial.
911-
if(!(nameinprimordials)){
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+
constcontextOwnNames=
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)