Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib(repl): add more primordials instead of builtin #36262

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 38 additions & 25 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
'use strict';

const {
ArrayPrototypeFilter,
ArrayPrototypeMap,
ArrayPrototypePush,
Error,
MathMax,
NumberIsNaN,
Expand All @@ -62,6 +65,7 @@ const {
StringPrototypeCharAt,
StringPrototypeIncludes,
StringPrototypeMatch,
StringPrototypeStartsWith,
Symbol,
SyntaxError,
SyntaxErrorPrototype,
Expand Down Expand Up @@ -92,7 +96,7 @@ const {
const { Console } = require('console');
const CJSModule = require('internal/modules/cjs/loader').Module;
let _builtinLibs = [...CJSModule.builtinModules]
.filter((e) => !e.startsWith('_') && !e.includes('/'));
.filter((e) => !StringPrototypeStartsWith(e, '_') && !e.includes('/'));
const domain = require('domain');
let debug = require('internal/util/debuglog').debuglog('repl', (fn) => {
debug = fn;
Expand Down Expand Up @@ -703,7 +707,7 @@ function REPLServer(prompt,

self.on('close', function emitExit() {
if (paused) {
pausedBuffer.push(['close']);
ArrayPrototypePush(pausedBuffer, ['close']);
return;
}
self.emit('exit');
Expand Down Expand Up @@ -796,7 +800,8 @@ function REPLServer(prompt,
debug('finish', e, ret);
_memory.call(self, cmd);

if (e && !self[kBufferedCommandSymbol] && cmd.trim().startsWith('npm ')) {
if (e && !self[kBufferedCommandSymbol] &&
StringPrototypeStartsWith(cmd.trim(), 'npm ')) {
self.output.write('npm should be run outside of the ' +
'Node.js REPL, in your normal shell.\n' +
'(Press Ctrl+D to exit.)\n');
Expand Down Expand Up @@ -869,7 +874,8 @@ function REPLServer(prompt,
self._ttyWrite = (d, key) => {
key = key || {};
if (paused && !(self.breakEvalOnSigint && key.ctrl && key.name === 'c')) {
pausedBuffer.push(['key', [d, key], self.isCompletionEnabled]);
ArrayPrototypePush(pausedBuffer,
['key', [d, key], self.isCompletionEnabled]);
return;
}
if (!self.editorMode || !self.terminal) {
Expand Down Expand Up @@ -1124,9 +1130,9 @@ function completeFSFunctions(line) {
fileList = gracefulReaddir(filePath, { withFileTypes: true }) || [];
}

const completions = fileList
.filter((dirent) => dirent.name.startsWith(baseName))
.map((d) => d.name);
const completions = ArrayPrototypeMap(ArrayPrototypeFilter(
fileList, (dirent) => StringPrototypeStartsWith(dirent.name, baseName)
), (d) => d.name);

return [[completions], baseName];
}
Expand Down Expand Up @@ -1160,8 +1166,10 @@ function complete(line, callback) {
} else if (requireRE.test(line)) {
// require('...<Tab>')
const extensions = ObjectKeys(this.context.require.extensions);
const indexes = extensions.map((extension) => `index${extension}`);
indexes.push('package.json', 'index');
const indexes = ArrayPrototypeMap(
extensions,
(extension) => `index${extension}`);
ArrayPrototypePush(indexes, 'package.json', 'index');
const versionedFileNamesRe = /-\d+\.\d+/;

const match = line.match(requireRE);
Expand Down Expand Up @@ -1193,7 +1201,7 @@ function complete(line, callback) {
const base = dirent.name.slice(0, -extension.length);
if (!dirent.isDirectory()) {
if (extensions.includes(extension) && (!subdir || base !== 'index')) {
group.push(`${subdir}${base}`);
ArrayPrototypePush(group, `${subdir}${base}`);
}
continue;
}
Expand All @@ -1202,18 +1210,18 @@ function complete(line, callback) {
const subfiles = gracefulReaddir(absolute) || [];
for (const subfile of subfiles) {
if (indexes.includes(subfile)) {
group.push(`${subdir}${dirent.name}`);
ArrayPrototypePush(group, `${subdir}${dirent.name}`);
break;
}
}
}
}
if (group.length) {
completionGroups.push(group);
ArrayPrototypePush(completionGroups, group);
}

if (!subdir) {
completionGroups.push(_builtinLibs);
ArrayPrototypePush(completionGroups, _builtinLibs);
}
} else if (fsAutoCompleteRE.test(line)) {
[completionGroups, completeOn] = completeFSFunctions(line);
Expand Down Expand Up @@ -1249,13 +1257,14 @@ function complete(line, callback) {
completionGroups.push(getGlobalLexicalScopeNames(this[kContextId]));
let contextProto = this.context;
while (contextProto = ObjectGetPrototypeOf(contextProto)) {
completionGroups.push(filteredOwnPropertyNames(contextProto));
ArrayPrototypePush(completionGroups,
filteredOwnPropertyNames(contextProto));
}
const contextOwnNames = filteredOwnPropertyNames(this.context);
if (!this.useGlobal) {
// When the context is not `global`, builtins are not own
// properties of it.
contextOwnNames.push(...globalBuiltins);
ArrayPrototypePush(contextOwnNames, ...globalBuiltins);
}
completionGroups.push(contextOwnNames);
if (filter !== '') addCommonWords(completionGroups);
Expand All @@ -1276,15 +1285,15 @@ function complete(line, callback) {
let p;
if ((typeof obj === 'object' && obj !== null) ||
typeof obj === 'function') {
memberGroups.push(filteredOwnPropertyNames(obj));
ArrayPrototypePush(memberGroups, filteredOwnPropertyNames(obj));
p = ObjectGetPrototypeOf(obj);
} else {
p = obj.constructor ? obj.constructor.prototype : null;
}
// Circular refs possible? Let's guard against that.
let sentinel = 5;
while (p !== null && sentinel-- !== 0) {
memberGroups.push(filteredOwnPropertyNames(p));
ArrayPrototypePush(memberGroups, filteredOwnPropertyNames(p));
p = ObjectGetPrototypeOf(p);
}
} catch {
Expand All @@ -1297,7 +1306,9 @@ function complete(line, callback) {
if (memberGroups.length) {
expr += chaining;
for (const group of memberGroups) {
completionGroups.push(group.map((member) => `${expr}${member}`));
ArrayPrototypePush(
completionGroups,
ArrayPrototypeMap(group, (member) => `${expr}${member}`));
}
if (filter) {
filter = `${expr}${filter}`;
Expand All @@ -1318,9 +1329,11 @@ function complete(line, callback) {
if (completionGroups.length && filter) {
const newCompletionGroups = [];
for (const group of completionGroups) {
const filteredGroup = group.filter((str) => str.startsWith(filter));
const filteredGroup = ArrayPrototypeFilter(
group,
(str) => StringPrototypeStartsWith(str, filter));
if (filteredGroup.length) {
newCompletionGroups.push(filteredGroup);
ArrayPrototypePush(newCompletionGroups, filteredGroup);
}
}
completionGroups = newCompletionGroups;
Expand Down Expand Up @@ -1388,10 +1401,10 @@ function _memory(cmd) {
// Save the line so I can do magic later
if (cmd) {
const len = self.lines.level.length ? self.lines.level.length - 1 : 0;
self.lines.push(' '.repeat(len) + cmd);
ArrayPrototypePush(self.lines, ' '.repeat(len) + cmd);
} else {
// I don't want to not change the format too much...
self.lines.push('');
ArrayPrototypePush(self.lines, '');
}

if (!cmd) {
Expand Down Expand Up @@ -1421,7 +1434,7 @@ function _memory(cmd) {
// "function()
// {" but nothing should break, only tab completion for local
// scope will not work for this function.
self.lines.level.push({
ArrayPrototypePush(self.lines.level, {
line: self.lines.length - 1,
depth: depth
});
Expand All @@ -1437,7 +1450,7 @@ function _memory(cmd) {
} else if (tmp > 0) {
// Remove and push back
curr.depth += depth;
self.lines.level.push(curr);
ArrayPrototypePush(self.lines.level, curr);
}
}
}
Expand All @@ -1448,7 +1461,7 @@ function _memory(cmd) {
function addCommonWords(completionGroups) {
// Only words which do not yet exist as global property should be added to
// this list.
completionGroups.push([
ArrayPrototypePush(completionGroups, [
'async', 'await', 'break', 'case', 'catch', 'const', 'continue',
'debugger', 'default', 'delete', 'do', 'else', 'export', 'false',
'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'let',
Expand Down