Skip to content

Commit

Permalink
Add coverage for value coercions in TypedArray.p.with
Browse files Browse the repository at this point in the history
  • Loading branch information
anba authored and Ms2ger committed Jan 13, 2025
1 parent d49db63 commit ab5c086
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/built-ins/TypedArray/prototype/with/order-of-evaluation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2025 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-%typedarray%.prototype.with
description: >
Index parameter is coerced before value parameter.
info: |
%TypedArray%.prototype.with ( index, value )
...
4. Let relativeIndex be ? ToIntegerOrInfinity(index).
...
8. Else, let numericValue be ? ToNumber(value).
...
features: [TypedArray, change-array-by-copy]
includes: [testTypedArray.js, compareArray.js]
---*/

testWithTypedArrayConstructors(TA => {
var ta = new TA(1);

var logs = [];

var index = {
valueOf() {
logs.push("index");
return 0;
}
};

var value = {
valueOf() {
logs.push("value");
return 0;
}
};

ta.with(index, value);

assert.compareArray(logs, ["index", "value"]);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2025 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-%typedarray%.prototype.with
description: >
IsValidIntegerIndex is checked after all coercions happened.
info: |
%TypedArray%.prototype.with ( index, value )
...
8. Else, let numericValue be ? ToNumber(value).
9. If IsValidIntegerIndex(O, 𝔽(actualIndex)) is false, throw a RangeError exception.
...
features: [TypedArray, change-array-by-copy, resizable-arraybuffer]
includes: [testTypedArray.js]
---*/

testWithTypedArrayConstructors(TA => {
var rab = new ArrayBuffer(0, {maxByteLength: TA.BYTES_PER_ELEMENT});
var ta = new TA(rab);
assert.sameValue(ta.length, 0);

var value = {
valueOf() {
rab.resize(TA.BYTES_PER_ELEMENT);
return 0;
}
};

var result = ta.with(0, value);

assert.sameValue(result.length, 0);
assert.sameValue(rab.byteLength, TA.BYTES_PER_ELEMENT);
});

0 comments on commit ab5c086

Please sign in to comment.