-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy patht20_api.nim
254 lines (207 loc) Β· 5.36 KB
/
t20_api.nim
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
include preamble
import tests/exports
suite "cps api":
block:
## bootstrap
var k = newKiller 1
proc bootstrap(): int {.cps: Cont.} =
noop()
step 1
noop()
return 3
var i = bootstrap()
check i is int, "bootstrap's output is not an int"
check i == 3, "bootstrap's output has the wrong value"
check k
block:
## whelp
var k = newKiller 0
proc whelped(): int {.cps: Cont.} =
noop()
step 1
noop()
return 3
var c = whelp whelped()
check "whelp's output is bogus":
c is Cont
check k
block:
## state symbols and trampoline
var k = newKiller 1
proc states(): int {.cps: Cont.} =
noop()
step 1
noop()
return 3
var c = whelp states()
check "whelp initial state surprised us":
not c.dismissed
not c.finished
c.state == State.Running
c.running
c = cps.trampoline c
check "whelp state after trampoline surprised us":
not c.dismissed
c.state == State.Finished
c.finished
not c.running
check k
block:
## trampolineIt
var k = newKiller 1
proc boing(): int {.cps: Cont.} =
noop()
step 1
noop()
return 3
var c = whelp boing()
trampolineIt c:
check "state inside trampolineIt is " & $it.state:
not it.dismissed
it.running
check "state post-trampolineIt is " & $c.state:
not c.dismissed
c.finished
check k
block:
## magic voodoo
var k = newKiller 4
proc magic(c: Cont): Cont {.cpsMagic.} =
step 2
check not c.dismissed, "continuation was dismissed"
check not c.finished, "continuation was finished"
result = c
proc voodoo(c: Cont): int {.cpsVoodoo.} =
step 3
check not c.dismissed, "continuation was dismissed"
check not c.finished, "continuation was finished"
result = 2
proc foo(): int {.cps: Cont.} =
noop()
step 1
check k.step == 1, "right"
magic()
check k.step == 2, "magic failed to run"
noop()
check voodoo() == 2, "voodoo failed to yield 2"
noop()
step 4
return 3
check foo() == 3
check k
block:
## exporting CPS procedures works
check entry() == 42
block:
## accessing exported continuation's result from a continuation works
var k = newKiller 2
proc foo() {.cps: Cont.} =
step 1
check entry() == 42
step 2
foo()
check k
block:
## one can whelp a cps'd proc that was borrowed
type
D = distinct int
proc bar(x: int) {.cps: Continuation.} =
discard
proc bar(d: D) {.borrow.}
discard whelp bar(42.D)
block:
## one can call a cps'd proc that was borrowed... from inside cps
type
D = distinct int
var k = newKiller 3
proc bar(x: int; y = 0.0) {.cps: Continuation.} =
check y == 3.5
check x == 42
step 2
proc bar(d: D; y = 0.0) {.borrow.}
proc foo() {.cps: Continuation.} =
step 1
bar(y = 3.5, d = 42.D)
step 3
foo()
check k
block:
## calling magic that is not defined for the base type should not compile
skip "FIXME: Figure out a way to test compile-time failures":
type AnotherCont = ref object of Continuation
proc magic(c: AnotherCont): AnotherCont {.cpsMagic.} = discard
proc foo() {.cps: Cont.} =
magic()
block:
## calling magic/voodoo with generics continuation parameter works
var k = newKiller 3
type AnotherCont = ref object of Continuation
proc magic(c: Cont or AnotherCont): auto {.cpsMagic.} =
k.step 3
c
proc voodoo(c: Cont or AnotherCont) {.cpsVoodoo.} =
k.step 2
proc foo() {.cps: Cont.} =
step 1
voodoo()
magic()
foo()
check k
block:
## magic/voodoo can be defined with `using`
skip "`using` can only be used on top-level, and if it is, balls fails the test in debug mode":
using c: Cont
var k = newKiller 4
proc magic(c): Cont {.cpsMagic.} =
step 2
check not c.dismissed, "continuation was dismissed"
check not c.finished, "continuation was finished"
result = c
proc voodoo(c): int {.cpsVoodoo.} =
step 3
check not c.dismissed, "continuation was dismissed"
check not c.finished, "continuation was finished"
result = 2
proc foo(): int {.cps: Cont.} =
noop()
step 1
check k.step == 1, "right"
magic()
check k.step == 2, "magic failed to run"
noop()
check voodoo() == 2, "voodoo failed to yield 2"
noop()
step 4
return 3
check foo() == 3
check k
block:
## parent-child voodoo works correctly
# https://github.com/nim-works/cps/issues/145
type
C = ref object of Continuation
val: int
proc send(c: C, v: int) {.cpsVoodoo.} =
c.val = v
proc recv(c: C): int {.cpsVoodoo.} =
c.val
var k = newKiller(6)
proc level_two() {.cps:C.} =
step 2
send(42)
step 3
echo "level_two: ", recv()
let x = recv()
echo "level_two: ", x
step 4
proc level_one() {.cps:C.} =
step 1
level_two()
step 5
echo "level_one: ", recv()
let v = recv()
step 6
check v == 0
var a = whelp level_one()
trampoline a
check k