Skip to content

Commit

Permalink
detect early implementations of %TypedArray%.prototype.toSpliced
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jun 25, 2022
1 parent 1b7d33d commit cdbd392
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
18 changes: 17 additions & 1 deletion packages/core-js/modules/esnext.typed-array.to-spliced.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var toAbsoluteIndex = require('../internals/to-absolute-index');
var toBigInt = require('../internals/to-big-int');
var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
var uncurryThis = require('../internals/function-uncurry-this');
var fails = require('../internals/fails');

var aTypedArray = ArrayBufferViewCore.aTypedArray;
var getTypedArrayConstructor = ArrayBufferViewCore.getTypedArrayConstructor;
Expand All @@ -14,6 +15,21 @@ var push = uncurryThis([].push);
var max = Math.max;
var min = Math.min;

// some early implementations, like WebKit, does not follow the final semantic
var PROPER_ORDER = !fails(function () {
// eslint-disable-next-line es-x/no-typed-arrays -- required for testing
var array = new Int8Array([1]);

var spliced = array.toSpliced(1, 0, {
valueOf: function () {
array[0] = 2;
return 3;
}
});

return spliced[0] !== 2 || spliced[1] !== 3;
});

// `%TypedArray%.prototype.toSpliced` method
// https://tc39.es/proposal-change-array-by-copy/#sec-%typedarray%.prototype.toSpliced
exportTypedArrayMethod('toSpliced', function toSpliced(start, deleteCount /* , ...items */) {
Expand Down Expand Up @@ -48,4 +64,4 @@ exportTypedArrayMethod('toSpliced', function toSpliced(start, deleteCount /* , .
for (; k < newLen; k++) A[k] = O[k + actualDeleteCount - insertCount];

return A;
});
}, !PROPER_ORDER);
11 changes: 10 additions & 1 deletion tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,16 @@ GLOBAL.tests = {
return Int8Array.prototype.toSorted;
},
'esnext.typed-array.to-spliced': function () {
return Int8Array.prototype.toSpliced;
var array = new Int8Array([1]);

var spliced = array.toSpliced(1, 0, {
valueOf: function () {
array[0] = 2;
return 3;
}
});

return spliced[0] === 2 && spliced[1] === 3;
},
'esnext.typed-array.unique-by': function () {
return Int8Array.prototype.uniqueBy;
Expand Down
11 changes: 10 additions & 1 deletion tests/tests/esnext.typed-array.to-spliced.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if (DESCRIPTORS) QUnit.test('%TypedArrayPrototype%.toSpliced', assert => {
assert.name(toSpliced, 'toSpliced', `${ name }::toSpliced name is 'toSpliced'`);
assert.looksNative(toSpliced, `${ name }::toSpliced looks native`);

const array = new TypedArray([1, 2, 3, 4, 5]);
let array = new TypedArray([1, 2, 3, 4, 5]);
assert.notSame(array.toSpliced(2), array, 'immutable');

assert.deepEqual(new TypedArray([1, 2, 3, 4, 5]).toSpliced(2), new TypedArray([1, 2]));
Expand All @@ -20,6 +20,15 @@ if (DESCRIPTORS) QUnit.test('%TypedArrayPrototype%.toSpliced', assert => {
assert.deepEqual(new TypedArray([1, 2, 3, 4, 5]).toSpliced(2, -2), new TypedArray([1, 2, 3, 4, 5]));
assert.deepEqual(new TypedArray([1, 2, 3, 4, 5]).toSpliced(2, 2, 6, 7), new TypedArray([1, 2, 6, 7, 5]));

array = new TypedArray([1]);

assert.deepEqual(array.toSpliced(1, 0, {
valueOf() {
array[0] = 2;
return 3;
},
}), new TypedArray([2, 3]), 'operations order');

assert.throws(() => toSpliced.call(null), TypeError, "isn't generic #1");
assert.throws(() => toSpliced.call(undefined), TypeError, "isn't generic #2");
assert.throws(() => toSpliced.call([1, 2]), TypeError, "isn't generic #3");
Expand Down

0 comments on commit cdbd392

Please sign in to comment.