summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/spec/gc/br_on_cast_fail.wast.js
blob: 4d402f280308996659b31597c563dee69343afb0 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* Copyright 2021 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// ./test/core/gc/br_on_cast_fail.wast

// ./test/core/gc/br_on_cast_fail.wast:3
let $0 = instantiate(`(module
  (type $$ft (func (result i32)))
  (type $$st (struct (field i16)))
  (type $$at (array i8))

  (table 10 anyref)

  (elem declare func $$f)
  (func $$f (result i32) (i32.const 9))

  (func (export "init") (param $$x externref)
    (table.set (i32.const 0) (ref.null any))
    (table.set (i32.const 1) (ref.i31 (i32.const 7)))
    (table.set (i32.const 2) (struct.new $$st (i32.const 6)))
    (table.set (i32.const 3) (array.new $$at (i32.const 5) (i32.const 3)))
    (table.set (i32.const 4) (any.convert_extern (local.get $$x)))
  )

  (func (export "br_on_non_null") (param $$i i32) (result i32)
    (block $$l (result (ref any))
      (br_on_non_null $$l (table.get (local.get $$i)))
      (return (i32.const 0))
    )
    (return (i32.const -1))
  )
  (func (export "br_on_non_i31") (param $$i i32) (result i32)
    (block $$l (result anyref)
      (br_on_cast_fail $$l anyref (ref i31) (table.get (local.get $$i)))
      (return (i31.get_u))
    )
    (return (i32.const -1))
  )
  (func (export "br_on_non_struct") (param $$i i32) (result i32)
    (block $$l (result anyref)
      (br_on_cast_fail $$l anyref (ref struct) (table.get (local.get $$i)))
      (block $$l2 (param structref) (result (ref $$st))
        (block $$l3 (param structref) (result (ref $$at))
          (br_on_cast $$l2 structref (ref $$st))
          (br_on_cast $$l3 anyref (ref $$at))
          (return (i32.const -2))
        )
        (return (array.get_u $$at (i32.const 0)))
      )
      (return (struct.get_s $$st 0))
    )
    (return (i32.const -1))
  )
  (func (export "br_on_non_array") (param $$i i32) (result i32)
    (block $$l (result anyref)
      (br_on_cast_fail $$l anyref (ref array) (table.get (local.get $$i)))
      (return (array.len))
    )
    (return (i32.const -1))
  )

  (func (export "null-diff") (param $$i i32) (result i32)
    (block $$l (result (ref any))
      (block (result (ref null struct))
        (br_on_cast_fail $$l (ref null any) (ref null struct) (table.get (local.get $$i)))
      )
      (return (i32.const 1))
    )
    (return (i32.const 0))
  )
)`);

// ./test/core/gc/br_on_cast_fail.wast:69
invoke($0, `init`, [externref(0)]);

// ./test/core/gc/br_on_cast_fail.wast:71
assert_return(() => invoke($0, `br_on_non_null`, [0]), [value("i32", 0)]);

// ./test/core/gc/br_on_cast_fail.wast:72
assert_return(() => invoke($0, `br_on_non_null`, [1]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:73
assert_return(() => invoke($0, `br_on_non_null`, [2]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:74
assert_return(() => invoke($0, `br_on_non_null`, [3]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:75
assert_return(() => invoke($0, `br_on_non_null`, [4]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:77
assert_return(() => invoke($0, `br_on_non_i31`, [0]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:78
assert_return(() => invoke($0, `br_on_non_i31`, [1]), [value("i32", 7)]);

// ./test/core/gc/br_on_cast_fail.wast:79
assert_return(() => invoke($0, `br_on_non_i31`, [2]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:80
assert_return(() => invoke($0, `br_on_non_i31`, [3]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:81
assert_return(() => invoke($0, `br_on_non_i31`, [4]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:83
assert_return(() => invoke($0, `br_on_non_struct`, [0]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:84
assert_return(() => invoke($0, `br_on_non_struct`, [1]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:85
assert_return(() => invoke($0, `br_on_non_struct`, [2]), [value("i32", 6)]);

// ./test/core/gc/br_on_cast_fail.wast:86
assert_return(() => invoke($0, `br_on_non_struct`, [3]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:87
assert_return(() => invoke($0, `br_on_non_struct`, [4]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:89
assert_return(() => invoke($0, `br_on_non_array`, [0]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:90
assert_return(() => invoke($0, `br_on_non_array`, [1]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:91
assert_return(() => invoke($0, `br_on_non_array`, [2]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:92
assert_return(() => invoke($0, `br_on_non_array`, [3]), [value("i32", 3)]);

// ./test/core/gc/br_on_cast_fail.wast:93
assert_return(() => invoke($0, `br_on_non_array`, [4]), [value("i32", -1)]);

// ./test/core/gc/br_on_cast_fail.wast:95
assert_return(() => invoke($0, `null-diff`, [0]), [value("i32", 1)]);

// ./test/core/gc/br_on_cast_fail.wast:96
assert_return(() => invoke($0, `null-diff`, [1]), [value("i32", 0)]);

// ./test/core/gc/br_on_cast_fail.wast:97
assert_return(() => invoke($0, `null-diff`, [2]), [value("i32", 1)]);

// ./test/core/gc/br_on_cast_fail.wast:98
assert_return(() => invoke($0, `null-diff`, [3]), [value("i32", 0)]);

// ./test/core/gc/br_on_cast_fail.wast:99
assert_return(() => invoke($0, `null-diff`, [4]), [value("i32", 0)]);

// ./test/core/gc/br_on_cast_fail.wast:104
let $1 = instantiate(`(module
  (type $$t0 (sub (struct)))
  (type $$t1 (sub $$t0 (struct (field i32))))
  (type $$t1' (sub $$t0 (struct (field i32))))
  (type $$t2 (sub $$t1 (struct (field i32 i32))))
  (type $$t2' (sub $$t1' (struct (field i32 i32))))
  (type $$t3 (sub $$t0 (struct (field i32 i32))))
  (type $$t0' (sub $$t0 (struct)))
  (type $$t4 (sub $$t0' (struct (field i32 i32))))

  (table 20 structref)

  (func $$init
    (table.set (i32.const 0) (struct.new_default $$t0))
    (table.set (i32.const 10) (struct.new_default $$t0))
    (table.set (i32.const 1) (struct.new_default $$t1))
    (table.set (i32.const 11) (struct.new_default $$t1'))
    (table.set (i32.const 2) (struct.new_default $$t2))
    (table.set (i32.const 12) (struct.new_default $$t2'))
    (table.set (i32.const 3) (struct.new_default $$t3 ))
    (table.set (i32.const 4) (struct.new_default $$t4))
  )

  (func (export "test-sub")
    (call $$init)
    (block $$l (result structref)
      ;; must not succeed
      (br_on_cast_fail $$l structref (ref null $$t0) (ref.null struct))
      (br_on_cast_fail $$l structref (ref null $$t0) (table.get (i32.const 0)))
      (br_on_cast_fail $$l structref (ref null $$t0) (table.get (i32.const 1)))
      (br_on_cast_fail $$l structref (ref null $$t0) (table.get (i32.const 2)))
      (br_on_cast_fail $$l structref (ref null $$t0) (table.get (i32.const 3)))
      (br_on_cast_fail $$l structref (ref null $$t0) (table.get (i32.const 4)))
      (br_on_cast_fail $$l structref (ref $$t0) (table.get (i32.const 0)))
      (br_on_cast_fail $$l structref (ref $$t0) (table.get (i32.const 1)))
      (br_on_cast_fail $$l structref (ref $$t0) (table.get (i32.const 2)))
      (br_on_cast_fail $$l structref (ref $$t0) (table.get (i32.const 3)))
      (br_on_cast_fail $$l structref (ref $$t0) (table.get (i32.const 4)))

      (br_on_cast_fail $$l structref (ref null $$t1) (ref.null struct))
      (br_on_cast_fail $$l structref (ref null $$t1) (table.get (i32.const 1)))
      (br_on_cast_fail $$l structref (ref null $$t1) (table.get (i32.const 2)))
      (br_on_cast_fail $$l structref (ref $$t1) (table.get (i32.const 1)))
      (br_on_cast_fail $$l structref (ref $$t1) (table.get (i32.const 2)))

      (br_on_cast_fail $$l structref (ref null $$t2) (ref.null struct))
      (br_on_cast_fail $$l structref (ref null $$t2) (table.get (i32.const 2)))
      (br_on_cast_fail $$l structref (ref $$t2) (table.get (i32.const 2)))

      (br_on_cast_fail $$l structref (ref null $$t3) (ref.null struct))
      (br_on_cast_fail $$l structref (ref null $$t3) (table.get (i32.const 3)))
      (br_on_cast_fail $$l structref (ref $$t3) (table.get (i32.const 3)))

      (br_on_cast_fail $$l structref (ref null $$t4) (ref.null struct))
      (br_on_cast_fail $$l structref (ref null $$t4) (table.get (i32.const 4)))
      (br_on_cast_fail $$l structref (ref $$t4) (table.get (i32.const 4)))

      ;; must succeed
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t0) (ref.null struct))))

      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t1) (ref.null struct))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t1) (table.get (i32.const 0)))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t1) (table.get (i32.const 3)))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t1) (table.get (i32.const 4)))))

      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t2) (ref.null struct))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t2) (table.get (i32.const 0)))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t2) (table.get (i32.const 1)))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t2) (table.get (i32.const 3)))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t2) (table.get (i32.const 4)))))

      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t3) (ref.null struct))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t3) (table.get (i32.const 0)))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t3) (table.get (i32.const 1)))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t3) (table.get (i32.const 2)))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t3) (table.get (i32.const 4)))))

      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t4) (ref.null struct))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t4) (table.get (i32.const 0)))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t4) (table.get (i32.const 1)))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t4) (table.get (i32.const 2)))))
      (drop (block (result structref) (br_on_cast_fail 0 structref (ref $$t4) (table.get (i32.const 3)))))

      (return)
    )
    (unreachable)
  )

  (func (export "test-canon")
    (call $$init)
    (block $$l (result structref)
      (br_on_cast_fail $$l structref (ref $$t0) (table.get (i32.const 0)))
      (br_on_cast_fail $$l structref (ref $$t0) (table.get (i32.const 1)))
      (br_on_cast_fail $$l structref (ref $$t0) (table.get (i32.const 2)))
      (br_on_cast_fail $$l structref (ref $$t0) (table.get (i32.const 3)))
      (br_on_cast_fail $$l structref (ref $$t0) (table.get (i32.const 4)))
      (br_on_cast_fail $$l structref (ref $$t0) (table.get (i32.const 10)))
      (br_on_cast_fail $$l structref (ref $$t0) (table.get (i32.const 11)))
      (br_on_cast_fail $$l structref (ref $$t0) (table.get (i32.const 12)))

      (br_on_cast_fail $$l structref (ref $$t1') (table.get (i32.const 1)))
      (br_on_cast_fail $$l structref (ref $$t1') (table.get (i32.const 2)))

      (br_on_cast_fail $$l structref (ref $$t1) (table.get (i32.const 11)))
      (br_on_cast_fail $$l structref (ref $$t1) (table.get (i32.const 12)))

      (br_on_cast_fail $$l structref (ref $$t2') (table.get (i32.const 2)))

      (br_on_cast_fail $$l structref (ref $$t2) (table.get (i32.const 12)))

      (return)
    )
    (unreachable)
  )
)`);

// ./test/core/gc/br_on_cast_fail.wast:220
invoke($1, `test-sub`, []);

// ./test/core/gc/br_on_cast_fail.wast:221
invoke($1, `test-canon`, []);

// ./test/core/gc/br_on_cast_fail.wast:226
let $2 = instantiate(`(module
  (type $$t (struct))

  (func (param (ref any)) (result (ref any))
    (block (result (ref $$t)) (br_on_cast_fail 1 (ref any) (ref $$t) (local.get 0)))
  )
  (func (param (ref null any)) (result (ref null any))
    (block (result (ref $$t)) (br_on_cast_fail 1 (ref null any) (ref $$t) (local.get 0)))
  )
  (func (param (ref null any)) (result (ref null any))
    (block (result (ref null $$t)) (br_on_cast_fail 1 (ref null any) (ref null $$t) (local.get 0)))
  )
)`);

// ./test/core/gc/br_on_cast_fail.wast:240
assert_invalid(
  () => instantiate(`(module
    (type $$t (struct))
    (func (param (ref any)) (result (ref any))
      (block (result (ref $$t)) (br_on_cast_fail 1 (ref null any) (ref null $$t) (local.get 0)))
    )
  )`),
  `type mismatch`,
);

// ./test/core/gc/br_on_cast_fail.wast:249
assert_invalid(
  () => instantiate(`(module
    (type $$t (struct))
    (func (param (ref any)) (result (ref any))
      (block (result (ref null $$t)) (br_on_cast_fail 1 (ref any) (ref null $$t) (local.get 0))) (ref.as_non_null)
    )
  )`),
  `type mismatch`,
);

// ./test/core/gc/br_on_cast_fail.wast:258
assert_invalid(
  () => instantiate(`(module
    (type $$t (struct))
    (func (param (ref null any)) (result (ref any))
      (block (result (ref $$t)) (br_on_cast_fail 1 (ref null any) (ref $$t) (local.get 0)))
    )
  )`),
  `type mismatch`,
);

// ./test/core/gc/br_on_cast_fail.wast:267
assert_invalid(
  () => instantiate(`(module
    (func (result anyref)
      (br_on_cast_fail 0 eqref anyref (unreachable))
    )
  )`),
  `type mismatch`,
);

// ./test/core/gc/br_on_cast_fail.wast:275
assert_invalid(
  () => instantiate(`(module
    (func (result anyref)
      (br_on_cast_fail 0 structref arrayref (unreachable))
    )
  )`),
  `type mismatch`,
);