diff --git a/lib/repl.js b/lib/repl.js index 98b0d2415d4f70..4d40da4daabd95 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -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(); @@ -935,8 +938,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)); } @@ -1283,8 +1286,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 {