From 5cf0eaa097070d27491ed940f8edb51546f66533 Mon Sep 17 00:00:00 2001
From: Yash Ladha <yashladhapankajladha123@gmail.com>
Date: Wed, 25 Nov 2020 20:52:06 +0530
Subject: [PATCH] lib(repl): add more primordials instead of builtin

---
 lib/repl.js | 63 ++++++++++++++++++++++++++++++++---------------------
 1 file changed, 38 insertions(+), 25 deletions(-)

diff --git a/lib/repl.js b/lib/repl.js
index 78c256d60b5559..0525c2789f0c24 100644
--- a/lib/repl.js
+++ b/lib/repl.js
@@ -43,6 +43,9 @@
 'use strict';
 
 const {
+  ArrayPrototypeFilter,
+  ArrayPrototypeMap,
+  ArrayPrototypePush,
   Error,
   MathMax,
   NumberIsNaN,
@@ -62,6 +65,7 @@ const {
   StringPrototypeCharAt,
   StringPrototypeIncludes,
   StringPrototypeMatch,
+  StringPrototypeStartsWith,
   Symbol,
   SyntaxError,
   SyntaxErrorPrototype,
@@ -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;
@@ -703,7 +707,7 @@ function REPLServer(prompt,
 
   self.on('close', function emitExit() {
     if (paused) {
-      pausedBuffer.push(['close']);
+      ArrayPrototypePush(pausedBuffer, ['close']);
       return;
     }
     self.emit('exit');
@@ -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');
@@ -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) {
@@ -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];
 }
@@ -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);
@@ -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;
         }
@@ -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);
@@ -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);
@@ -1276,7 +1285,7 @@ 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;
@@ -1284,7 +1293,7 @@ function complete(line, callback) {
         // 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 {
@@ -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}`;
@@ -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;
@@ -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) {
@@ -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
         });
@@ -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);
           }
         }
       }
@@ -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',