vile / scheme / loop_continue_control_test.v
50 lines · 48 sloc · 2.12 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import reader
4
5fn test_continue_winding_landing_and_saved_after_callbacks_replay_immutable_destinations() {
6 mut vm := new()
7 vm.eval('(define after-k #f) (define steps (list)) (define path (list))
8 (define (before) (set! path (cons 1 path)))
9 (define (after) (set! path (cons 2 path))
10 (call/cc (lambda (k) (if after-k #f (set! after-k k)) #f)))') or { panic(err.msg()) }
11 source := '(do ((i 0 (begin (set! steps (cons (+ i 1) steps)) (+ i 1))))
12 ((= i 3) (reverse steps)) (continue) missing)'
13 mut code := &Code{ doc: reader.read_all(source) or { panic(err.msg()) } }
14 context := vm.next_context()
15 mut machine := &Machine{ context: context, logical_context: context, function: compile_program(code, code.doc.forms) }
16 vm.active << machine
17 mut injected := false
18 for _ in 0 .. 1000 {
19 instruction := machine.function.instructions[machine.ip]
20 machine.ip++
21 machine.origin = instruction.origin
22 if instruction.op == .loop_continue {
23 // The public syntaxes prohibit jumping across a procedure boundary.
24 // Inject an enclosing host/control extent at the actual opcode to
25 // exercise its general winder path and a real Scheme after callback.
26 machine.wind = &WindFrame{
27 depth: 1
28 before: vm.lookup('before') or { panic(err.msg()) }
29 after: vm.lookup('after') or { panic(err.msg()) }
30 }
31 machine.execute(mut vm, instruction) or { panic(err.msg()) }
32 injected = true
33 break
34 }
35 machine.execute(mut vm, instruction) or { panic(err.msg()) }
36 }
37 assert injected
38 first := machine.run(mut vm) or { panic(err.msg()) }
39 vm.leave_context()
40 assert write(first) or { panic(err.msg()) } == '(1 2 3)'
41 for index, arguments in [[]Value{}, [integer(42)], [integer(20), integer(22)]] {
42 gc_collect()
43 result := vm.call(vm.lookup('after-k') or { panic(err.msg()) }, arguments) or { panic(err.msg()) }
44 expected := ['(1 2 3 1 2 3)', '(1 2 3 1 2 3 1 2 3)', '(1 2 3 1 2 3 1 2 3 1 2 3)'][index]
45 assert write(result) or { panic(err.msg()) } == expected
46 assert vm.active.len == 0
47 }
48 assert write(vm.eval('path') or { panic(err.msg()) }) or { panic(err.msg()) } == '(2)'
49 assert write(first) or { panic(err.msg()) } == '(1 2 3)'
50}
51