Skip to content

Commit

Permalink
[Fix] spec change: throw a TypeError when the length is >= 2**53
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 9, 2022
1 parent 28ed657 commit e232521
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
17 changes: 11 additions & 6 deletions implementation.js
Expand Up @@ -16,6 +16,8 @@ var forEach = require('es-abstract/helpers/forEach');

var max = GetIntrinsic('%Math.max%');
var min = GetIntrinsic('%Math.min%');
var $MAX_SAFE_INTEGER = require('es-abstract/helpers/maxSafeInteger');
var $TypeError = GetIntrinsic('%TypeError%');

var $slice = callBound('Array.prototype.slice');

Expand Down Expand Up @@ -48,28 +50,31 @@ module.exports = function toSpliced(start, deleteCount) {

var newLen = len + insertCount - actualDeleteCount; // step 11

var A = ArrayCreate(newLen); // step 12
var k = 0; // step 13
while (k < actualStart) { // step 14
if (newLen > $MAX_SAFE_INTEGER) {
throw new $TypeError('Length exceeded the maximum array length');
}
var A = ArrayCreate(newLen); // step 13
var k = 0; // step 14
while (k < actualStart) { // step 15
var Pk = ToString(k);
var kValue = Get(O, Pk);
CreateDataPropertyOrThrow(A, Pk, kValue);
k += 1;
}
/* eslint no-shadow: 1, no-redeclare: 1 */
forEach(items, function (E) { // step 15
forEach(items, function (E) { // step 16
var Pk = ToString(k);
CreateDataPropertyOrThrow(A, Pk, E);
k += 1;
});

while (k < newLen) { // step 16
while (k < newLen) { // step 17
var Pk = ToString(k);
var from = ToString(k + actualDeleteCount - insertCount);
var fromValue = Get(O, from);
CreateDataPropertyOrThrow(A, Pk, fromValue);
k += 1;
}

return A; // step 17
return A; // step 18
};
16 changes: 16 additions & 0 deletions test/tests.js
Expand Up @@ -166,4 +166,20 @@ module.exports = function (toSpliced, t) {

st.end();
});

t.test('too-large length', function (st) {
st['throws'](
function () { toSpliced({ length: Math.pow(2, 53) - 1 }, 0, 0, 1); },
TypeError,
'throws the proper kind of error for >= 2**53'
);

st['throws'](
function () { toSpliced({ length: Math.pow(2, 32) - 1 }, 0, 0, 1); },
RangeError,
'throws the proper kind of error for [2**32, 2**53]'
);

st.end();
});
};

0 comments on commit e232521

Please sign in to comment.