Debian Codegraph @

tests/cli/function-references/call_ref/call_ref.wast

raw
1 ;; RUN: wast --assert default --snapshot tests/snapshots %
2
3 (module
4 (type $ii (func (param i32) (result i32)))
5
6 (func $apply (param $f (ref $ii)) (param $x i32) (result i32)
7 (call_ref $ii (local.get $x) (local.get $f))
8 )
9
10 (func $f (type $ii) (i32.mul (local.get 0) (local.get 0)))
11 (func $g (type $ii) (i32.sub (i32.const 0) (local.get 0)))
12
13 (elem declare func $f $g)
14
15 (func (export "run") (param $x i32) (result i32)
16 (local $rf (ref null $ii))
17 (local $rg (ref null $ii))
18 (local.set $rf (ref.func $f))
19 (local.set $rg (ref.func $g))
20 (call_ref $ii (call_ref $ii (local.get $x) (local.get $rf)) (local.get $rg))
21 )
22
23 (func (export "null") (result i32)
24 (call_ref $ii (i32.const 1) (ref.null $ii))
25 )
26
27 ;; Recursion
28
29 (type $ll (func (param i64) (result i64)))
30 (type $lll (func (param i64 i64) (result i64)))
31
32 (elem declare func $fac)
33 (global $fac (ref $ll) (ref.func $fac))
34
35 (func $fac (export "fac") (type $ll)
36 (if (result i64) (i64.eqz (local.get 0))
37 (then (i64.const 1))
38 (else
39 (i64.mul
40 (local.get 0)
41 (call_ref $ll (i64.sub (local.get 0) (i64.const 1)) (global.get $fac))
42 )
43 )
44 )
45 )
46
47 (elem declare func $fac-acc)
48 (global $fac-acc (ref $lll) (ref.func $fac-acc))
49
50 (func $fac-acc (export "fac-acc") (type $lll)
51 (if (result i64) (i64.eqz (local.get 0))
52 (then (local.get 1))
53 (else
54 (call_ref $lll
55 (i64.sub (local.get 0) (i64.const 1))
56 (i64.mul (local.get 0) (local.get 1))
57 (global.get $fac-acc)
58 )
59 )
60 )
61 )
62
63 (elem declare func $fib)
64 (global $fib (ref $ll) (ref.func $fib))
65
66 (func $fib (export "fib") (type $ll)
67 (if (result i64) (i64.le_u (local.get 0) (i64.const 1))
68 (then (i64.const 1))
69 (else
70 (i64.add
71 (call_ref $ll (i64.sub (local.get 0) (i64.const 2)) (global.get $fib))
72 (call_ref $ll (i64.sub (local.get 0) (i64.const 1)) (global.get $fib))
73 )
74 )
75 )
76 )
77
78 (elem declare func $even $odd)
79 (global $even (ref $ll) (ref.func $even))
80 (global $odd (ref $ll) (ref.func $odd))
81
82 (func $even (export "even") (type $ll)
83 (if (result i64) (i64.eqz (local.get 0))
84 (then (i64.const 44))
85 (else (call_ref $ll (i64.sub (local.get 0) (i64.const 1)) (global.get $odd)))
86 )
87 )
88 (func $odd (export "odd") (type $ll)
89 (if (result i64) (i64.eqz (local.get 0))
90 (then (i64.const 99))
91 (else (call_ref $ll (i64.sub (local.get 0) (i64.const 1)) (global.get $even)))
92 )
93 )
94 )
95
96 (assert_return (invoke "run" (i32.const 0)) (i32.const 0))
97 (assert_return (invoke "run" (i32.const 3)) (i32.const -9))
98
99 (assert_trap (invoke "null") "null function")
100
101 (assert_return (invoke "fac" (i64.const 0)) (i64.const 1))
102 (assert_return (invoke "fac" (i64.const 1)) (i64.const 1))
103 (assert_return (invoke "fac" (i64.const 5)) (i64.const 120))
104 (assert_return (invoke "fac" (i64.const 25)) (i64.const 7034535277573963776))
105 (assert_return (invoke "fac-acc" (i64.const 0) (i64.const 1)) (i64.const 1))
106 (assert_return (invoke "fac-acc" (i64.const 1) (i64.const 1)) (i64.const 1))
107 (assert_return (invoke "fac-acc" (i64.const 5) (i64.const 1)) (i64.const 120))
108 (assert_return
109 (invoke "fac-acc" (i64.const 25) (i64.const 1))
110 (i64.const 7034535277573963776)
111 )
112
113 (assert_return (invoke "fib" (i64.const 0)) (i64.const 1))
114 (assert_return (invoke "fib" (i64.const 1)) (i64.const 1))
115 (assert_return (invoke "fib" (i64.const 2)) (i64.const 2))
116 (assert_return (invoke "fib" (i64.const 5)) (i64.const 8))
117 (assert_return (invoke "fib" (i64.const 20)) (i64.const 10946))
118
119 (assert_return (invoke "even" (i64.const 0)) (i64.const 44))
120 (assert_return (invoke "even" (i64.const 1)) (i64.const 99))
121 (assert_return (invoke "even" (i64.const 100)) (i64.const 44))
122 (assert_return (invoke "even" (i64.const 77)) (i64.const 99))
123 (assert_return (invoke "odd" (i64.const 0)) (i64.const 99))
124 (assert_return (invoke "odd" (i64.const 1)) (i64.const 44))
125 (assert_return (invoke "odd" (i64.const 200)) (i64.const 99))
126 (assert_return (invoke "odd" (i64.const 77)) (i64.const 44))
127
128
129 ;; Unreachable typing.
130
131 (module
132 (type $t (func))
133 (func (export "unreachable") (result i32)
134 (unreachable)
135 (call_ref $t)
136 )
137 )
138 (assert_trap (invoke "unreachable") "unreachable")
139
140 (module
141 (elem declare func $f)
142 (type $t (func (param i32) (result i32)))
143 (func $f (param i32) (result i32) (local.get 0))
144
145 (func (export "unreachable") (result i32)
146 (unreachable)
147 (ref.func $f)
148 (call_ref $t)
149 )
150 )
151 (assert_trap (invoke "unreachable") "unreachable")
152
153 (module
154 (elem declare func $f)
155 (type $t (func (param i32) (result i32)))
156 (func $f (param i32) (result i32) (local.get 0))
157
158 (func (export "unreachable") (result i32)
159 (unreachable)
160 (i32.const 0)
161 (ref.func $f)
162 (call_ref $t)
163 (drop)
164 (i32.const 0)
165 )
166 )
167 (assert_trap (invoke "unreachable") "unreachable")
168
169 (assert_invalid
170 (module
171 (elem declare func $f)
172 (type $t (func (param i32) (result i32)))
173 (func $f (param i32) (result i32) (local.get 0))
174
175 (func (export "unreachable") (result i32)
176 (unreachable)
177 (i64.const 0)
178 (ref.func $f)
179 (call_ref $t)
180 )
181 )
182 "type mismatch"
183 )
184
185 (assert_invalid
186 (module
187 (elem declare func $f)
188 (type $t (func (param i32) (result i32)))
189 (func $f (param i32) (result i32) (local.get 0))
190
191 (func (export "unreachable") (result i32)
192 (unreachable)
193 (ref.func $f)
194 (call_ref $t)
195 (drop)
196 (i64.const 0)
197 )
198 )
199 "type mismatch"
200 )
201
202 (assert_invalid
203 (module
204 (type $t (func))
205 (func $f (param $r externref)
206 (call_ref $t (local.get $r))
207 )
208 )
209 "type mismatch"
210 )