summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/sort_errors.js
blob: f3bdc05b2012ebd68950878661d8ab5750096300 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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);