| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | fn ctc_parent_error(mut _vm VM, _args []Value) !Value { return failure('ctc writer failed') } |
| 6 | |
| 7 | fn ctc_parent_close_error(mut _vm VM, _args []Value) !Value { return failure('ctc closer failed') } |
| 8 | |
| 9 | fn ctc_parent_depth(mut vm VM, _args []Value) !Value { return integer(vm.active.len) } |
| 10 | |
| 11 | fn ctc_parent_eval(mut vm VM, code string) Value { |
| 12 | return vm.eval(code) or { panic('${code}: ${err.msg()}') } |
| 13 | } |
| 14 | |
| 15 | fn ctc_parent_setup(mut vm VM, bytes []u8, codec CodecKind) (Value, Value) { |
| 16 | vm.register('ct-writer-error', ctc_parent_error) or { panic(err.msg()) } |
| 17 | vm.register('ct-closer-error', ctc_parent_close_error) or { panic(err.msg()) } |
| 18 | vm.register('ct-depth', ctc_parent_depth) or { panic(err.msg()) } |
| 19 | vm.define('ct-source', bytevector(bytes)) or { panic(err.msg()) } |
| 20 | vm.define('ct-transcoder', transcoder_value(codec, .none, .raise)) or { panic(err.msg()) } |
| 21 | ctc_parent_eval(mut vm, '(define ct-rx 0) (define ct-tx (make-bytevector 64 0)) (define ct-used 0) |
| 22 | (define ct-writes 0) (define ct-fail #f) (define ct-close-fail #f) |
| 23 | (define ct-closes 0) (define ct-seen #f) (define ct-depths (quote ())) |
| 24 | (define ct-binary (make-custom-binary-input/output-port "ctc" |
| 25 | (lambda (b s n) (let ((count (min n 1 (- (bytevector-length ct-source) ct-rx)))) |
| 26 | (bytevector-copy! b s ct-source ct-rx (+ ct-rx count)) (set! ct-rx (+ ct-rx count)) count)) |
| 27 | (lambda (b s n) (set! ct-depths (cons (ct-depth) ct-depths)) |
| 28 | (set! ct-writes (+ ct-writes 1)) |
| 29 | (if (and ct-fail (> ct-writes 1)) (ct-writer-error) |
| 30 | (let ((count (min n 1))) (bytevector-copy! ct-tx ct-used b s (+ s count)) |
| 31 | (set! ct-used (+ ct-used count)) count))) #f #f |
| 32 | (lambda () (set! ct-closes (+ ct-closes 1)) |
| 33 | (set! ct-seen (list (input-port-open? ct-port) (output-port-open? ct-port))) |
| 34 | (set! ct-depths (cons (ct-depth) ct-depths)) |
| 35 | (if ct-close-fail (ct-closer-error) #t)))) |
| 36 | (define ct-port (transcoded-port ct-binary ct-transcoder))') |
| 37 | return vm.lookup('ct-binary') or { panic(err.msg()) }, vm.lookup('ct-port') or { panic(err.msg()) } |
| 38 | } |
| 39 | |
| 40 | fn ctc_parent_bytes(vm &VM) []u8 { |
| 41 | length := vm.lookup('ct-used') or { panic(err.msg()) }.whole() |
| 42 | return vm.lookup('ct-tx') or { panic(err.msg()) }.bytevector_pointer().bytes[..int(length)].clone() |
| 43 | } |
| 44 | |
| 45 | fn test_custom_combined_transcoder_both_frontends_transfer_alias_and_keep_independent_text_io() { |
| 46 | for alt in [false, true] { |
| 47 | mut vm := new() |
| 48 | binary, p := ctc_parent_setup(mut vm, 'λRX'.bytes(), .utf8) |
| 49 | assert binary.input_pointer().closed && binary.output_pointer().closed |
| 50 | assert !p.input_pointer().closed && !p.output_pointer().closed |
| 51 | assert vm.lookup('ct-rx') or { panic(err.msg()) }.whole() == 0 |
| 52 | if _ := vm.read_u8(binary) { |
| 53 | assert false |
| 54 | } |
| 55 | assert vm.peek_char(p) or { panic(err.msg()) }.whole() == `λ` |
| 56 | if alt { |
| 57 | vm.eval_alt('put-string(ct-port, "😀")\nclose-output-port(ct-port)') or { panic(err.msg()) } |
| 58 | } else { |
| 59 | ctc_parent_eval(mut vm, '(put-string ct-port "😀") (close-output-port ct-port)') |
| 60 | } |
| 61 | assert vm.read_char(p) or { panic(err.msg()) }.whole() == `λ` |
| 62 | assert vm.read_string(p, 2) or { panic(err.msg()) }.text() == 'RX' |
| 63 | assert ctc_parent_bytes(vm) == '😀'.bytes() |
| 64 | assert vm.lookup('ct-closes') or { panic(err.msg()) }.whole() == 0 |
| 65 | vm.close_port(p) or { panic(err.msg()) } |
| 66 | assert vm.lookup('ct-closes') or { panic(err.msg()) }.whole() == 1 |
| 67 | assert write(vm.lookup('ct-seen') or { panic(err.msg()) }) or { panic(err.msg()) } == '(#f #f)' |
| 68 | vm.close_port(binary) or { panic(err.msg()) } |
| 69 | vm.close_port(p) or { panic(err.msg()) } |
| 70 | assert vm.lookup('ct-closes') or { panic(err.msg()) }.whole() == 1 |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fn test_custom_combined_transcoder_last_input_close_drains_failed_tx_on_same_machine() { |
| 75 | for host_close in [false, true] { |
| 76 | mut vm := new() |
| 77 | _, p := ctc_parent_setup(mut vm, [u8(0xff), 0xfe, 65, 0, 66, 0], .utf16) |
| 78 | ctc_parent_eval(mut vm, '(set! ct-fail #t)') |
| 79 | if _ := vm.eval('(put-string ct-port "Ω")') { |
| 80 | assert false |
| 81 | } |
| 82 | assert ctc_parent_bytes(vm) == [u8(3)] |
| 83 | if _ := vm.eval('(close-output-port ct-port)') { |
| 84 | assert false |
| 85 | } |
| 86 | assert p.output_pointer().closed && !p.input_pointer().closed |
| 87 | assert vm.lookup('ct-writes') or { panic(err.msg()) }.whole() == 3 |
| 88 | assert vm.read_char(p) or { panic(err.msg()) }.whole() == `A` |
| 89 | assert vm.lookup('ct-writes') or { panic(err.msg()) }.whole() == 3 |
| 90 | ctc_parent_eval(mut vm, '(set! ct-fail #f)') |
| 91 | if host_close { |
| 92 | vm.close_input_port(p) or { panic(err.msg()) } |
| 93 | } else { |
| 94 | ctc_parent_eval(mut vm, '(close-input-port ct-port)') |
| 95 | } |
| 96 | assert ctc_parent_bytes(vm) == [u8(3), 0xa9] |
| 97 | assert vm.lookup('ct-closes') or { panic(err.msg()) }.whole() == 1 |
| 98 | assert write(vm.lookup('ct-seen') or { panic(err.msg()) }) or { panic(err.msg()) } == '(#f #f)' |
| 99 | assert ctc_parent_eval(mut vm, '(let loop ((xs ct-depths)) (or (null? xs) (and (= (car xs) 1) (loop (cdr xs)))))').is_true() |
| 100 | assert vm.active.len == 0 |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | fn test_custom_combined_transcoder_full_close_preserves_flush_error_and_claims_closer_once() { |
| 105 | mut vm := new() |
| 106 | _, p := ctc_parent_setup(mut vm, 'R'.bytes(), .utf8) |
| 107 | ctc_parent_eval(mut vm, '(set! ct-fail #t)') |
| 108 | if _ := vm.eval('(put-string ct-port "AB")') { |
| 109 | assert false |
| 110 | } |
| 111 | ctc_parent_eval(mut vm, '(set! ct-close-fail #t)') |
| 112 | if _ := vm.eval('(close-port ct-port)') { |
| 113 | assert false |
| 114 | } else { |
| 115 | assert err.msg().contains('ctc writer failed'), err.msg() |
| 116 | } |
| 117 | assert p.input_pointer().closed && p.output_pointer().closed |
| 118 | assert vm.lookup('ct-closes') or { panic(err.msg()) }.whole() == 1 |
| 119 | ctc_parent_eval(mut vm, '(close-port ct-port) (close-input-port ct-port) (close-output-port ct-port)') |
| 120 | assert vm.lookup('ct-closes') or { panic(err.msg()) }.whole() == 1 |
| 121 | assert vm.active.len == 0 |
| 122 | } |
| 123 | |
| 124 | fn test_custom_combined_transcoder_input_halfclose_releases_rx_storage_and_keeps_output() { |
| 125 | mut vm := new() |
| 126 | _, p := ctc_parent_setup(mut vm, 'λR'.bytes(), .utf8) |
| 127 | resource := custom_port_resource(p) |
| 128 | assert vm.peek_char(p) or { panic(err.msg()) }.whole() == `λ` |
| 129 | assert resource.input_adapter.decoded.len > 0 |
| 130 | vm.close_input_port(p) or { panic(err.msg()) } |
| 131 | assert resource.input_adapter.decoded.len == 0 && resource.input_adapter.raw.buffer.len == 0 |
| 132 | ctc_parent_eval(mut vm, '(put-string ct-port "λ")') |
| 133 | assert ctc_parent_bytes(vm) == 'λ'.bytes() |
| 134 | assert vm.lookup('ct-closes') or { panic(err.msg()) }.whole() == 0 |
| 135 | vm.close_output_port(p) or { panic(err.msg()) } |
| 136 | assert vm.lookup('ct-closes') or { panic(err.msg()) }.whole() == 1 |
| 137 | } |
| 138 | |
| 139 | fn test_custom_combined_transcoder_invalid_foreign_busy_options_do_not_transfer_resources() { |
| 140 | mut vm := new() |
| 141 | _, p := ctc_parent_setup(mut vm, 'R'.bytes(), .utf8) |
| 142 | resource := custom_port_resource(p) |
| 143 | ctc_parent_eval(mut vm, '(define ct-second (make-custom-binary-input/output-port "second" (lambda (b s n) 0) (lambda (b s n) n) #f #f #f))') |
| 144 | binary := vm.lookup('ct-second') or { panic(err.msg()) } |
| 145 | mut foreign := new() |
| 146 | options := transcoder_value(.utf8, .none, .raise) |
| 147 | for invalid in [boolean(false), integer(0)] { |
| 148 | if _ := vm.transcoded_port(binary, invalid) { |
| 149 | assert false |
| 150 | } |
| 151 | } |
| 152 | if _ := foreign.transcoded_port(binary, options) { |
| 153 | assert false |
| 154 | } |
| 155 | binary.output_pointer().busy = true |
| 156 | if _ := vm.transcoded_port(binary, options) { |
| 157 | assert false |
| 158 | } |
| 159 | binary.output_pointer().busy = false |
| 160 | assert !binary.input_pointer().closed && !binary.output_pointer().closed |
| 161 | assert !resource.closed |
| 162 | vm.close_port(p) or { panic(err.msg()) } |
| 163 | vm.close_port(binary) or { panic(err.msg()) } |
| 164 | } |
| 165 | |
| 166 | fn test_custom_combined_transcoder_cold_caller_arena_closes_before_reset() { |
| 167 | for input_first in [false, true] { |
| 168 | mut region := arena.new() |
| 169 | region.push() |
| 170 | mut vm := new() |
| 171 | _, p := ctc_parent_setup(mut vm, 'λR'.bytes(), .utf8) |
| 172 | assert vm.peek_char(p) or { panic(err.msg()) }.whole() == `λ` |
| 173 | gc_collect() |
| 174 | if input_first { |
| 175 | vm.close_input_port(p) or { panic(err.msg()) } |
| 176 | ctc_parent_eval(mut vm, '(put-string ct-port "X")') |
| 177 | } else { |
| 178 | ctc_parent_eval(mut vm, '(put-string ct-port "X")') |
| 179 | vm.close_output_port(p) or { panic(err.msg()) } |
| 180 | assert vm.read_char(p) or { panic(err.msg()) }.whole() == `λ` |
| 181 | } |
| 182 | vm.close_port(p) or { panic(err.msg()) } |
| 183 | // Remove every global that can retain this region's callbacks/data. |
| 184 | for name in ['ct-source', 'ct-tx', 'ct-port', 'ct-binary', 'ct-depths', 'ct-seen', |
| 185 | 'ct-writer-error', 'ct-closer-error', 'ct-depth'] { |
| 186 | vm.define(name, boolean(false)) or { panic(err.msg()) } |
| 187 | } |
| 188 | assert vm.active.len == 0 |
| 189 | region.pop() |
| 190 | region.free() |
| 191 | } |
| 192 | mut healthy := new() |
| 193 | assert ctc_parent_eval(mut healthy, '(+ 20 22)').whole() == 42 |
| 194 | } |
| 195 | |
| 196 | fn test_custom_combined_transcoder_datum_graph_owns_mutable_values_after_rx_close() { |
| 197 | mut vm := new() |
| 198 | _, p := ctc_parent_setup(mut vm, '(#1="λ" #1# #2=(x . #2#) #u8(7 8))'.bytes(), .utf8) |
| 199 | ctc_parent_eval(mut vm, '(define ct-datum (get-datum ct-port)) (close-input-port ct-port)') |
| 200 | gc_collect() |
| 201 | assert ctc_parent_eval(mut vm, '(and (eq? (car ct-datum) (cadr ct-datum)) |
| 202 | (eq? (caddr ct-datum) (cdr (caddr ct-datum))))').is_true() |
| 203 | ctc_parent_eval(mut vm, '(string-set! (car ct-datum) 0 #\\Z) |
| 204 | (put-string ct-port (cadr ct-datum)) (close-output-port ct-port)') |
| 205 | assert ctc_parent_bytes(vm) == [u8(90)] |
| 206 | assert write(ctc_parent_eval(mut vm, '(list (cadr ct-datum) (list-ref ct-datum 3) ct-closes)')) or { panic(err.msg()) } == '("Z" #u8(7 8) 1)' |
| 207 | assert p.input_pointer().closed && p.output_pointer().closed && vm.active.len == 0 |
| 208 | } |
| 209 | |