-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathcall_ref.wast
More file actions
218 lines (185 loc) · 5.64 KB
/
call_ref.wast
File metadata and controls
218 lines (185 loc) · 5.64 KB
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
(module
(type $ii (func (param i32) (result i32)))
(func $apply (param $f (ref $ii)) (param $x i32) (result i32)
(call_ref $ii (local.get $x) (local.get $f))
)
(func $f (type $ii) (i32.mul (local.get 0) (local.get 0)))
(func $g (type $ii) (i32.sub (i32.const 0) (local.get 0)))
(elem declare func $f $g)
(func (export "run") (param $x i32) (result i32)
(local $rf (ref null $ii))
(local $rg (ref null $ii))
(local.set $rf (ref.func $f))
(local.set $rg (ref.func $g))
(call_ref $ii (call_ref $ii (local.get $x) (local.get $rf)) (local.get $rg))
)
(func (export "null") (result i32)
(call_ref $ii (i32.const 1) (ref.null $ii))
)
;; Recursion
(type $ll (func (param i64) (result i64)))
(type $lll (func (param i64 i64) (result i64)))
(elem declare func $fac)
(global $fac (ref $ll) (ref.func $fac))
(func $fac (export "fac") (type $ll)
(if (result i64) (i64.eqz (local.get 0))
(then (i64.const 1))
(else
(i64.mul
(local.get 0)
(call_ref $ll (i64.sub (local.get 0) (i64.const 1)) (global.get $fac))
)
)
)
)
(elem declare func $fac-acc)
(global $fac-acc (ref $lll) (ref.func $fac-acc))
(func $fac-acc (export "fac-acc") (type $lll)
(if (result i64) (i64.eqz (local.get 0))
(then (local.get 1))
(else
(call_ref $lll
(i64.sub (local.get 0) (i64.const 1))
(i64.mul (local.get 0) (local.get 1))
(global.get $fac-acc)
)
)
)
)
(elem declare func $fib)
(global $fib (ref $ll) (ref.func $fib))
(func $fib (export "fib") (type $ll)
(if (result i64) (i64.le_u (local.get 0) (i64.const 1))
(then (i64.const 1))
(else
(i64.add
(call_ref $ll (i64.sub (local.get 0) (i64.const 2)) (global.get $fib))
(call_ref $ll (i64.sub (local.get 0) (i64.const 1)) (global.get $fib))
)
)
)
)
(elem declare func $even $odd)
(global $even (ref $ll) (ref.func $even))
(global $odd (ref $ll) (ref.func $odd))
(func $even (export "even") (type $ll)
(if (result i64) (i64.eqz (local.get 0))
(then (i64.const 44))
(else (call_ref $ll (i64.sub (local.get 0) (i64.const 1)) (global.get $odd)))
)
)
(func $odd (export "odd") (type $ll)
(if (result i64) (i64.eqz (local.get 0))
(then (i64.const 99))
(else (call_ref $ll (i64.sub (local.get 0) (i64.const 1)) (global.get $even)))
)
)
)
(assert_return (invoke "run" (i32.const 0)) (i32.const 0))
(assert_return (invoke "run" (i32.const 3)) (i32.const -9))
(assert_trap (invoke "null") "null function reference")
(assert_return (invoke "fac" (i64.const 0)) (i64.const 1))
(assert_return (invoke "fac" (i64.const 1)) (i64.const 1))
(assert_return (invoke "fac" (i64.const 5)) (i64.const 120))
(assert_return (invoke "fac" (i64.const 25)) (i64.const 7034535277573963776))
(assert_return (invoke "fac-acc" (i64.const 0) (i64.const 1)) (i64.const 1))
(assert_return (invoke "fac-acc" (i64.const 1) (i64.const 1)) (i64.const 1))
(assert_return (invoke "fac-acc" (i64.const 5) (i64.const 1)) (i64.const 120))
(assert_return
(invoke "fac-acc" (i64.const 25) (i64.const 1))
(i64.const 7034535277573963776)
)
(assert_return (invoke "fib" (i64.const 0)) (i64.const 1))
(assert_return (invoke "fib" (i64.const 1)) (i64.const 1))
(assert_return (invoke "fib" (i64.const 2)) (i64.const 2))
(assert_return (invoke "fib" (i64.const 5)) (i64.const 8))
(assert_return (invoke "fib" (i64.const 20)) (i64.const 10946))
(assert_return (invoke "even" (i64.const 0)) (i64.const 44))
(assert_return (invoke "even" (i64.const 1)) (i64.const 99))
(assert_return (invoke "even" (i64.const 100)) (i64.const 44))
(assert_return (invoke "even" (i64.const 77)) (i64.const 99))
(assert_return (invoke "odd" (i64.const 0)) (i64.const 99))
(assert_return (invoke "odd" (i64.const 1)) (i64.const 44))
(assert_return (invoke "odd" (i64.const 200)) (i64.const 99))
(assert_return (invoke "odd" (i64.const 77)) (i64.const 44))
;; Unreachable typing.
(module
(type $t (func))
(func (export "unreachable") (result i32)
(unreachable)
(call_ref $t)
)
)
(assert_trap (invoke "unreachable") "unreachable")
(module
(elem declare func $f)
(type $t (func (param i32) (result i32)))
(func $f (param i32) (result i32) (local.get 0))
(func (export "unreachable") (result i32)
(unreachable)
(ref.func $f)
(call_ref $t)
)
)
(assert_trap (invoke "unreachable") "unreachable")
(module
(elem declare func $f)
(type $t (func (param i32) (result i32)))
(func $f (param i32) (result i32) (local.get 0))
(func (export "unreachable") (result i32)
(unreachable)
(i32.const 0)
(ref.func $f)
(call_ref $t)
(drop)
(i32.const 0)
)
)
(assert_trap (invoke "unreachable") "unreachable")
(assert_invalid
(module
(elem declare func $f)
(type $t (func (param i32) (result i32)))
(func $f (param i32) (result i32) (local.get 0))
(func (export "unreachable") (result i32)
(unreachable)
(i64.const 0)
(ref.func $f)
(call_ref $t)
)
)
"type mismatch"
)
(assert_invalid
(module
(elem declare func $f)
(type $t (func (param i32) (result i32)))
(func $f (param i32) (result i32) (local.get 0))
(func (export "unreachable") (result i32)
(unreachable)
(ref.func $f)
(call_ref $t)
(drop)
(i64.const 0)
)
)
"type mismatch"
)
(assert_invalid
(module
(type $t (func))
(func $f (param $r externref)
(call_ref $t (local.get $r))
)
)
"type mismatch"
)
(assert_invalid
(module
(type $t (func))
(func $f (param $r funcref)
(call_ref $t (local.get $r))
)
)
"type mismatch"
)