diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/tests/non262/TypedArray/set-toobject.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/src/tests/non262/TypedArray/set-toobject.js')
-rw-r--r-- | js/src/tests/non262/TypedArray/set-toobject.js | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/js/src/tests/non262/TypedArray/set-toobject.js b/js/src/tests/non262/TypedArray/set-toobject.js new file mode 100644 index 0000000000..360c5dde7e --- /dev/null +++ b/js/src/tests/non262/TypedArray/set-toobject.js @@ -0,0 +1,53 @@ +// Test ToObject in %TypedArray%.prototype.set(array|typedArray, offset). + +let ta = new Int32Array(4); + +for (let nullOrUndefined of [null, undefined]) { + // ToObject(array) throws a TypeError when |array| is null or undefined. + assertThrowsInstanceOf(() => ta.set(nullOrUndefined), TypeError); + + // ToInteger(offset) is called before ToObject(array). + class ExpectedError extends Error {} + assertThrowsInstanceOf(() => ta.set(nullOrUndefined, { + valueOf() { + throw new ExpectedError(); + } + }), ExpectedError); +} + +// Ensure ta is still initialized with zeros. +assertEqArray(ta, [0, 0, 0, 0]); + +// %TypedArray%.prototype.set can be called with a string primitive values. +ta.set(""); +assertEqArray(ta, [0, 0, 0, 0]); + +ta.set("123"); +assertEqArray(ta, [1, 2, 3, 0]); + +// Throws a RangeError if the length is too large. +assertThrowsInstanceOf(() => ta.set("456789"), RangeError); +assertEqArray(ta, [1, 2, 3, 0]); + +// When called with other primitive values the typed array contents don't +// change since ToObject(<primitive>).length is zero, i.e. the source object is +// treated the same as an empty array. +for (let value of [true, false, 0, NaN, 123, Infinity, Symbol()]) { + ta.set(value); + assertEqArray(ta, [1, 2, 3, 0]); +} + +// Repeat test from above when the primitive wrapper prototype has been changed +// to include "length" and an indexed property. +Number.prototype.length = 4; +Number.prototype[3] = -1; +try { + ta.set(456); + assertEqArray(ta, [0, 0, 0, -1]); +} finally { + delete Number.prototype.length; + delete Number.prototype[3]; +} + +if (typeof reportCompare === "function") + reportCompare(true, true); |