summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/self-hosting/is-possibly-wrapped-typed-array.js
blob: 5e76aad69848a5959bff3ec6ab50e4890a295f1c (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var IsPossiblyWrappedTypedArray = getSelfHostedValue("IsPossiblyWrappedTypedArray");

var declareSamples = `
    var allTypedArraySamples = [
        { value: new Int8Array(1), expected: true },
        { value: new Uint8Array(1), expected: true },
        { value: new Int16Array(1), expected: true },
        { value: new Uint16Array(1), expected: true },
        { value: new Int32Array(1), expected: true },
        { value: new Uint32Array(1), expected: true },
        { value: new Float32Array(1), expected: true },
        { value: new Float64Array(1), expected: true },
        { value: new Uint8ClampedArray(1), expected: true }
    ];

    var allObjectSamples = [
        { value: new Array(1), expected: false },
        { value: {}, expected: false },
        { value: { length: 1 }, expected: false }
    ];

    var allNonObjectSamples = [
        { value: "a", expected: false },
        { value: 1.2, expected: false },
        { value: true, expected: false },
        { value: Symbol("a"), expected: false }
    ];
`;

// Create a new global to wrap with cross compartment wrappers.
var g = newGlobal();
evaluate(declareSamples)
g.evaluate(declareSamples);

var assertCode = `function (value, expected) {
    assertEq(IsPossiblyWrappedTypedArray(value), expected);
    return inIon();
}`;

function checkSamples(samples) {
    // Create the assert function anew every run so as not to share JIT code,
    // type information, etc.
    var assert = new Function(`return (${assertCode})`)();

    // Prevent Ion compilation of this function so that we don't freeze the
    // sample array's type.  If we did, IonBuilder's typed-array-length inlining
    // would always see a Mixed state, preventing IsPossiblyWrappedTypedArray
    // from being inlined.
    with ({}) {};

    do {
        // spinInJit is used to ensure that we at least test all elements in the
        // sample vector while running a compiled version of the assert
        // function.
        var spinInJit = true;
        for (var i = 0; i < samples.length; i++) {
            var e = samples[i];
            if (!e) continue;
            spinInJit = spinInJit && assert(e.value, e.expected);
        }
    } while(!spinInJit);
}

// Check a mix of samples from each type.
function test(a, b, c, d, e, f) {
    var samples = [
        a == -1 ? null : allTypedArraySamples[a],
        b == -1 ? null : allObjectSamples[b],
        c == -1 ? null : allNonObjectSamples[c],
        d == -1 ? null : g.allTypedArraySamples[d],
        e == -1 ? null : g.allObjectSamples[e],
        f == -1 ? null : g.allNonObjectSamples[f]
    ];

    checkSamples(samples);
}

// Check all samples.
checkSamples(allTypedArraySamples);
checkSamples(allObjectSamples);
checkSamples(allNonObjectSamples);
checkSamples(g.allTypedArraySamples);
checkSamples(g.allObjectSamples);
checkSamples(g.allNonObjectSamples);

// Check combinations mixing 2 elements from different types.
test(-1, -1, -1, -1,  0,  0);
test(-1, -1, -1,  0, -1,  0);
test(-1, -1, -1,  0,  0, -1);
test(-1, -1,  0, -1, -1,  0);
test(-1, -1,  0, -1,  0, -1);
test(-1, -1,  0,  0, -1, -1);
test(-1,  0, -1, -1, -1,  0);
test(-1,  0, -1, -1,  0, -1);
test(-1,  0, -1,  0, -1, -1);
test(-1,  0,  0, -1, -1, -1);
test( 0, -1, -1, -1, -1,  0);
test( 0, -1, -1, -1,  0, -1);
test( 0, -1, -1,  0, -1, -1);
test( 0, -1,  0, -1, -1, -1);
test( 0,  0, -1, -1, -1, -1);

// Check combinations mixing 3 elements from different types.
test(-1, -1, -1,  0,  0,  0);
test(-1, -1,  0, -1,  0,  0);
test(-1, -1,  0,  0, -1,  0);
test(-1, -1,  0,  0,  0, -1);
test(-1,  0, -1, -1,  0,  0);
test(-1,  0, -1,  0, -1,  0);
test(-1,  0, -1,  0,  0, -1);
test(-1,  0,  0, -1, -1,  0);
test(-1,  0,  0, -1,  0, -1);
test(-1,  0,  0,  0, -1, -1);
test( 0, -1, -1, -1,  0,  0);
test( 0, -1, -1,  0, -1,  0);
test( 0, -1, -1,  0,  0, -1);
test( 0, -1,  0, -1, -1,  0);
test( 0, -1,  0, -1,  0, -1);
test( 0, -1,  0,  0, -1, -1);
test( 0,  0, -1, -1, -1,  0);
test( 0,  0, -1, -1,  0, -1);
test( 0,  0, -1,  0, -1, -1);
test( 0,  0,  0, -1, -1, -1);

// Check combinations mixing 4 elements from different types.
test(-1, -1,  0,  0,  0,  0);
test(-1,  0, -1,  0,  0,  0);
test(-1,  0,  0, -1,  0,  0);
test(-1,  0,  0,  0, -1,  0);
test(-1,  0,  0,  0,  0, -1);
test( 0, -1, -1,  0,  0,  0);
test( 0, -1,  0, -1,  0,  0);
test( 0, -1,  0,  0, -1,  0);
test( 0, -1,  0,  0,  0, -1);
test( 0,  0, -1, -1,  0,  0);
test( 0,  0, -1,  0, -1,  0);
test( 0,  0, -1,  0,  0, -1);
test( 0,  0,  0, -1, -1,  0);
test( 0,  0,  0, -1,  0, -1);
test( 0,  0,  0,  0, -1, -1);

// Check combinations mixing 5 elements from different types.
test(-1,  0,  0,  0,  0,  0);
test( 0, -1,  0,  0,  0,  0);
test( 0,  0, -1,  0,  0,  0);
test( 0,  0,  0, -1,  0,  0);
test( 0,  0,  0,  0, -1,  0);
test( 0,  0,  0,  0,  0, -1);

// Check combinations mixing 6 elements from different types.
test( 0,  0,  0,  0,  0,  0);