-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpreamble.nim
58 lines (49 loc) · 1.57 KB
/
preamble.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
#
# modules and sanity checking that we want to include
# in all test files
#
when not declaredInScope(InfiniteLoop):
import pkg/balls
import cps except trampoline
include killer
type
EmptyLoop = CatchableError
InfiniteLoop = CatchableError
Cont* = ref object of Continuation
var jumps: int
proc trampoline[T: Continuation](c: sink T) {.used.} =
jumps = 0
var c: Continuation = move c
while c.running:
# capture the exception in the environment
let exception = getCurrentException()
try:
var y = c.fn
var x = y(c)
c = x
except CatchableError:
if not c.dismissed:
writeStackFrames c
raise
# the current exception should not change
check getCurrentException() == exception
inc jumps
if jumps > 1000:
raise InfiniteLoop.newException: $jumps & " iterations"
if jumps == 0:
raise EmptyLoop.newException:
"continuations test best when they, uh, bounce"
proc noop*(c: Cont): Cont {.cpsMagic.} = c
# We have a lot of these for the purpose of control-flow validation
{.warning[UnreachableCode]: off.}
template shouldRun(wanted: int; body: untyped) {.used.} =
var measured {.inject.} = 0
try:
body
finally:
check measured == wanted:
if wanted == 0: "oops; continuation ran"
elif measured == 0: "continuation never ran"
elif measured > wanted: "continuation ran too often"
else: "continuation ran too rarely"
template ran {.used, dirty.} = inc measured