Debian Codegraph @

js/src/jit-test/tests/wasm/multi-value/call-js.js

raw
1
2 wasmFullPass(`
3 (module
4 (import "env" "f" (func $f (result i32 i32 i32)))
5 (func (export "run") (result i32)
6 (call $f)
7 i32.sub
8 i32.sub))`,
9 42,
10 { env: { f: () => [52, 10, 0] } });
11
12 wasmFullPass(`
13 (module
14 (import "env" "f" (func $f (result i64 i64 i64)))
15 (func (export "run") (result i64)
16 (call $f)
17 i64.sub
18 i64.sub))`,
19 42n,
20 { env: { f: () => [52n, 10n, 0n] } });
21
22 wasmFullPass(`
23 (module
24 (import "env" "f" (func $f (result f32 f32 f32)))
25 (func (export "run") (result i32)
26 (call $f)
27 f32.sub
28 f32.sub
29 i32.trunc_f32_s))`,
30 42,
31 { env: { f: () => [52.25, 10.5, 0.25] } });
32
33 wasmFullPass(`
34 (module
35 (import "env" "f" (func $f (result f64 f64 f64)))
36 (func (export "run") (result i32)
37 (call $f)
38 f64.sub
39 f64.sub
40 i32.trunc_f64_s))`,
41 42,
42 { env: { f: () => [52.25, 10.5, 0.25] } });
43
44 // Multiple values are returned as an iterable; it doesn't have to be an array.
45 function expectMultiValuePass(f) {
46 wasmFullPass(`
47 (module
48 (import "env" "f" (func $f (result i32 i32 i32)))
49 (func (export "run") (result i32)
50 (call $f)
51 i32.sub
52 i32.sub))`,
53 42,
54 { env: { f } });
55 }
56 function expectMultiValueError(f, type, pattern) {
57 let module = new WebAssembly.Module(wasmTextToBinary(`
58 (module
59 (import "env" "f" (func $f (result i32 i32 i32)))
60 (func (export "run") (result i32)
61 (call $f)
62 i32.sub
63 i32.sub))`));
64
65 let instance = new WebAssembly.Instance(module, { env: { f } } );
66 assertErrorMessage(() => instance.exports.run(), type, pattern);
67 }
68
69 expectMultiValuePass(() => [52, 10, 0]);
70 expectMultiValuePass(() => [32, -10, 0]);
71 expectMultiValuePass(() => [52.75, 10, 0.5]); // Values converted to i32 via ToInt32.
72 expectMultiValuePass(() => [42, undefined, undefined]);
73 expectMultiValuePass(() => (function*() { yield 52; yield 10; yield 0; })());
74 expectMultiValuePass(() => (function*() { yield '52'; yield '10'; yield 0; })());
75
76 // Multi-value result must be iterable.
77 expectMultiValueError(() => 1, TypeError, /iterable/);
78 expectMultiValueError(() => 1n, TypeError, /iterable/);
79
80 // Check that the iterator's values are collected first, and that the
81 // length of the result is correct.
82 expectMultiValueError(() => [1], TypeError, /expected 3, got 1/);
83 expectMultiValueError(() => [1n], TypeError, /expected 3, got 1/);
84 expectMultiValueError(() => [52, 10, 0, 0], TypeError, /expected 3, got 4/);
85 expectMultiValueError(() => (function*() { yield 52; yield 10; yield 0; yield 0; })(),
86 TypeError, /expected 3, got 4/);
87
88 // Check that side effects from conversions are done in order.
89 {
90 let calls = [];
91 function log(x) { calls.push(x); return x; }
92 function logged(x) { return { valueOf: () => log(x) } }
93 expectMultiValuePass(() => [logged(52), logged(10), logged(0)]);
94 assertEq(calls.join(','), '52,10,0');
95 }
96
97 function expectMultiValueResult(text, expected) {
98 let instance = wasmEvalText(text);
99 assertDeepEq(instance.exports.run(), expected);
100 }
101
102 expectMultiValueResult(`
103 (module
104 (func (export "run") (result i32 i32 i32)
105 (i32.const 0)
106 (i32.const 52)
107 (i32.const 10)))`, [0, 52, 10]);
108 expectMultiValueResult(`
109 (module
110 (func (export "run") (result f32 f32 f32)
111 (f32.const 0.5)
112 (f32.const 52.5)
113 (f32.const 10.5)))`, [0.5, 52.5, 10.5]);
114 expectMultiValueResult(`
115 (module
116 (func (export "run") (result f64 f64 f64)
117 (f64.const 0.5)
118 (f64.const 52.5)
119 (f64.const 10.5)))`, [0.5, 52.5, 10.5]);
120
121 expectMultiValueResult(`
122 (module
123 (func (export "run") (result i32 i64 i32)
124 (i32.const 0)
125 (i64.const 52)
126 (i32.const 10)))`, [0, 52n, 10]);
127 expectMultiValueResult(`
128 (module
129 (func (export "run") (result i64 i32 i64)
130 (i64.const 0)
131 (i32.const 52)
132 (i64.const 10)))`, [0n, 52, 10n]);
133 expectMultiValueResult(`
134 (module
135 (func (export "run") (result i64 i64 i64)
136 (i64.const 0)
137 (i64.const 52)
138 (i64.const 10)))`, [0n, 52n, 10n]);