summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/spec/exception-handling/try_catch.wast.js
blob: ab67fc936ce10828ef371e2f6509ccf7d5f5c85d (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
/* 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/try_catch.wast

// ./test/core/try_catch.wast:3
let $0 = instantiate(`(module
  (tag $$e0 (export "e0"))
  (func (export "throw") (throw $$e0))
)`);

// ./test/core/try_catch.wast:8
register($0, `test`);

// ./test/core/try_catch.wast:10
let $1 = instantiate(`(module
  (tag $$imported-e0 (import "test" "e0"))
  (func $$imported-throw (import "test" "throw"))
  (tag $$e0)
  (tag $$e1)
  (tag $$e2)
  (tag $$e-i32 (param i32))
  (tag $$e-f32 (param f32))
  (tag $$e-i64 (param i64))
  (tag $$e-f64 (param f64))

  (func $$throw-if (param i32) (result i32)
    (local.get 0)
    (i32.const 0) (if (i32.ne) (then (throw $$e0)))
    (i32.const 0)
  )

  (func (export "empty-catch") (try (do) (catch $$e0)))

  (func (export "simple-throw-catch") (param i32) (result i32)
    (try (result i32)
      (do (local.get 0) (i32.eqz) (if (then (throw $$e0)) (else)) (i32.const 42))
      (catch $$e0 (i32.const 23))
    )
  )

  (func (export "unreachable-not-caught") (try (do (unreachable)) (catch_all)))

  (func $$div (param i32 i32) (result i32)
    (local.get 0) (local.get 1) (i32.div_u)
  )
  (func (export "trap-in-callee") (param i32 i32) (result i32)
    (try (result i32)
      (do (local.get 0) (local.get 1) (call $$div))
      (catch_all (i32.const 11))
    )
  )

  (func (export "catch-complex-1") (param i32) (result i32)
    (try (result i32)
      (do
        (try (result i32)
          (do
            (local.get 0)
            (i32.eqz)
            (if
              (then (throw $$e0))
              (else
                (local.get 0)
                (i32.const 1)
                (i32.eq)
                (if (then (throw $$e1)) (else (throw $$e2)))
              )
            )
            (i32.const 2)
          )
          (catch $$e0 (i32.const 3))
        )
      )
      (catch $$e1 (i32.const 4))
    )
  )

  (func (export "catch-complex-2") (param i32) (result i32)
    (try (result i32)
      (do
        (local.get 0)
        (i32.eqz)
        (if
          (then (throw $$e0))
          (else
            (local.get 0)
            (i32.const 1)
            (i32.eq)
            (if (then (throw $$e1)) (else (throw $$e2)))
          )
        )
        (i32.const 2)
      )
      (catch $$e0 (i32.const 3))
      (catch $$e1 (i32.const 4))
    )
  )

  (func (export "throw-catch-param-i32") (param i32) (result i32)
    (try (result i32)
      (do (local.get 0) (throw $$e-i32) (i32.const 2))
      (catch $$e-i32 (return))
    )
  )

  (func (export "throw-catch-param-f32") (param f32) (result f32)
    (try (result f32)
      (do (local.get 0) (throw $$e-f32) (f32.const 0))
      (catch $$e-f32 (return))
    )
  )

  (func (export "throw-catch-param-i64") (param i64) (result i64)
    (try (result i64)
      (do (local.get 0) (throw $$e-i64) (i64.const 2))
      (catch $$e-i64 (return))
    )
  )

  (func (export "throw-catch-param-f64") (param f64) (result f64)
    (try (result f64)
      (do (local.get 0) (throw $$e-f64) (f64.const 0))
      (catch $$e-f64 (return))
    )
  )

  (func $$throw-param-i32 (param i32) (local.get 0) (throw $$e-i32))
  (func (export "catch-param-i32") (param i32) (result i32)
    (try (result i32)
      (do (i32.const 0) (local.get 0) (call $$throw-param-i32))
      (catch $$e-i32)
    )
  )

  (func (export "catch-imported") (result i32)
    (try (result i32)
      (do
        (i32.const 1)
        (call $$imported-throw)
      )
      (catch $$imported-e0 (i32.const 2))
    )
  )

  (func (export "catchless-try") (param i32) (result i32)
    (try (result i32)
      (do
        (try (result i32)
          (do (local.get 0) (call $$throw-if))
        )
      )
      (catch $$e0 (i32.const 1))
    )
  )
)`);

// ./test/core/try_catch.wast:152
assert_return(() => invoke($1, `empty-catch`, []), []);

// ./test/core/try_catch.wast:154
assert_return(() => invoke($1, `simple-throw-catch`, [0]), [value("i32", 23)]);

// ./test/core/try_catch.wast:155
assert_return(() => invoke($1, `simple-throw-catch`, [1]), [value("i32", 42)]);

// ./test/core/try_catch.wast:157
assert_trap(() => invoke($1, `unreachable-not-caught`, []), `unreachable`);

// ./test/core/try_catch.wast:159
assert_return(() => invoke($1, `trap-in-callee`, [7, 2]), [value("i32", 3)]);

// ./test/core/try_catch.wast:160
assert_trap(() => invoke($1, `trap-in-callee`, [1, 0]), `integer divide by zero`);

// ./test/core/try_catch.wast:162
assert_return(() => invoke($1, `catch-complex-1`, [0]), [value("i32", 3)]);

// ./test/core/try_catch.wast:163
assert_return(() => invoke($1, `catch-complex-1`, [1]), [value("i32", 4)]);

// ./test/core/try_catch.wast:164
assert_exception(() => invoke($1, `catch-complex-1`, [2]));

// ./test/core/try_catch.wast:166
assert_return(() => invoke($1, `catch-complex-2`, [0]), [value("i32", 3)]);

// ./test/core/try_catch.wast:167
assert_return(() => invoke($1, `catch-complex-2`, [1]), [value("i32", 4)]);

// ./test/core/try_catch.wast:168
assert_exception(() => invoke($1, `catch-complex-2`, [2]));

// ./test/core/try_catch.wast:170
assert_return(() => invoke($1, `throw-catch-param-i32`, [0]), [value("i32", 0)]);

// ./test/core/try_catch.wast:171
assert_return(() => invoke($1, `throw-catch-param-i32`, [1]), [value("i32", 1)]);

// ./test/core/try_catch.wast:172
assert_return(() => invoke($1, `throw-catch-param-i32`, [10]), [value("i32", 10)]);

// ./test/core/try_catch.wast:174
assert_return(() => invoke($1, `throw-catch-param-f32`, [value("f32", 5)]), [value("f32", 5)]);

// ./test/core/try_catch.wast:175
assert_return(() => invoke($1, `throw-catch-param-f32`, [value("f32", 10.5)]), [value("f32", 10.5)]);

// ./test/core/try_catch.wast:177
assert_return(() => invoke($1, `throw-catch-param-i64`, [5n]), [value("i64", 5n)]);

// ./test/core/try_catch.wast:178
assert_return(() => invoke($1, `throw-catch-param-i64`, [0n]), [value("i64", 0n)]);

// ./test/core/try_catch.wast:179
assert_return(() => invoke($1, `throw-catch-param-i64`, [-1n]), [value("i64", -1n)]);

// ./test/core/try_catch.wast:181
assert_return(() => invoke($1, `throw-catch-param-f64`, [value("f64", 5)]), [value("f64", 5)]);

// ./test/core/try_catch.wast:182
assert_return(() => invoke($1, `throw-catch-param-f64`, [value("f64", 10.5)]), [value("f64", 10.5)]);

// ./test/core/try_catch.wast:184
assert_return(() => invoke($1, `catch-param-i32`, [5]), [value("i32", 5)]);

// ./test/core/try_catch.wast:186
assert_return(() => invoke($1, `catch-imported`, []), [value("i32", 2)]);

// ./test/core/try_catch.wast:188
assert_return(() => invoke($1, `catchless-try`, [0]), [value("i32", 0)]);

// ./test/core/try_catch.wast:189
assert_return(() => invoke($1, `catchless-try`, [1]), [value("i32", 1)]);

// ./test/core/try_catch.wast:191
let $2 = instantiate(`(module
  (func $$imported-throw (import "test" "throw"))
  (tag $$e0)

  (func (export "imported-mismatch") (result i32)
    (try (result i32)
      (do
        (try (result i32)
          (do
            (i32.const 1)
            (call $$imported-throw)
          )
          (catch $$e0 (i32.const 2))
        )
      )
      (catch_all (i32.const 3))
    )
  )
)`);

// ./test/core/try_catch.wast:211
assert_return(() => invoke($2, `imported-mismatch`, []), [value("i32", 3)]);

// ./test/core/try_catch.wast:213
assert_malformed(() => instantiate(`(module (func (catch_all))) `), `unexpected token`);

// ./test/core/try_catch.wast:218
assert_malformed(
  () => instantiate(`(module (tag $$e) (func (catch $$e))) `),
  `unexpected token`,
);

// ./test/core/try_catch.wast:223
assert_malformed(
  () => instantiate(`(module (func (try (do) (catch_all) (catch_all)))) `),
  `unexpected token`,
);

// ./test/core/try_catch.wast:230
assert_invalid(
  () => instantiate(`(module (func (result i32) (try (result i32) (do))))`),
  `type mismatch: instruction requires [i32] but stack has []`,
);

// ./test/core/try_catch.wast:232
assert_invalid(
  () => instantiate(`(module (func (result i32) (try (result i32) (do (i64.const 42)))))`),
  `type mismatch: instruction requires [i32] but stack has [i64]`,
);

// ./test/core/try_catch.wast:234
assert_invalid(
  () => instantiate(`(module (tag) (func (try (do) (catch 0 (i32.const 42)))))`),
  `type mismatch: block requires [] but stack has [i32]`,
);

// ./test/core/try_catch.wast:236
assert_invalid(
  () => instantiate(`(module
                  (tag (param i64))
                  (func (result i32)
                    (try (result i32) (do (i32.const 42)) (catch 0))))`),
  `type mismatch: instruction requires [i32] but stack has [i64]`,
);

// ./test/core/try_catch.wast:241
assert_invalid(
  () => instantiate(`(module (func (try (do) (catch_all (i32.const 42)))))`),
  `type mismatch: block requires [] but stack has [i32]`,
);