-
Notifications
You must be signed in to change notification settings - Fork 17
/
t50_hooks.nim
337 lines (292 loc) Β· 8.69 KB
/
t50_hooks.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
include preamble
import std/genasts
import std/macros
import std/os
import std/sequtils
import std/strutils
from cps/spec import Hook, cpsStackFrames
from cps/hooks import findColonLit
suite "hooks":
block:
## cooperative yield hooks are used automatically
shouldRun 6:
proc coop(c: Cont): Cont {.cpsMagic, used.} =
ran()
result = c
proc foo() {.cps: Cont.} =
var i = 0
while i < 3:
ran()
noop()
inc i
if i == 0:
continue
if i > 2:
break
foo()
block:
## control-flow tracing hooks are used automatically
var found: seq[string]
macro trace(hook: static[Hook]; c, n: typed;
fun: string; info: LineInfo; body: typed): untyped =
var body =
if body.kind == nnkNilLit:
newEmptyNode()
else:
body
let fun =
if hook == Stack: c.findColonLit("fun", string)
else: fun.strVal
genAst(c, hook, fun, info, body):
let sub = fun.split("_", maxsplit=1)[0]
var last =
case hook
of Dealloc: "π"
of Stack: sub
else: astToStr c
var path = info.filename.lastPathPart
path = if path == "t50_hooks.nim": "π" else: path
found.add "$#: $# $# $#" % [ $hook, $sub, last, path ]
body
proc bar() {.cps: Cont.} =
noop()
proc foo() {.cps: Cont.} =
var i = 0
while i < 3:
noop()
inc i
if i == 0:
continue
if i > 2:
break
bar()
foo()
let s = found.join("\10")
proc normalize(s: string): seq[string] =
var s = strip s
result = splitLines s
result = map(result, proc(x: string): string = strip(x))
when cpsStackFrames:
const
expected = """
alloc: cps:foo() env Cont π
head: trace nil π
stack: foo foo π
boot: c nil π
trace: foo continuation π
coop: Cont nil environment.nim
trace: cps:foo() loop continuation π
trace: cps:foo() jump noop() continuation π
tail: Cont continuation π
alloc: cps:bar() env Cont π
stack: bar bar π
boot: Cont nil π
pass: cps:foo() env Cont(continuation) π
trace: bar continuation π
trace: cps:bar() jump noop() continuation π
pass: continuation.mom Cont(continuation) environment.nim
coop: result nil normalizedast.nim
dealloc: cps:bar() env π environment.nim
trace: cps:foo() child bar() continuation normalizedast.nim
coop: Cont nil environment.nim
trace: cps:foo() loop continuation π
trace: cps:foo() jump noop() continuation π
tail: Cont continuation π
alloc: cps:bar() env Cont π
stack: bar bar π
boot: Cont nil π
pass: cps:foo() env Cont(continuation) π
trace: bar continuation π
trace: cps:bar() jump noop() continuation π
pass: continuation.mom Cont(continuation) environment.nim
coop: result nil normalizedast.nim
dealloc: cps:bar() env π environment.nim
trace: cps:foo() child bar() continuation normalizedast.nim
coop: Cont nil environment.nim
trace: cps:foo() loop continuation π
trace: cps:foo() jump noop() continuation π
dealloc: cps:foo() env π π
"""
else:
const
expected = """
alloc: cps:foo() env Cont π
head: trace nil π
boot: c nil π
trace: foo continuation π
coop: Cont nil environment.nim
trace: cps:foo() loop continuation π
trace: cps:foo() jump noop() continuation π
tail: Cont continuation π
alloc: cps:bar() env Cont π
boot: Cont nil π
pass: cps:foo() env Cont(continuation) π
trace: bar continuation π
trace: cps:bar() jump noop() continuation π
pass: continuation.mom Cont(continuation) environment.nim
coop: result nil normalizedast.nim
dealloc: cps:bar() env π environment.nim
trace: cps:foo() child bar() continuation normalizedast.nim
coop: Cont nil environment.nim
trace: cps:foo() loop continuation π
trace: cps:foo() jump noop() continuation π
tail: Cont continuation π
alloc: cps:bar() env Cont π
boot: Cont nil π
pass: cps:foo() env Cont(continuation) π
trace: bar continuation π
trace: cps:bar() jump noop() continuation π
pass: continuation.mom Cont(continuation) environment.nim
coop: result nil normalizedast.nim
dealloc: cps:bar() env π environment.nim
trace: cps:foo() child bar() continuation normalizedast.nim
coop: Cont nil environment.nim
trace: cps:foo() loop continuation π
trace: cps:foo() jump noop() continuation π
dealloc: cps:foo() env π π
"""
let x = expected.normalize
let y = s.normalize
if x != y:
if x.len != y.len:
checkpoint "expected:"
for n in x.items:
checkpoint n
checkpoint "\n"
checkpoint "received:"
for n in y.items:
checkpoint n
else:
var i = 0
for (a, b) in zip(x, y).items:
if a != b:
checkpoint "#", i, " < ", a
checkpoint "#", i, " > ", b
inc i
fail "trace output doesn't match"
block:
## custom continuation allocators are used automatically
shouldRun 1:
proc alloc[T](root: typedesc[Cont]; c: typedesc[T]): T =
ran()
new c
proc foo(x: int) {.cps: Cont.} =
check x == 3
noop()
check x == 3
foo(3)
block:
## custom continuation deallocators can nil the continuation
shouldRun 5:
proc dealloc[T: Cont](c: sink T; E: typedesc[T]): E =
ran() # (runs twice)
c = nil
proc bar() {.cps: Cont.} =
ran()
noop()
ran()
proc foo(x: int) {.cps: Cont.} =
check x == 3
noop()
check x == 3
bar()
ran()
foo(3)
block:
## custom continuation deallocators work with whelp
shouldRun 4:
proc dealloc[T: Cont](c: sink T; E: typedesc[T]): E =
ran()
c = nil
proc bar() {.cps: Cont.} =
ran()
noop()
ran()
proc foo(x: int) {.cps: Cont.} =
check x == 3
noop()
check x == 3
bar()
ran()
let c = whelp foo(3)
trampoline c
block:
## custom continuation passing hook works
shouldRun 34:
proc pass(a: Cont; b: Continuation): Continuation =
echo "pass to parent"
for n in 0..9: ran()
result = b
proc pass(a: Cont; b: Cont): Continuation =
echo "pass to child"
for n in 0..9: ran()
for n in 0..9: ran()
result = b
proc bar() {.cps: Cont.} =
ran()
noop()
ran()
proc foo() {.cps: Cont.} =
ran()
bar()
ran()
foo()
block:
## custom continuation bootstrap hook works
shouldRun 2:
proc bar() {.cps: Cont.} =
noop()
proc boot(c: Cont): Cont =
ran()
result = c
proc foo() {.cps: Cont.} =
bar()
foo()
var c = whelp foo()
c = cps.trampoline c
block:
## custom continuation head/tail setup hooks work
var h, t = 0
proc head(c: Cont): Cont =
inc h
result = c
proc tail(mom: Continuation; c: Cont): Continuation =
inc t
result = c
result.mom = mom
proc bar() {.cps: Cont.} =
check h == 1, "parent triggered second"
noop()
proc foo() {.cps: Cont.} =
check h == 1, "parent triggered first"
check t == 0, "child triggered first"
bar()
check t == 1, "child triggered second"
var c = whelp foo()
c = cps.trampoline c
check "bzzzt whelped":
h == t
t == 1
h = 0
t = 0
foo()
check "bzzzt bootstrapped":
h == t
t == 1
block:
## custom continuation exception handling works
var k = newKiller 4
proc unwind(c: Cont; ex: ref Exception): Continuation {.cpsMagic, used.} =
inc k
result = cps.unwind(c, ex)
proc bar() {.cps: Cont.} =
step 2
noop()
raise IOError.newException "hol' up"
proc foo() {.cps: Cont.} =
step 1
bar()
step 4
expect IOError:
foo()
check k