Skip to content

Commit

Permalink
repl: fix autocomplete when useGlobal is false
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
targos committed Jan 14, 2020
1 parent 7c3ac42 commit 521458a
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ const { setImmediate } = require('timers');
// Lazy-loaded.
let processTopLevelAwait;

const globalBuiltins =
new Set(vm.runInNewContext('Object.getOwnPropertyNames(globalThis)'));

const parentModule = module;
const replMap = new WeakMap();
const domainSet = new WeakSet();
Expand Down Expand Up @@ -907,8 +910,8 @@ REPLServer.prototype.createContext = function() {
context = vm.createContext();
});
for (const name of ObjectGetOwnPropertyNames(global)) {
// Only set properties on the context that do not exist as primordial.
if (!(name in primordials)) {
// Only set properties that do not already exist as a global builtin.
if (!globalBuiltins.has(name)) {
ObjectDefineProperty(context, name,
ObjectGetOwnPropertyDescriptor(global, name));
}
Expand Down Expand Up @@ -1257,8 +1260,14 @@ function complete(line, callback) {
completionGroups.push(
filteredOwnPropertyNames.call(this, contextProto));
}
completionGroups.push(
filteredOwnPropertyNames.call(this, this.context));
const contextOwnNames =
filteredOwnPropertyNames.call(this, this.context);
if (!this.useGlobal) {
// When the context is not `global`, builtins are not own
// properties of it.
contextOwnNames.push(...globalBuiltins);
}
completionGroups.push(contextOwnNames);
if (filter !== '') addCommonWords(completionGroups);
completionGroupsLoaded();
} else {
Expand Down

0 comments on commit 521458a

Please sign in to comment.