Debian Codegraph @

js/src/jit-test/tests/wasm/exceptions/js-api.js

raw
1 // Tests for wasm exception proposal JS API features.
2
3 load(libdir + "eqArrayHelper.js");
4
5 // WebAssembly.Tag tests.
6 assertErrorMessage(
7 () => WebAssembly.Tag(),
8 TypeError,
9 /calling a builtin Tag constructor without new is forbidden/
10 );
11
12 assertErrorMessage(
13 () => new WebAssembly.Tag(),
14 TypeError,
15 /At least 1 argument required/
16 );
17
18 assertErrorMessage(
19 () => new WebAssembly.Tag(3),
20 TypeError,
21 /first argument must be a tag descriptor/
22 );
23
24 assertErrorMessage(
25 () => new WebAssembly.Tag({ parameters: ["foobar"] }),
26 TypeError,
27 /bad value type/
28 );
29
30 new WebAssembly.Tag({ parameters: [] });
31 new WebAssembly.Tag({ parameters: ["i32"] });
32 new WebAssembly.Tag({ parameters: ["i32", "externref"] });
33
34 wasmEvalText(`(module (import "m" "e" (tag)))`, {
35 m: { e: new WebAssembly.Tag({ parameters: [] }) },
36 });
37
38 wasmEvalText(`(module (import "m" "e" (tag (param i32))))`, {
39 m: { e: new WebAssembly.Tag({ parameters: ["i32"] }) },
40 });
41
42 wasmEvalText(`(module (import "m" "e" (tag (param i32 i64))))`, {
43 m: { e: new WebAssembly.Tag({ parameters: ["i32", "i64"] }) },
44 });
45
46 assertErrorMessage(
47 () =>
48 wasmEvalText(`(module (import "m" "e" (tag (param i32))))`, {
49 m: { e: new WebAssembly.Tag({ parameters: [] }) },
50 }),
51 WebAssembly.LinkError,
52 /imported tag 'm.e' signature mismatch/
53 );
54
55 assertErrorMessage(
56 () =>
57 wasmEvalText(`(module (import "m" "e" (tag (param))))`, {
58 m: { e: new WebAssembly.Tag({ parameters: ["i32"] }) },
59 }),
60 WebAssembly.LinkError,
61 /imported tag 'm.e' signature mismatch/
62 );
63
64 // Test WebAssembly.Tag methods.
65 // TODO: add runtime detection for js-types
66 // {
67 // let params = [
68 // [],
69 // ["i32"],
70 // ["i32", "i64"],
71 // ["f32", "externref"],
72 // ["i32", "i64", "f32", "f64"],
73 // ];
74
75 // for (const arg of params) {
76 // const tag = new WebAssembly.Tag({ parameters: arg });
77 // assertEqArray(tag.type().parameters, arg);
78 // }
79 // }
80
81 // WebAssembly.Exception tests.
82 assertErrorMessage(
83 () => WebAssembly.Exception(),
84 TypeError,
85 /calling a builtin Exception constructor without new is forbidden/
86 );
87
88 assertErrorMessage(
89 () => new WebAssembly.Exception(),
90 TypeError,
91 /At least 2 arguments required/
92 );
93
94 assertErrorMessage(
95 () => new WebAssembly.Exception(3, []),
96 TypeError,
97 /first argument must be a WebAssembly.Tag/
98 );
99
100 const { tag1, tag2, tag3, tag4, tag5, tag6, tag7, tag8, tag9 } = wasmEvalText(
101 `(module
102 (tag (export "tag1") (param))
103 (tag (export "tag2") (param i32))
104 (tag (export "tag3") (param i32 f32))
105 (tag (export "tag4") (param i32 externref i32))
106 (tag (export "tag5") (param i32 externref i32 externref))
107 (tag (export "tag6") (param funcref))
108 (tag (export "tag7") (param i64))
109 (tag (export "tag8") (param i32 f64))
110 (tag (export "tag9") (param externref funcref)))`
111 ).exports;
112
113 new WebAssembly.Exception(tag1, []);
114 new WebAssembly.Exception(tag2, [3]);
115 new WebAssembly.Exception(tag3, [3, 5.5]);
116 new WebAssembly.Exception(tag4, [3, "foo", 4]);
117 new WebAssembly.Exception(tag5, [3, "foo", 4, "bar"]);
118
119 assertErrorMessage(
120 () => new WebAssembly.Exception(tag2, []),
121 TypeError,
122 /expected 1 values but got 0/
123 );
124
125 assertErrorMessage(
126 () => new WebAssembly.Exception(tag2, [3n]),
127 TypeError,
128 /can't convert BigInt to number/
129 );
130
131 assertErrorMessage(
132 () => new WebAssembly.Exception(tag6, [undefined]),
133 TypeError,
134 /can only pass WebAssembly exported functions to funcref/
135 );
136
137 assertErrorMessage(
138 () => new WebAssembly.Exception(tag7, [undefined]),
139 TypeError,
140 /can't convert undefined to BigInt/
141 );
142
143 assertErrorMessage(
144 () => new WebAssembly.Exception(tag7, {}),
145 TypeError,
146 /\({}\) is not iterable/
147 );
148
149 assertErrorMessage(
150 () => new WebAssembly.Exception(tag7, 1),
151 TypeError,
152 /second argument must be an object/
153 );
154
155 // Test Exception methods.
156 {
157 const exn1 = new WebAssembly.Exception(tag1, []);
158 assertEq(exn1.is(tag1), true);
159 assertEq(exn1.is(tag2), false);
160 assertErrorMessage(
161 () => exn1.is(),
162 TypeError,
163 /At least 1 argument required/
164 );
165 assertErrorMessage(
166 () => exn1.is(5),
167 TypeError,
168 /first argument must be a WebAssembly.Tag/
169 );
170
171 const exn2 = new WebAssembly.Exception(tag2, [3]);
172 assertEq(exn2.getArg(tag2, 0), 3);
173
174 assertEq(new WebAssembly.Exception(tag2, [undefined]).getArg(tag2, 0), 0);
175
176 const exn4 = new WebAssembly.Exception(tag4, [3, "foo", 4]);
177 assertEq(exn4.getArg(tag4, 0), 3);
178 assertEq(exn4.getArg(tag4, 1), "foo");
179 assertEq(exn4.getArg(tag4, 2), 4);
180
181 const exn5 = new WebAssembly.Exception(tag5, [3, "foo", 4, "bar"]);
182 assertEq(exn5.getArg(tag5, 3), "bar");
183
184 const { funcref } = wasmEvalText(
185 `(module (func (export "funcref")))`
186 ).exports;
187 const exn9 = new WebAssembly.Exception(tag9, ["foo", funcref]);
188 assertEq(exn9.getArg(tag9, 0), "foo");
189 assertEq(exn9.getArg(tag9, 1), funcref);
190
191 assertErrorMessage(
192 () => exn2.getArg(),
193 TypeError,
194 /At least 2 arguments required/
195 );
196 assertErrorMessage(
197 () => exn2.getArg(5, 0),
198 TypeError,
199 /first argument must be a WebAssembly.Tag/
200 );
201 assertErrorMessage(
202 () => exn2.getArg(tag2, "foo"),
203 TypeError,
204 /bad Exception getArg index/
205 );
206 assertErrorMessage(
207 () => exn2.getArg(tag2, 10),
208 RangeError,
209 /bad Exception getArg index/
210 );
211 }
212
213 // Test throwing a JS constructed exception to Wasm.
214 assertEq(
215 wasmEvalText(
216 `(module
217 (import "m" "exn" (tag $exn (param i32)))
218 (import "m" "f" (func $f))
219 (func (export "f") (result i32)
220 try (result i32)
221 call $f
222 (i32.const 0)
223 catch $exn
224 end))`,
225 {
226 m: {
227 exn: tag2,
228 f: () => {
229 throw new WebAssembly.Exception(tag2, [42]);
230 },
231 },
232 }
233 ).exports.f(),
234 42
235 );
236
237 assertEqArray(
238 wasmEvalText(
239 `(module
240 (import "m" "exn" (tag $exn (param i32 f32)))
241 (import "m" "f" (func $f))
242 (func (export "f") (result i32 f32)
243 try (result i32 f32)
244 call $f
245 (i32.const 0)
246 (f32.const 0)
247 catch $exn
248 end))`,
249 {
250 m: {
251 exn: tag3,
252 f: () => {
253 throw new WebAssembly.Exception(tag3, [42, 5.5]);
254 },
255 },
256 }
257 ).exports.f(),
258 [42, 5.5]
259 );
260
261 assertEqArray(
262 wasmEvalText(
263 `(module
264 (import "m" "exn" (tag $exn (param i32 f64)))
265 (import "m" "f" (func $f))
266 (func (export "f") (result i32 f64)
267 try (result i32 f64)
268 call $f
269 (i32.const 0)
270 (f64.const 0)
271 catch $exn
272 end))`,
273 {
274 m: {
275 exn: tag8,
276 f: () => {
277 throw new WebAssembly.Exception(tag8, [9999, 9999]);
278 },
279 },
280 }
281 ).exports.f(),
282 [9999, 9999]
283 );
284
285 assertEqArray(
286 wasmEvalText(
287 `(module
288 (import "m" "exn" (tag $exn (param i32 externref i32)))
289 (import "m" "f" (func $f))
290 (func (export "f") (result i32 externref i32)
291 try (result i32 externref i32)
292 call $f
293 (i32.const 0)
294 (ref.null extern)
295 (i32.const 0)
296 catch $exn
297 end))`,
298 {
299 m: {
300 exn: tag4,
301 f: () => {
302 throw new WebAssembly.Exception(tag4, [42, "foo", 42]);
303 },
304 },
305 }
306 ).exports.f(),
307 [42, "foo", 42]
308 );
309
310 assertEqArray(
311 wasmEvalText(
312 `(module
313 (import "m" "exn" (tag $exn (param i32 externref i32 externref)))
314 (import "m" "f" (func $f))
315 (func (export "f") (result i32 externref i32 externref)
316 try (result i32 externref i32 externref)
317 call $f
318 (i32.const 0)
319 (ref.null extern)
320 (i32.const 0)
321 (ref.null extern)
322 catch $exn
323 end))`,
324 {
325 m: {
326 exn: tag5,
327 f: () => {
328 throw new WebAssembly.Exception(tag5, [42, "foo", 42, "bar"]);
329 },
330 },
331 }
332 ).exports.f(),
333 [42, "foo", 42, "bar"]
334 );
335
336 {
337 const { funcref } = wasmEvalText(
338 `(module (func (export "funcref")))`
339 ).exports;
340 assertEqArray(
341 wasmEvalText(
342 `(module
343 (import "m" "exn" (tag $exn (param externref funcref)))
344 (import "m" "f" (func $f))
345 (func (export "f") (result externref funcref)
346 try (result externref funcref)
347 call $f
348 (ref.null extern)
349 (ref.null func)
350 catch $exn
351 end))`,
352 {
353 m: {
354 exn: tag9,
355 f: () => {
356 throw new WebAssembly.Exception(tag9, ["foo", funcref]);
357 },
358 },
359 }
360 ).exports.f(),
361 ["foo", funcref]
362 );
363 }
364
365 assertEq(
366 wasmEvalText(
367 `(module
368 (import "m" "exn" (tag $exn))
369 (import "m" "f" (func $f))
370 (func (export "f") (result i32)
371 try (result i32)
372 call $f
373 (i32.const 0)
374 catch $exn
375 (i32.const 0)
376 catch_all
377 (i32.const 1)
378 end))`,
379 {
380 m: {
381 exn: tag1,
382 f: () => {
383 throw new WebAssembly.Exception(tag2, [42]);
384 },
385 },
386 }
387 ).exports.f(),
388 1
389 );
390
391 {
392 const exn = new WebAssembly.Tag({ parameters: ["i32"] });
393 assertEq(
394 wasmEvalText(
395 `(module
396 (import "m" "exn" (tag $exn (param i32)))
397 (import "m" "f" (func $f))
398 (func (export "f") (result i32)
399 try (result i32)
400 call $f
401 (i32.const 0)
402 catch $exn
403 end))`,
404 {
405 m: {
406 exn,
407 f: () => {
408 throw new WebAssembly.Exception(exn, [42]);
409 },
410 },
411 }
412 ).exports.f(),
413 42
414 );
415 }
416
417 {
418 const exn1 = new WebAssembly.Tag({ parameters: ["i32"] });
419 const exn2 = new WebAssembly.Tag({ parameters: ["i32"] });
420 assertEq(
421 wasmEvalText(
422 `(module
423 (import "m" "exn" (tag $exn (param i32)))
424 (import "m" "f" (func $f))
425 (func (export "f") (result i32)
426 try (result i32)
427 call $f
428 (i32.const 0)
429 catch $exn
430 catch_all
431 (i32.const 1)
432 end))`,
433 {
434 m: {
435 exn: exn1,
436 f: () => {
437 throw new WebAssembly.Exception(exn2, [42]);
438 },
439 },
440 }
441 ).exports.f(),
442 1
443 );
444 }
445
446 // Test `getArg` on a Wasm-thrown exception.
447 assertEq(
448 (() => {
449 try {
450 wasmEvalText(
451 `(module
452 (import "m" "exn" (tag $exn (param i32 f64)))
453 (func (export "f")
454 (i32.const 9999)
455 (f64.const 9999)
456 throw $exn))`,
457 { m: { exn: tag8 } }
458 ).exports.f();
459 } catch (exn) {
460 return exn.getArg(tag8, 1);
461 }
462 })(),
463 9999
464 );
465
466 assertEqArray(
467 (() => {
468 try {
469 wasmEvalText(
470 `(module
471 (import "m" "exn" (tag $exn (param i32 externref i32 externref)))
472 (func (export "f") (param externref externref)
473 (i32.const 1)
474 (local.get 0)
475 (i32.const 2)
476 (local.get 1)
477 throw $exn))`,
478 { m: { exn: tag5 } }
479 ).exports.f("foo", "bar");
480 } catch (exn) {
481 return [
482 exn.getArg(tag5, 0),
483 exn.getArg(tag5, 1),
484 exn.getArg(tag5, 2),
485 exn.getArg(tag5, 3),
486 ];
487 }
488 })(),
489 [1, "foo", 2, "bar"]
490 );