summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/sort_errors.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/tests/non262/TypedArray/sort_errors.js
parentInitial commit. (diff)
downloadfirefox-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 '')
-rw-r--r--js/src/tests/non262/TypedArray/sort_errors.js87
1 files changed, 87 insertions, 0 deletions
diff --git a/js/src/tests/non262/TypedArray/sort_errors.js b/js/src/tests/non262/TypedArray/sort_errors.js
new file mode 100644
index 0000000000..f3bdc05b20
--- /dev/null
+++ b/js/src/tests/non262/TypedArray/sort_errors.js
@@ -0,0 +1,87 @@
+// Ensure that TypedArrays throw when attempting to sort a detached ArrayBuffer
+if (typeof detachArrayBuffer === "function") {
+ assertThrowsInstanceOf(() => {
+ let buffer = new ArrayBuffer(32);
+ let array = new Int32Array(buffer);
+ detachArrayBuffer(buffer);
+ array.sort();
+ }, TypeError);
+}
+
+// Ensure detaching buffer in comparator doesn't throw an error.
+if (typeof detachArrayBuffer === "function") {
+ let detached = false;
+ let ta = new Int32Array(3);
+ ta.sort(function(a, b) {
+ if (!detached) {
+ detached = true;
+ detachArrayBuffer(ta.buffer);
+ }
+ return a - b;
+ });
+ assertEq(detached, true);
+}
+
+// Ensure detachment check doesn't choke on wrapped typed array.
+if (typeof newGlobal === "function") {
+ let ta = new Int32Array(3);
+ let otherGlobal = newGlobal();
+ otherGlobal.Int32Array.prototype.sort.call(ta, function(a, b) {
+ return a - b;
+ });
+}
+
+// Ensure detaching buffer in comparator doesn't throw an error when the typed array is wrapped.
+if (typeof newGlobal === "function" && typeof detachArrayBuffer === "function") {
+ let detached = false;
+ let ta = new Int32Array(3);
+ let otherGlobal = newGlobal();
+ otherGlobal.Int32Array.prototype.sort.call(ta, function(a,b) {
+ if (!detached) {
+ detached = true;
+ detachArrayBuffer(ta.buffer);
+ }
+ return a - b;
+ });
+ assertEq(detached, true);
+}
+
+// Ensure that TypedArray.prototype.sort will not sort non-TypedArrays
+assertThrowsInstanceOf(() => {
+ let array = [4, 3, 2, 1];
+ Int32Array.prototype.sort.call(array);
+}, TypeError);
+
+assertThrowsInstanceOf(() => {
+ Int32Array.prototype.sort.call({a: 1, b: 2});
+}, TypeError);
+
+assertThrowsInstanceOf(() => {
+ Int32Array.prototype.sort.call(Int32Array.prototype);
+}, TypeError);
+
+assertThrowsInstanceOf(() => {
+ let buf = new ArrayBuffer(32);
+ Int32Array.prototype.sort.call(buf);
+}, TypeError);
+
+// Ensure that comparator errors are propagataed
+function badComparator(x, y) {
+ if (x == 99 && y == 99)
+ throw new TypeError;
+ return x - y;
+}
+
+assertThrowsInstanceOf(() => {
+ let array = new Uint8Array([99, 99, 99, 99]);
+ array.sort(badComparator);
+}, TypeError);
+
+assertThrowsInstanceOf(() => {
+ let array = new Uint8Array([1, 99, 2, 99]);
+ array.sort(badComparator);
+}, TypeError);
+
+
+if (typeof reportCompare === "function")
+ reportCompare(true, true);