Debian Codegraph @

js/src/jit-test/tests/wasm/js-promise-integration/exception-handling.js

raw
1 // |jit-tests| test-also=--setpref=wasm_exnref=true;
2
3 // Test if we can handle WebAssembly.Exception on suspendable stack,
4 // and in suspending function.
5
6 function wasmException(i) {
7 if (i == 0) {
8 // no promise return
9 throw new WebAssembly.Exception(ins1.exports.tag, [i]);
10 }
11 return Promise.reject(new WebAssembly.Exception(ins1.exports.tag, [i]));
12 }
13
14 var ins1 = wasmEvalText(`
15 (module
16 (import "" "t" (func $t (param i32)))
17 (tag $t1 (param i32))
18
19 (func (export "t0")
20 i32.const 0
21 call $t
22 )
23 (func (export "t1")
24 i32.const 1
25 call $t
26 )
27 (func (export "t2")
28 try
29 i32.const 2
30 call $t
31 catch $t1
32 drop
33 end
34 )
35 (func (export "t3")
36 try
37 i32.const 0
38 call $t
39 catch $t1
40 drop
41 end
42 )
43 (export "tag" (tag $t1))
44 )
45 `, {"": {
46 t: new WebAssembly.Suspending(wasmException),
47 }});
48
49 async function testWasmException() {
50 var test0 = WebAssembly.promising(ins1.exports.t0);
51 var test1 = WebAssembly.promising(ins1.exports.t1);
52 var test2 = WebAssembly.promising(ins1.exports.t2);
53 var test3 = WebAssembly.promising(ins1.exports.t3);
54 try {
55 await test0();
56 assertEq(true, false);
57 } catch (ex) {
58 assertEq(ex instanceof WebAssembly.Exception && ex.is(ins1.exports.tag), true);
59 assertEq(ex.getArg(ins1.exports.tag, 0), 0);
60 }
61 try {
62 await test1();
63 assertEq(true, false);
64 } catch (ex) {
65 assertEq(ex instanceof WebAssembly.Exception && ex.is(ins1.exports.tag), true);
66 assertEq(ex.getArg(ins1.exports.tag, 0), 1);
67 }
68 await test2();
69 await test3();
70 }
71
72 // run test asynchronously
73 var p = testWasmException();
74
75 // Test if we can handle JS exception/rejection on suspendable stack,
76 // and in suspending function.
77
78 function jsException(i) {
79 if (i == 0) {
80 // No promise return.
81 throw new Error("test" + i);
82 }
83 if (i == 42) {
84 // Reject with non-Error object.
85 return Promise.reject(3.14);
86 }
87 return Promise.reject(new Error("test" + i));
88 }
89
90 var ins2 = wasmEvalText(`
91 (module
92 (import "" "t" (func $t (param i32)))
93 (import "" "tag" (tag $t1 (param externref)))
94
95 (func (export "t0")
96 i32.const 0
97 call $t
98 )
99 (func (export "t1")
100 i32.const 1
101 call $t
102 )
103 (func (export "t2") (result externref)
104 try
105 i32.const 2
106 call $t
107 catch $t1
108 return
109 end
110 unreachable
111 )
112 (func (export "t3") (result externref)
113 try
114 i32.const 0
115 call $t
116 catch $t1
117 return
118 end
119 unreachable
120 )
121 (func (export "t42")
122 i32.const 42
123 call $t
124 )
125 )
126 `, {"": {
127 t: new WebAssembly.Suspending(jsException),
128 tag: WebAssembly.JSTag,
129 }});
130
131 assertEq(WebAssembly.JSTag instanceof WebAssembly.Tag, true);
132 async function testJSException() {
133 var test0 = WebAssembly.promising(ins2.exports.t0);
134 var test1 = WebAssembly.promising(ins2.exports.t1);
135 var test2 = WebAssembly.promising(ins2.exports.t2);
136 var test3 = WebAssembly.promising(ins2.exports.t3);
137 var test42 = WebAssembly.promising(ins2.exports.t42);
138 try {
139 await test0();
140 assertEq(true, false);
141 } catch (ex) {
142 assertEq(ex instanceof Error && ex.message == "test0", true);
143 }
144 try {
145 await test1();
146 assertEq(true, false);
147 } catch (ex) {
148 assertEq(ex instanceof Error && ex.message == "test1", true);
149 }
150 var ex2 = await test2();
151 assertEq(ex2 instanceof Error && ex2.message == "test2", true);
152 var ex3 = await test3();
153 assertEq(ex3 instanceof Error && ex3.message == "test0", true);
154 try {
155 await test42();
156 assertEq(true, false);
157 } catch (ex) {
158 assertEq(ex, 3.14);
159 }
160 }
161
162 // run test asynchronously
163 p = p.then(testJSException);