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