-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy patht80_try2.nim
330 lines (277 loc) Β· 6.33 KB
/
t80_try2.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
import std/strutils
include preamble
from cps/spec import cpsStackFrames
suite "try statements":
block:
## handling exception across multiple continuations
var k = newKiller(6)
proc foo() {.cps: Cont.} =
noop()
step 4
raise newException(ValueError, "foo")
proc bar() {.cps: Cont.} =
noop()
step 3
foo()
proc barbar() {.cps: Cont.} =
try:
noop()
step 2
bar()
except ValueError as e:
step 5
doAssert e.msg == "foo"
proc foobar() {.cps: Cont.} =
step 1
barbar()
step 6
trampoline whelp(foobar())
check k
block:
## try statement with a single statement which is a cps assignment
var k = newKiller(2)
proc bar(): int {.cps: Cont.} =
step 1
42
proc foo() {.cps: Cont.} =
var x = 0
try:
x = bar()
except CatchableError:
fail "This branch should not be executed"
step 2
check x == 42
trampoline whelp(foo())
check k
block:
## try-finally-reraise escape via break statements.
var k = newKiller(1)
proc foo() {.cps: Cont.} =
while true:
try:
noop()
step 1
raise newException(ValueError, "")
finally:
break
fail "statement in while-loop after break"
fail "statement after unhandled exception"
expect ValueError:
trampoline whelp(foo())
check k
block:
## try-finally-reraise escape via continue statements.
var k = newKiller(1)
proc foo() {.cps: Cont.} =
while true:
try:
noop()
step 1
raise newException(ValueError, "")
finally:
continue
fail "statement in while-loop after finally"
fail "statement after unhandled exception"
expect ValueError:
trampoline whelp(foo())
check k
block:
## try: raise() except: continue
var k = newKiller(2)
proc foo() {.cps: Cont.} =
try:
while true:
noop()
inc k
if k.step == 2:
break
try:
raise newException(ValueError, "oops")
except ValueError:
continue
fail "statement in while-loop after continue"
except ValueError:
fail "uncaught exception"
trampoline whelp(foo())
check k
block:
## try-finally-reraise escape via return statements.
var k = newKiller(1)
proc foo() {.cps: Cont.} =
try:
noop()
step 1
raise newException(ValueError, "")
finally:
return
fail "statement after return"
expect ValueError:
trampoline whelp(foo())
check k
block:
## try-finally-reraise handle after escape attempt
var k = newKiller(2)
proc foo() {.cps: Cont.} =
try:
while true:
try:
noop()
step 1
raise newException(ValueError, "")
finally:
break
except ValueError:
step 2
trampoline whelp(foo())
check k
block:
## the stack trace probably still works
when not cpsStackFrames:
skip"--stacktrace:off specified"
else:
var r = 0
proc foo() {.cps: Cont.} =
noop()
inc r
try:
raise newException(CatchableError, "test")
except CatchableError:
let frames = renderStackFrames()
check frames.len > 0, "expected at least one stack trace record"
check "t80_try2.nim" in frames[0], "couldn't find t80_try2.nim in the trace"
raise
try:
trampoline whelp(foo())
inc r
except CatchableError as e:
check e.msg == "test", "unable to pass exception message from cps"
check r == 1
suite "defer statements":
var r = 0
block:
## a defer statement works across a continuation
r = 0
proc foo() {.cps: Cont.} =
defer:
check r == 2, "defer run before end of scope"
inc r
inc r
noop()
inc r
foo()
check r == 3
block:
## a basic defer statement is supported
r = 0
proc foo() {.cps: Cont.} =
inc r
noop()
defer:
check r == 4
inc r
inc r
defer:
check r == 3
inc r
inc r
foo()
check r == 5
block:
## a defer in a nested template is supported
r = 0
template deferChk(i: int) =
inc r
defer:
check r == i
inc r
proc foo() {.cps: Cont.} =
deferChk(5)
inc r
deferChk(4)
inc r
foo()
check r == 6
block:
## a defer inside a block statement works
r = 0
proc foo() {.cps: Cont.} =
inc r
block:
defer:
check r == 2
inc r
inc r
defer:
check r == 4
inc r
inc r
foo()
check r == 5
block:
## a naked defer is not a problem
r = 0
proc foo() {.cps: Cont.} =
defer:
inc r
foo()
check r == 1
when defined(gcArc) or defined(gcOrc):
suite "breaking deterministic memory managers":
block:
## try-except-statement splits
proc foo() {.cps: Cont.} =
var k = newKiller(3)
step 1
try:
noop()
step 2
except CatchableError:
fail "this branch should not run"
step 3
check k
foo()
block:
## try-except splits with raise
proc foo() {.cps: Cont.} =
var k = newKiller(4)
step 1
try:
noop()
step 2
raise newException(CatchableError, "")
fail "statement run after raise"
except CatchableError:
step 3
step 4
check k
foo()
block:
## try-finally-statement splits
proc foo() {.cps: Cont.} =
var k = newKiller(4)
step 1
try:
noop()
step 2
finally:
step 3
step 4
check k
foo()
block:
## try-except-finally splits with raise
proc foo() {.cps: Cont.} =
var k = newKiller(5)
step 1
try:
noop()
step 2
raise newException(CatchableError, "")
fail "statement run after raise"
except CatchableError:
step 3
finally:
step 4
step 5
check k
foo()