This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
forked from WebAssembly/spec
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerators.wast
166 lines (145 loc) · 5.02 KB
/
generators.wast
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
;; Generators
;; generator interface
(module $generator
(tag $yield (export "yield") (param i32))
)
(register "generator")
(module $examples
(type $func (func))
(type $cont (cont $func))
(tag $yield (import "generator" "yield") (param i32))
(func $log (import "spectest" "print_i32") (param i32))
;; yields successive natural numbers
(func $naturals (export "naturals")
(local $n i32)
(loop $l
(suspend $yield (local.get $n))
(local.set $n (i32.add (local.get $n) (i32.const 1)))
(br $l)
)
)
;; yields 1-2-3
(func $one-two-three (export "one-two-three")
(suspend $yield (i32.const 1))
(suspend $yield (i32.const 2))
(suspend $yield (i32.const 3))
)
;; yields successive Fibonacci numbers
(func $fibonacci (export "fibonacci")
(local $a i32)
(local $b i32)
(local $t i32)
(local.set $b (i32.const 1))
(loop $l
(suspend $yield (local.get $a))
(local.set $t (local.get $a))
(local.set $a (local.get $b))
(local.set $b (i32.add (local.get $t) (local.get $a)))
(br $l)
)
)
(func $print-first (export "print-first") (param $n i32) (param $k (ref $cont))
(loop $l
(block $on_yield (result i32 (ref $cont))
(if (local.get $n)
(then (resume $cont (tag $yield $on_yield) (local.get $k)))
)
(return)
) ;; $on_yield (result i32 (ref $cont))
(local.set $k)
(call $log)
(local.set $n (i32.add (local.get $n) (i32.const -1)))
(br $l)
)
(unreachable)
)
(func $sum-first (export "sum-first") (param $n i32) (param $k (ref $cont)) (result i32)
(local $sum i32)
(loop $l
(block $on_yield (result i32 (ref $cont))
(if (local.get $n)
(then (resume $cont (tag $yield $on_yield) (local.get $k)))
)
(return (local.get $sum))
) ;; $on_yield (result i32 (ref $cont))
(local.set $k)
(local.set $sum (i32.add (local.get $sum)))
(local.set $n (i32.add (local.get $n) (i32.const -1)))
(br $l)
)
(unreachable)
)
)
(register "examples")
;; storing generators in a global table and then accessing them through i32 handles
;; without knowledge of handlers
(module $manager
(type $func (func))
(type $cont (cont $func))
(tag $yield (import "generator" "yield") (param i32))
(table $active 0 (ref null $cont))
(func $init (export "init") (param $k (ref $cont)) (result i32)
(table.grow $active (local.get $k) (i32.const 1))
)
(func $next (export "next") (param $g i32) (result i32)
(local $next_k (ref $cont))
(local $next_v i32)
(block $on_yield (result i32 (ref $cont))
(resume $cont (tag $yield $on_yield)
(table.get $active (local.get $g))
)
(return (i32.const -1))
) ;; $on_yield (result i32 (ref $cont))
(local.set $next_k)
(local.set $next_v)
(table.set (local.get $g) (local.get $next_k))
(return (local.get $next_v))
)
)
(register "manager")
(module
(type $func (func))
(type $cont (cont $func))
(elem declare func $naturals $fibonacci $one-two-three)
(func $log (import "spectest" "print_i32") (param i32))
(func $naturals (import "examples" "naturals"))
(func $fibonacci (import "examples" "fibonacci"))
(func $one-two-three (import "examples" "one-two-three"))
(func $print-first (import "examples" "print-first") (param $n i32) (param $k (ref $cont)))
(func $sum-first (import "examples" "sum-first") (param $n i32) (param $k (ref $cont)) (result i32))
(func $init (import "manager" "init") (param $k (ref $cont)) (result i32))
(func $next (import "manager" "next") (param i32) (result i32))
(func $print-with-next (param $n i32) (param $gen i32)
(loop $l
(if (i32.eqz (local.get $n)) (then (return)))
(call $next (local.get $gen))
(call $log)
(local.set $n (i32.add (local.get $n) (i32.const -1)))
(br $l)
)
)
(func $interleave-naturals-and-fib
(local $gen1 i32)
(local $gen2 i32)
(local.set $gen1 (call $init (cont.new $cont (ref.func $naturals))))
(local.set $gen2 (call $init (cont.new $cont (ref.func $fibonacci))))
(call $print-with-next (i32.const 5) (local.get $gen1))
(call $print-with-next (i32.const 5) (local.get $gen2))
(call $print-with-next (i32.const 5) (local.get $gen1))
(call $print-with-next (i32.const 5) (local.get $gen2))
(call $print-with-next (i32.const 5) (local.get $gen1))
(call $print-with-next (i32.const 5) (local.get $gen2))
(call $print-with-next (i32.const 5) (local.get $gen1))
(call $print-with-next (i32.const 5) (local.get $gen2))
)
(func $main (export "main")
(call $interleave-naturals-and-fib)
(call $print-first (i32.const 42) (cont.new $cont (ref.func $naturals)))
(call $print-first (i32.const 42) (cont.new $cont (ref.func $fibonacci)))
(call $sum-first (i32.const 101) (cont.new $cont (ref.func $naturals)))
(call $log)
(call $sum-first (i32.const 10) (cont.new $cont (ref.func $one-two-three)))
(call $log)
)
)
(invoke "main")