Repository navigation
add tests for TA.prototype.with/filter shrinking/detaching buffer during coercion #5136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
test/built-ins/TypedArray/prototype/filter/BigInt/callbackfn-detachbuffer-kept-undefined.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
test/built-ins/TypedArray/prototype/filter/BigInt/callbackfn-shrink-kept-undefined.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
test/built-ins/TypedArray/prototype/with/BigInt/index-coercion-shrinks.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"]); |
30 changes: 30 additions & 0 deletions
30
test/built-ins/TypedArray/prototype/with/BigInt/negative-index-resize-to-out-of-bounds.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"]); |
35 changes: 35 additions & 0 deletions
35
test/built-ins/TypedArray/prototype/with/BigInt/value-coercion-shrinks.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"]); |
35 changes: 35 additions & 0 deletions
35
test/built-ins/TypedArray/prototype/with/index-coercion-shrinks.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"]); |
35 changes: 35 additions & 0 deletions
35
test/built-ins/TypedArray/prototype/with/value-coercion-shrinks.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"]); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.