| 1 | module scheme |
| 2 | |
| 3 | import context |
| 4 | import os |
| 5 | import reader |
| 6 | import time |
| 7 | |
| 8 | fn record_r6_control_text(mut vm VM, source string) string { |
| 9 | return write(vm.eval(source) or { panic('${source}: ${err}') }) or { panic(err) } |
| 10 | } |
| 11 | |
| 12 | fn test_r6_protocol_callback_errors_exit_and_emergency_cleanup() { |
| 13 | root := os.join_path(os.temp_dir(), 'vile-r6-record-${os.getpid()}-${time.sys_mono_now()}') |
| 14 | os.mkdir_all(root) or { panic(err) } |
| 15 | defer { os.rmdir_all(root) or {} } |
| 16 | path := os.join_path(root, 'record.txt') |
| 17 | for emergency in [false, true] { |
| 18 | mut vm := new() |
| 19 | exit := if emergency { 'emergency-exit' } else { 'exit' } |
| 20 | source := '(define port #f) (define mark 0) |
| 21 | (call-with-output-file "${path}" (lambda(p)(set! port p) |
| 22 | (dynamic-wind (lambda()(set! mark 1)) |
| 23 | (lambda()(define-record-type t (protocol (lambda(p)(${exit} 7))))) |
| 24 | (lambda()(set! mark 2)))))' |
| 25 | if _ := vm.eval(source) { |
| 26 | assert false |
| 27 | } else { |
| 28 | assert err is ExitRequest, err.msg() |
| 29 | if err is ExitRequest { |
| 30 | assert err.status == 7 && err.emergency == emergency |
| 31 | } |
| 32 | } |
| 33 | assert record_r6_control_text(mut vm, '(list (output-port-open? port) mark)') == if emergency { |
| 34 | '(#f 1)' |
| 35 | } else { |
| 36 | '(#f 2)' |
| 37 | } |
| 38 | assert vm.active.len == 0 |
| 39 | os.rm(path) or { panic(err) } |
| 40 | } |
| 41 | mut vm := new() |
| 42 | for source in ['(define-record-type t (protocol (lambda(p)42)))', |
| 43 | '(define-record-type t (protocol (lambda(p)(values p p))))', |
| 44 | '(define-record-type t (protocol (lambda(p)(/ 1 0))))', |
| 45 | '(define-record-type t (fields x)) (make-t)', '(define-record-type t (fields x)) (make-t 1 2)'] { |
| 46 | if _ := vm.eval(source) { |
| 47 | assert false, source |
| 48 | } |
| 49 | assert vm.active.len == 0 |
| 50 | assert record_r6_control_text(mut vm, '(+ 20 22)') == '42' |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn test_r6_capture_during_protocol_preparation_replays_definition_without_reinvoking_protocol() { |
| 55 | mut vm := new() |
| 56 | assert record_r6_control_text(mut vm, '(define saved #f) (define calls 0) |
| 57 | (define-record-type t (fields x) |
| 58 | (protocol (lambda(p)(set! calls (+ calls 1)) (call/cc(lambda(k)(set! saved k)p))))) |
| 59 | (define first (make-t 20)) (list (t-x first) calls)') == '(20 1)' |
| 60 | for _ in 0 .. 3 { |
| 61 | gc_collect() |
| 62 | // The continuation resumes at the protocol return: the replacement constructor |
| 63 | // is committed and later source forms replay, without restarting the callback. |
| 64 | assert record_r6_control_text(mut vm, '(saved (lambda(x) first))') == '(20 1)' |
| 65 | } |
| 66 | assert vm.active.len == 0 |
| 67 | } |
| 68 | |
| 69 | fn test_r6_protocol_native_capture_reentry_and_tail_constructor_recursion() { |
| 70 | mut vm := new_with_options(Options{ max_steps: 0, max_frames: 12 }) or { panic(err) } |
| 71 | vm.register('host-prepare', fn (mut vm VM, args []Value) !Value { |
| 72 | vm.define('host-p', args[0])! |
| 73 | return vm.eval('(call/cc(lambda(k)(set! saved k)host-p))') |
| 74 | }) or { panic(err) } |
| 75 | assert record_r6_control_text(mut vm, '(define saved #f) |
| 76 | (define-record-type t (fields x) (protocol host-prepare)) |
| 77 | (define first (make-t 42)) (t-x first)') == '42' |
| 78 | // A completed native entry resumes at its own existing VM boundary. |
| 79 | resumed := vm.eval('(saved (lambda(x)first))') or { panic(err) } |
| 80 | assert resumed.tag == .procedure |
| 81 | assert eqv(vm.call(resumed, [integer(0)]) or { panic(err) }, vm.lookup('first') or { panic(err) }) |
| 82 | assert record_r6_control_text(mut vm, '(define-record-type recursive (fields x) |
| 83 | (protocol(lambda(p)(letrec((run(lambda(n)(if (= n 0)(p 42)(run (- n 1))))))run)))) |
| 84 | (recursive-x (make-recursive 100000))') == '42' |
| 85 | assert vm.active.len == 0 |
| 86 | } |
| 87 | |
| 88 | fn test_r6_protocol_callbacks_share_instruction_limits() { |
| 89 | mut vm := new_with_options(Options{ max_steps: 180 }) or { panic(err) } |
| 90 | if _ := vm.eval('(define-record-type t (protocol(lambda(p)(let loop()(loop)))))') { |
| 91 | assert false |
| 92 | } else { |
| 93 | assert err.msg().contains('step limit'), err.msg() |
| 94 | } |
| 95 | assert vm.active.len == 0 |
| 96 | assert record_r6_control_text(mut vm, '(+ 20 22)') == '42' |
| 97 | } |
| 98 | |
| 99 | fn test_r6_protocol_cancellation_closes_managed_files_without_running_after_thunks() { |
| 100 | root := os.join_path(os.temp_dir(), 'vile-r6-cancel-${os.getpid()}-${time.sys_mono_now()}') |
| 101 | os.mkdir_all(root) or { panic(err) } |
| 102 | defer { os.rmdir_all(root) or {} } |
| 103 | path := os.join_path(root, 'record.txt') |
| 104 | mut background := context.background() |
| 105 | mut ctx, cancel := context.with_cancel(mut background) |
| 106 | defer { cancel() } |
| 107 | mut vm := new() |
| 108 | vm.register('cancel-protocol', fn [cancel] (mut _vm VM, _args []Value) !Value { |
| 109 | cancel() |
| 110 | return integer(0) |
| 111 | }) or { panic(err) } |
| 112 | vm.define('path', string_value(path)) or { panic(err) } |
| 113 | if _ := vm.with_context(mut ctx, fn (mut vm VM) !Value { |
| 114 | return vm.eval('(define port #f) (define mark 0) |
| 115 | (call-with-output-file path(lambda(p)(set! port p) |
| 116 | (dynamic-wind (lambda()(set! mark 1)) |
| 117 | (lambda()(define-record-type t(protocol cancel-protocol))) |
| 118 | (lambda()(set! mark 2)))))') |
| 119 | }) { |
| 120 | assert false |
| 121 | } else { |
| 122 | assert err is CancellationRequest, err.msg() |
| 123 | } |
| 124 | assert vm.active.len == 0 |
| 125 | assert record_r6_control_text(mut vm, '(list (output-port-open? port) mark)') == '(#f 1)' |
| 126 | } |
| 127 | |
| 128 | fn test_r6_anonymous_uids_survive_plain_source_recompilation_and_compiled_replay() { |
| 129 | for wrapper in ['', 'begin', 'begin begin'] { |
| 130 | mut vm := new() |
| 131 | parts := wrapper.split(' ') |
| 132 | mut prefix := '' |
| 133 | mut suffix := '' |
| 134 | for part in parts { |
| 135 | if part != '' { |
| 136 | prefix += '(begin ' |
| 137 | suffix += ')' |
| 138 | } |
| 139 | } |
| 140 | first := vm.eval('(define saved #f) (call/cc(lambda(k)(set! saved k)0)) |
| 141 | ${prefix}(define-record-type t (nongenerative) (fields x))${suffix} |
| 142 | (record-type-descriptor t)') or { panic(err) } |
| 143 | for _ in 0 .. 3 { |
| 144 | gc_collect() |
| 145 | resumed := vm.eval('(saved 0)') or { panic(err) } |
| 146 | assert eqv(first, resumed), wrapper |
| 147 | } |
| 148 | } |
| 149 | mut vm := new() |
| 150 | assert record_r6_control_text(mut vm, '(define saved #f) |
| 151 | (define (factory) (call/cc(lambda(k)(set! saved k)0)) |
| 152 | (define-record-type t(nongenerative)(fields x)) (record-type-descriptor t)) |
| 153 | (define original(factory)) (eq? original(factory))') == '#t' |
| 154 | assert record_r6_control_text(mut vm, '(eq? original(saved 0))') == '#t' |
| 155 | } |
| 156 | |
| 157 | fn test_r6_genuine_macro_reexpansions_get_fresh_automatic_uids_and_stable_explicit_uids() { |
| 158 | for wrapper in ['', 'begin', 'begin begin'] { |
| 159 | mut vm := new() |
| 160 | mut prefix := '' |
| 161 | mut suffix := '' |
| 162 | for part in wrapper.split(' ') { |
| 163 | if part != '' { |
| 164 | prefix += '(begin ' |
| 165 | suffix += ')' |
| 166 | } |
| 167 | } |
| 168 | first := vm.eval('(define-syntax build (syntax-rules () ((_ auto explicit) |
| 169 | (begin ' + prefix + '(define-record-type auto(nongenerative)(fields x))' + suffix + ' |
| 170 | (define-record-type explicit(nongenerative stable-source-uid)(fields x)))))) |
| 171 | (define saved #f) (call/cc(lambda(k)(set! saved k)0)) (build auto explicit) |
| 172 | (list(record-type-descriptor auto)(record-type-descriptor explicit))') or { panic(err) }.list_items() or { panic(err) } |
| 173 | second := vm.eval('(saved 0)') or { panic(err) }.list_items() or { panic(err) } |
| 174 | assert !eqv(first[0], second[0]) |
| 175 | assert eqv(first[1], second[1]) |
| 176 | } |
| 177 | // Capturing after expansion, inside a staged expanded begin, keeps both |
| 178 | // occurrences distinct and stable without re-expanding the outer macro. |
| 179 | mut vm := new() |
| 180 | first := vm.eval('(define saved #f) |
| 181 | (define-syntax build (syntax-rules () ((_) |
| 182 | (begin (call/cc(lambda(k)(set! saved k)0)) |
| 183 | (begin(define-record-type a(nongenerative)(fields x))) |
| 184 | (begin(define-record-type b(nongenerative)(fields x))) |
| 185 | (list(record-type-descriptor a)(record-type-descriptor b)))))) (build)') or { panic(err) }.list_items() or { panic(err) } |
| 186 | second := vm.eval('(saved 0)') or { panic(err) }.list_items() or { panic(err) } |
| 187 | assert !eqv(first[0], first[1]) |
| 188 | assert eqv(first[0], second[0]) && eqv(first[1], second[1]) |
| 189 | } |
| 190 | |
| 191 | fn test_r6_anonymous_uids_in_distinct_included_documents_keep_virtual_occurrence_identity() { |
| 192 | root := os.join_path(os.temp_dir(), 'vile-record-uids-${os.getpid()}-${time.sys_mono_now()}') |
| 193 | os.mkdir_all(root) or { panic(err.msg()) } |
| 194 | defer { os.rmdir_all(root) or {} } |
| 195 | a := '(call/cc(lambda(k)(set! saved-a k)0)) (define-record-type a(nongenerative)(fields x))' |
| 196 | b := '(call/cc(lambda(k)(set! saved-b k)0)) (define-record-type b(nongenerative)(fields x))' |
| 197 | first_doc := reader.read_all(a) or { panic(err.msg()) } |
| 198 | second_doc := reader.read_all(b) or { panic(err.msg()) } |
| 199 | // Equal local indexes exercise SourceDocuments' disjoint virtual ranges. |
| 200 | assert first_doc.forms[1] == second_doc.forms[1] |
| 201 | first_path := os.join_path(root, 'a.scm') |
| 202 | second_path := os.join_path(root, 'b.scm') |
| 203 | os.write_file(first_path, a) or { panic(err.msg()) } |
| 204 | os.write_file(second_path, b) or { panic(err.msg()) } |
| 205 | mut vm := new() |
| 206 | original := vm.eval('(define saved-a #f)(define saved-b #f) |
| 207 | (include "${first_path}" "${second_path}") |
| 208 | (list(record-type-descriptor a)(record-type-descriptor b))') or { panic(err.msg()) }.list_items() or { panic(err.msg()) } |
| 209 | assert !eqv(original[0], original[1]) |
| 210 | // The include has already expanded before the capture inside its first file. |
| 211 | // Replaying its remaining driver stages must reuse both exact occurrences. |
| 212 | for _ in 0 .. 3 { |
| 213 | gc_collect() |
| 214 | replay := vm.eval('(saved-a 0)') or { panic(err.msg()) }.list_items() or { panic(err.msg()) } |
| 215 | assert eqv(original[0], replay[0]) && eqv(original[1], replay[1]) |
| 216 | } |
| 217 | assert vm.active.len == 0 |
| 218 | } |
| 219 | |