From e232521083d229ce07d6ac3b53cc466a4f199ff0 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 9 Jan 2022 13:14:56 -0800 Subject: [PATCH] [Fix] spec change: throw a TypeError when the length is >= 2**53 See https://github.com/tc39/proposal-change-array-by-copy/pull/70 --- implementation.js | 17 +++++++++++------ test/tests.js | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/implementation.js b/implementation.js index 40ff32b..6171676 100644 --- a/implementation.js +++ b/implementation.js @@ -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'); @@ -48,22 +50,25 @@ 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); @@ -71,5 +76,5 @@ module.exports = function toSpliced(start, deleteCount) { k += 1; } - return A; // step 17 + return A; // step 18 }; diff --git a/test/tests.js b/test/tests.js index 68786d5..6b73e0d 100644 --- a/test/tests.js +++ b/test/tests.js @@ -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(); + }); };