| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | struct ScopeArgumentObservation { |
| 7 | text string |
| 8 | message string |
| 9 | steps int |
| 10 | } |
| 11 | |
| 12 | // Execute an independently compiled copy through the original closure protocol. |
| 13 | // This transformation belongs only to tests, including the zero-argument case. |
| 14 | fn scope_arguments_as_closures(mut function Function) int { |
| 15 | mut converted := 0 |
| 16 | for i in 0 .. function.children.len { |
| 17 | mut child := function.children[i] |
| 18 | converted += scope_arguments_as_closures(mut child) |
| 19 | } |
| 20 | for i in 0 .. function.instructions.len { |
| 21 | ins := function.instructions[i] |
| 22 | op := match ins.op { |
| 23 | .scope_function { Op.closure } |
| 24 | .scope_call { Op.call } |
| 25 | .scope_tail { Op.tail_call } |
| 26 | else { ins.op } |
| 27 | } |
| 28 | function.instructions[i] = Instruction{...ins, op: op} |
| 29 | if ins.op == .scope_function { converted++ } |
| 30 | } |
| 31 | return converted |
| 32 | } |
| 33 | |
| 34 | fn scope_arguments_observe(source string, reference bool, limit int, frames int) !ScopeArgumentObservation { |
| 35 | mut vm := new_with_options(Options{max_steps: limit, max_frames: frames})! |
| 36 | context := vm.next_context() |
| 37 | // Compile all forms now: a staged source driver would not yet contain the |
| 38 | // binding instructions that the reference must replace. |
| 39 | doc := reader.read_all(source)! |
| 40 | mut function := compile_program(vm.new_code(doc, context, unsafe { nil }, ''), doc.forms) |
| 41 | if reference { assert scope_arguments_as_closures(mut function) > 0 } |
| 42 | mut machine := Machine{function: function, context: context, logical_context: context} |
| 43 | entry := vm.enter_program(mut machine)! |
| 44 | defer { vm.leave_entry(entry) } |
| 45 | mut message := '' |
| 46 | value := machine.run(mut vm) or { message = err.msg(); unspecified() } |
| 47 | return ScopeArgumentObservation{text: if message == '' { write(value)! } else { '' }, message: message, steps: machine.steps} |
| 48 | } |
| 49 | |
| 50 | fn test_scope_arguments_match_closure_results_and_exact_virtual_steps() { |
| 51 | for source, expected in { |
| 52 | '(let ((a 20)(b 22)) (+ a b))': '42' |
| 53 | '(let ((x 10)) (let ((x (+ x 1))(y x)) (list x y)))': '(11 10)' |
| 54 | '(define x 1)(let ((a(begin(set! x(+ x 1)) x))(b(begin(set! x(* x 2)) x))) (list a b x))': '(2 4 4)' |
| 55 | '(let ((x 20)) (list 1 (let ((a 2)) (+ a x)) (let ((b 3)(c 4)) (+ b c x))))': '(1 22 27)' |
| 56 | '(let ((x 7)) (let () (let ((a x)) (+ a x))))': '14' |
| 57 | '(let ((f(lambda(x)(+ x 1)))(x 20)) (let ((y(f x))) (+ x y)))': '41' |
| 58 | '(let ((x 2)) (define y (+ x 1)) (+ x y))': '5' |
| 59 | '(let loop ((n 20)(a 0)) (if (= n 0) a (let ((x n)) (loop (- n 1) (+ a x)))))': '210' |
| 60 | } { |
| 61 | actual := scope_arguments_observe(source, false, 0, 0)! |
| 62 | original := scope_arguments_observe(source, true, 0, 0)! |
| 63 | assert actual.message == '' && actual.text == expected, source |
| 64 | assert actual.text == original.text && actual.steps == original.steps, source |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | fn test_scope_arguments_preserve_step_frame_errors_and_source_locations() { |
| 69 | for source in ['(define(f n)(let ((a n)(b(+ n 1))) (+ a b)))(f 20)', |
| 70 | '(let ((a 1)) (let ((b 2)) (let ((c 3)) (+ a b c))))', |
| 71 | '(let ((a(car 0))) (+ a 1))', '(let ((a 1)) (+ a x) (define x 2))'] { |
| 72 | for frames in [0, 1, 2] { |
| 73 | for limit in 0 .. 48 { |
| 74 | actual := scope_arguments_observe(source, false, limit, frames)! |
| 75 | original := scope_arguments_observe(source, true, limit, frames)! |
| 76 | assert actual.text == original.text && actual.message == original.message, '${source}/${limit}/${frames}' |
| 77 | assert actual.steps == original.steps, '${source}/${limit}/${frames}' |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | fn test_scope_arguments_keep_pending_function_across_multishot_initializers() { |
| 84 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 85 | vm.eval('(define saved #f)(define ticks 0)(define x 10) |
| 86 | (define(work) (let ((a(call/cc(lambda(k)(set! saved k) 1))) |
| 87 | (b(begin(set! ticks(+ ticks 1)) x))) (list a b ticks)))')! |
| 88 | assert write(vm.eval('(work)')!)! == '(1 10 1)' |
| 89 | vm.eval('(set! x 20)')! |
| 90 | gc_collect() |
| 91 | assert write(vm.call(vm.lookup('saved')!, [integer(7)])!)! == '(7 20 2)' |
| 92 | gc_collect() |
| 93 | assert write(vm.call(vm.lookup('saved')!, [integer(9)])!)! == '(9 20 3)' |
| 94 | assert vm.eval('(+ 20 22)')!.whole() == 42 |
| 95 | } |
| 96 | |
| 97 | fn test_scope_arguments_keep_fresh_escaped_locations_and_gc_roots() { |
| 98 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 99 | vm.eval('(define(make n) (let ((x n)(v(vector n))) |
| 100 | (lambda(d) (set! x(+ x d)) (vector-set! v 0 x) v))) |
| 101 | (define a(make 1))(define b(make 100))')! |
| 102 | a := vm.call(vm.lookup('a')!, [integer(2)])! |
| 103 | b := vm.call(vm.lookup('b')!, [integer(4)])! |
| 104 | gc_collect() |
| 105 | assert a.vector_pointer().items[0].whole() == 3 |
| 106 | assert b.vector_pointer().items[0].whole() == 104 |
| 107 | assert vm.call(vm.lookup('a')!, [integer(5)])!.pointer() == a.pointer() |
| 108 | assert a.vector_pointer().items[0].whole() == 8 |
| 109 | assert b.vector_pointer().items[0].whole() == 104 |
| 110 | vm.eval('(define saved #f)(define(make-again) |
| 111 | (let ((x(call/cc(lambda(k)(set! saved k) 10)))) (lambda(d)(set! x(+ x d)) x)))')! |
| 112 | first := vm.call(vm.lookup('make-again')!, [])! |
| 113 | second := vm.call(vm.lookup('saved')!, [integer(100)])! |
| 114 | assert vm.call(first, [integer(1)])!.whole() == 11 |
| 115 | assert vm.call(second, [integer(2)])!.whole() == 102 |
| 116 | } |
| 117 | |
| 118 | fn scope_arguments_host_reentry(mut vm VM, _args []Value) !Value { |
| 119 | mut temporary := arena.new() |
| 120 | temporary.push() |
| 121 | defer { temporary.pop(); temporary.free() } |
| 122 | return vm.eval('(let ((a 20)(b 22)) (+ a b))') |
| 123 | } |
| 124 | |
| 125 | fn test_scope_arguments_survive_borrowed_arena_reentry_and_saved_continuations() { |
| 126 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 127 | vm.register('host-entry', scope_arguments_host_reentry)! |
| 128 | // Continuations and their code stay in the VM's durable allocator. The |
| 129 | // host initializer borrows and releases another arena while this scope's |
| 130 | // private function is pending on the outer machine's operand stack. |
| 131 | vm.eval('(define saved #f)(define(work) |
| 132 | (let ((x(call/cc(lambda(k)(set! saved k) 1))) (y(host-entry))) (+ x y)))')! |
| 133 | assert vm.call(vm.lookup('work')!, [])!.whole() == 43 |
| 134 | gc_collect() |
| 135 | assert vm.call(vm.lookup('saved')!, [integer(10)])!.whole() == 52 |
| 136 | assert vm.active.len == 0 |
| 137 | assert vm.eval('(let ((x 40)) (+ x 2))')!.whole() == 42 |
| 138 | } |
| 139 | |
| 140 | fn test_scope_arguments_preserve_dynamic_extents_macros_and_vile_frontend() { |
| 141 | mut vm := new_with_options(Options{max_steps: 0, max_frames: 0})! |
| 142 | assert write(vm.eval('(define trace(list))(define p(make-parameter 1)) |
| 143 | (define saved #f) |
| 144 | (let ((x(dynamic-wind(lambda()(set! trace(cons 1 trace))) |
| 145 | (lambda()(parameterize((p 7)) (call/cc(lambda(k)(set! saved k)(p))))) |
| 146 | (lambda()(set! trace(cons 2 trace)))))) (list x (p) (reverse trace)))')!)! == '(7 1 (1 2))' |
| 147 | assert write(vm.call(vm.lookup('saved')!, [integer(9)])!)! == '(9 1 (1 2 1 2))' |
| 148 | assert vm.eval('(define-syntax bind-add(syntax-rules()((_ n) |
| 149 | (let ((x n)(y 2)) (+ x y))))) (let ((x 99)) (bind-add 40))')!.whole() == 42 |
| 150 | vm.eval_alt('work := fn(x) { let((y(x + 1), z(x + 2)), { x + y + z }) }')! |
| 151 | assert vm.call(vm.lookup('work')!, [integer(10)])!.whole() == 33 |
| 152 | } |
| 153 | |
| 154 | fn scope_arguments_deny_entry(mut vm VM, mut machine Machine) ! { |
| 155 | mut denied := arena.new(limits: arena.ChunkLimits{max_live_bytes: 0}) |
| 156 | denied.push() |
| 157 | defer { denied.pop(); denied.free() } |
| 158 | machine.call_scope(mut vm, 1, false)! |
| 159 | } |
| 160 | |
| 161 | fn test_scope_arguments_denial_preserves_pending_arguments_and_caller() { |
| 162 | mut bounded := new_bounded(Options{}, arena.ChunkLimits{max_live_bytes: 32 * 1024 * 1024})! |
| 163 | defer { bounded.close() or { panic(err) } } |
| 164 | bounded.run(fn (mut vm VM) !Value { |
| 165 | function := vm.eval('(lambda(a)(+ a 1))')!.procedure_pointer().function |
| 166 | caller := &Function{code: &Code{}} |
| 167 | parent := &Environment{slots: [integer(9)], escaped: true} |
| 168 | mut machine := Machine{function: caller, env: parent, |
| 169 | stack: [payload_value(.scope_function, function), integer(41)], frames: []CallFrame{cap: 1}} |
| 170 | mut denied := false |
| 171 | scope_arguments_deny_entry(mut vm, mut machine) or { assert err is ResourceLimitRequest; denied = true } |
| 172 | assert denied && machine.stack.len == 2 && machine.stack[1].whole() == 41 |
| 173 | assert machine.stack[0].pointer() == voidptr(function) |
| 174 | assert voidptr(machine.function) == voidptr(caller) && voidptr(machine.env) == voidptr(parent) |
| 175 | assert machine.frames.len == 0 && parent.slots[0].whole() == 9 |
| 176 | return vm.eval('(+ 20 22)') |
| 177 | })! |
| 178 | } |
| 179 | |