Skip to content

Commit

Permalink
lib: the REPL should survive deletion of Array.prototype methods
Browse files Browse the repository at this point in the history
Specifically, `delete Array.prototype.lastIndexOf` immediately crashes
the REPL, as does deletion of a few other Array prototype methods.
  • Loading branch information
ljharb committed Dec 28, 2020
1 parent d146b25 commit 12756b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const {
ObjectDefineProperty,
ReflectApply,
SafeMap,
StringPrototypeRepeat,
Symbol,
} = primordials;

Expand Down Expand Up @@ -118,7 +119,7 @@ const domainRequireStack = new Error('require(`domain`) at this point').stack;
const { setUncaughtExceptionCaptureCallback } = process;
process.setUncaughtExceptionCaptureCallback = function(fn) {
const err = new ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE();
err.stack = err.stack + '\n' + '-'.repeat(40) + '\n' + domainRequireStack;
err.stack += `\n${StringPrototypeRepeat('-', 40)}\n${domainRequireStack}`;
throw err;
};

Expand Down
15 changes: 9 additions & 6 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const {
ArrayPrototypeMap,
ArrayPrototypePop,
ArrayPrototypePush,
ArrayPrototypeReduce,
ArrayPrototypeReverse,
ArrayPrototypeShift,
ArrayPrototypeSort,
Expand Down Expand Up @@ -91,7 +92,7 @@ const {
StringPrototypeSplit,
StringPrototypeStartsWith,
StringPrototypeTrim,
StringPrototypeTrimLeft,
StringPrototypeTrimStart,
Symbol,
SyntaxError,
SyntaxErrorPrototype,
Expand Down Expand Up @@ -1195,7 +1196,7 @@ function complete(line, callback) {
let completeOn, group;

// Ignore right whitespace. It could change the outcome.
line = StringPrototypeTrimLeft(line);
line = StringPrototypeTrimStart(line);

// REPL commands (e.g. ".break").
let filter = '';
Expand Down Expand Up @@ -1332,15 +1333,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 Down Expand Up @@ -1566,8 +1567,10 @@ function defineDefaultCommands(repl) {
help: 'Print this help message',
action: function() {
const names = ArrayPrototypeSort(ObjectKeys(this.commands));
const longestNameLength = MathMax(
...ArrayPrototypeMap(names, (name) => name.length)
const longestNameLength = ArrayPrototypeReduce(
names,
(max, name) => MathMax(max, name.length),
0
);
for (let n = 0; n < names.length; n++) {
const name = names[n];
Expand Down

0 comments on commit 12756b9

Please sign in to comment.