Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2026 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-%typedarray%.prototype.filter
description: >
Keeping undefined after the callback detaches the buffer throws a TypeError,
after all callbacks have run.
includes: [detachArrayBuffer.js, testTypedArray.js]
features: [BigInt, TypedArray]
---*/

testWithBigIntTypedArrayConstructors(function(TA, makeCtorArg) {
var sample = new TA(makeCtorArg([1n, 2n, 3n]));
var calls = 0;

assert.throws(TypeError, function() {
sample.filter(function(value, index, array) {
assert.sameValue(index, calls);
assert.sameValue(array, sample);
if (calls === 0) {
assert.sameValue(value, 1n);
$DETACHBUFFER(sample.buffer);
} else {
assert.sameValue(value, undefined,
"callback element is undefined for index " + index);
}
calls++;
return true;
});
});

assert.sameValue(calls, 3, "All callbacks run before converting the kept values");
}, null, null, ["immutable"]);
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,28 @@ info: |
b. Let kValue be ? Get(O, Pk).
c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O »)).
...
includes: [detachArrayBuffer.js, testTypedArray.js]
includes: [detachArrayBuffer.js, testTypedArray.js, compareArray.js]
features: [BigInt, TypedArray]
---*/

testWithBigIntTypedArrayConstructors(function(TA, makeCtorArg) {
var loops = 0;
var sample = new TA(makeCtorArg(2));

sample.filter(function() {
var result = sample.filter(function(value, index) {
var flag = true;
if (loops === 0) {
assert.sameValue(value, 0n);
$DETACHBUFFER(sample.buffer);
} else {
flag = false;
assert.sameValue(value, undefined,
"callback element is undefined for index " + index);
Comment thread
gibson042 marked this conversation as resolved.
flag = false; // returning 0 would throw, which is tested elsehwere
}
loops++;
return flag;
});

assert.sameValue(loops, 2);
assert.compareArray(result, [0n]);
}, null, null, ["immutable"]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2026 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-%typedarray%.prototype.filter
description: >
Keeping undefined after the callback shrinks the buffer throws a TypeError,
after all callbacks have run.
includes: [testTypedArray.js]
features: [BigInt, TypedArray, resizable-arraybuffer]
---*/

testWithBigIntTypedArrayConstructors(function(TA) {
[false, true].forEach(function(fixedLength) {
var buffer = new ArrayBuffer(3 * TA.BYTES_PER_ELEMENT, {
maxByteLength: 3 * TA.BYTES_PER_ELEMENT
});
var sample = fixedLength ? new TA(buffer, 0, 3) : new TA(buffer);
sample[0] = 1n;
sample[1] = 2n;
sample[2] = 3n;
var calls = 0;

assert.throws(TypeError, function() {
sample.filter(function(value, index, array) {
assert.sameValue(index, calls);
assert.sameValue(array, sample);
if (calls === 0) {
assert.sameValue(value, 1n);
buffer.resize(TA.BYTES_PER_ELEMENT);
} else {
assert.sameValue(value, undefined,
"callback element is undefined for index " + index);
}
calls++;
return true;
});
});

assert.sameValue(calls, 3, "All callbacks run before converting the kept values");
});
}, null, ["passthrough"]);
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@ info: |
b. Let kValue be ? Get(O, Pk).
c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O »)).
...
includes: [detachArrayBuffer.js, testTypedArray.js]
includes: [detachArrayBuffer.js, testTypedArray.js, compareArray.js]
features: [TypedArray]
---*/

testWithTypedArrayConstructors(function(TA, makeCtorArg) {
var loops = 0;
var sample = new TA(makeCtorArg(2));

sample.filter(function() {
var result = sample.filter(function(value, index) {
if (loops === 0) {
assert.sameValue(value, 0);
$DETACHBUFFER(sample.buffer);
} else {
assert.sameValue(value, undefined,
"callback element is undefined for index " + index);
Comment thread
gibson042 marked this conversation as resolved.
}
loops++;
return true;
});

assert.sameValue(loops, 2);
var converted = isFloatTypedArrayConstructor(TA) ? NaN : 0;
assert.compareArray(result, [0, converted]);
}, null, null, ["immutable"]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2026 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-%typedarray%.prototype.with
description: >
Copying undefined throws a TypeError after index coercion shrinks the buffer.
includes: [testTypedArray.js]
features: [BigInt, TypedArray, change-array-by-copy, resizable-arraybuffer]
---*/

testWithBigIntTypedArrayConstructors(function(TA) {
var buffer = new ArrayBuffer(3 * TA.BYTES_PER_ELEMENT, {
maxByteLength: 3 * TA.BYTES_PER_ELEMENT
});
var sample = new TA(buffer);
sample[0] = 1n;
sample[1] = 2n;
sample[2] = 3n;
var calls = 0;
var index = {
valueOf: function() {
calls++;
buffer.resize(TA.BYTES_PER_ELEMENT);
return 0;
}
};

// Index 0 remains valid, but copying the remaining elements reads undefined.
assert.throws(TypeError, function() {
sample.with(index, 9n);
});
assert.sameValue(calls, 1, "index is coerced once");
assert.compareArray(sample, [1n]);
}, null, ["passthrough"]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2026 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-%typedarray%.prototype.with
description: >
A negative index is relative to the original length and throws a RangeError
if value coercion shrinks the buffer so that the index is out of bounds.
features: [BigInt, TypedArray, change-array-by-copy, resizable-arraybuffer]
includes: [testTypedArray.js]
---*/

testWithBigIntTypedArrayConstructors(function(TA) {
var byteLength = 2 * TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(byteLength, {maxByteLength: byteLength});
var sample = new TA(buffer);

var value = {
valueOf: function() {
buffer.resize(TA.BYTES_PER_ELEMENT);
return 0n;
}
};

// -1 resolves to index 1 before shrinking. Reject that index before copying
// missing elements, which would instead throw a TypeError for BigInt arrays.
assert.throws(RangeError, function() {
sample.with(-1, value);
});
}, null, ["passthrough"]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2026 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-%typedarray%.prototype.with
description: >
Copying undefined throws a TypeError after value coercion shrinks the buffer.
includes: [testTypedArray.js]
features: [BigInt, TypedArray, change-array-by-copy, resizable-arraybuffer]
---*/

testWithBigIntTypedArrayConstructors(function(TA) {
var buffer = new ArrayBuffer(3 * TA.BYTES_PER_ELEMENT, {
maxByteLength: 3 * TA.BYTES_PER_ELEMENT
});
var sample = new TA(buffer);
sample[0] = 1n;
sample[1] = 2n;
sample[2] = 3n;
var calls = 0;
var value = {
valueOf: function() {
calls++;
buffer.resize(TA.BYTES_PER_ELEMENT);
return 9n;
}
};

// Index 0 remains valid, but copying the remaining elements reads undefined.
assert.throws(TypeError, function() {
sample.with(0, value);
});
assert.sameValue(calls, 1, "value is coerced once");
assert.compareArray(sample, [1n]);
}, null, ["passthrough"]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2026 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-%typedarray%.prototype.with
description: >
Missing elements are converted to NaN or zero after index coercion shrinks the buffer.
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, change-array-by-copy, resizable-arraybuffer]
---*/

testWithTypedArrayConstructors(function(TA) {
var buffer = new ArrayBuffer(3 * TA.BYTES_PER_ELEMENT, {
maxByteLength: 3 * TA.BYTES_PER_ELEMENT
});
var sample = new TA(buffer);
sample[0] = 1;
sample[1] = 2;
sample[2] = 3;
var calls = 0;
var index = {
valueOf: function() {
calls++;
buffer.resize(TA.BYTES_PER_ELEMENT);
return 0;
}
};

// Index 0 remains valid, but copying the remaining elements reads undefined.
var result = sample.with(index, 9);
var converted = isFloatTypedArrayConstructor(TA) ? NaN : 0;
assert.compareArray(result, [9, converted, converted]);
assert.sameValue(calls, 1, "index is coerced once");
assert.compareArray(sample, [1]);
}, null, ["passthrough"]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2026 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-%typedarray%.prototype.with
description: >
Missing elements are converted to NaN or zero after value coercion shrinks the buffer.
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, change-array-by-copy, resizable-arraybuffer]
---*/

testWithTypedArrayConstructors(function(TA) {
var buffer = new ArrayBuffer(3 * TA.BYTES_PER_ELEMENT, {
maxByteLength: 3 * TA.BYTES_PER_ELEMENT
});
var sample = new TA(buffer);
sample[0] = 1;
sample[1] = 2;
sample[2] = 3;
var calls = 0;
var value = {
valueOf: function() {
calls++;
buffer.resize(TA.BYTES_PER_ELEMENT);
return 9;
}
};

// Index 0 remains valid, but copying the remaining elements reads undefined.
var result = sample.with(0, value);
var converted = isFloatTypedArrayConstructor(TA) ? NaN : 0;
assert.compareArray(result, [9, converted, converted]);
assert.sameValue(calls, 1, "value is coerced once");
assert.compareArray(sample, [1]);
}, null, ["passthrough"]);
Loading