| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | fn custom_input_test_eval(mut vm VM, source string) string { |
| 6 | return write(vm.eval(source) or { panic(err.msg()) }) or { panic(err.msg()) } |
| 7 | } |
| 8 | |
| 9 | fn custom_input_test_source(mut vm VM) { |
| 10 | vm.eval('(define calls 0) |
| 11 | (define (make-source bytes stride) |
| 12 | (let ((index 0)) |
| 13 | (make-custom-binary-input-port "source" |
| 14 | (lambda (b start requested) |
| 15 | (set! calls (+ calls 1)) |
| 16 | (let ((count (min requested stride (max 0 (- (bytevector-length bytes) index))))) |
| 17 | (do ((i 0 (+ i 1))) ((= i count)) |
| 18 | (bytevector-u8-set! b (+ start i) (bytevector-u8-ref bytes (+ index i)))) |
| 19 | (set! index (+ index count)) count)) |
| 20 | (lambda () index) (lambda (position) (set! index position) (values 1 2)) #f)))') or { panic(err.msg()) } |
| 21 | } |
| 22 | |
| 23 | fn test_custom_binary_input_scalar_lookahead_and_eof_share_read_ahead_without_consuming_peeks() { |
| 24 | mut vm := new() |
| 25 | custom_input_test_source(mut vm) |
| 26 | assert custom_input_test_eval(mut vm, '(define p (make-source #u8(0 255 7) 3)) |
| 27 | (list (port-eof? p) (lookahead-u8 p) (peek-u8 p) (get-u8 p) |
| 28 | (read-u8 p) (get-u8 p) (port-eof? p) (eof-object? (get-u8 p)) calls)') == '(#f 0 0 0 255 7 #t #t 2)' |
| 29 | } |
| 30 | |
| 31 | fn test_custom_binary_input_bulk_short_reads_zero_count_and_partial_eof_results() { |
| 32 | mut vm := new() |
| 33 | custom_input_test_source(mut vm) |
| 34 | assert custom_input_test_eval(mut vm, '(define p (make-source #u8(0 1 2 3 4) 2)) |
| 35 | (list (get-bytevector-n p 0) calls (read-bytevector 3 p) |
| 36 | (get-bytevector-n p 20) (get-bytevector-n p 0) |
| 37 | (eof-object? (get-bytevector-all p)) calls)') == '(#u8() 0 #u8(0 1 2) #u8(3 4) #u8() #t 4)' |
| 38 | } |
| 39 | |
| 40 | fn test_custom_binary_input_some_returns_first_available_chunk_without_another_read_callback() { |
| 41 | mut vm := new() |
| 42 | custom_input_test_source(mut vm) |
| 43 | assert custom_input_test_eval(mut vm, '(define p (make-source #u8(1 2 3 4 5) 2)) |
| 44 | (list (get-bytevector-some p) calls (get-u8 p) |
| 45 | (get-bytevector-some p) calls (get-bytevector-all p) calls)') == '(#u8(1 2) 1 3 #u8(4) 2 #u8(5) 4)' |
| 46 | } |
| 47 | |
| 48 | fn test_custom_binary_input_into_drains_lookahead_then_fills_final_target_directly() { |
| 49 | mut vm := new() |
| 50 | custom_input_test_source(mut vm) |
| 51 | assert custom_input_test_eval(mut vm, '(define p (make-source #u8(0 1 2 3 4) 2)) |
| 52 | (define target (make-bytevector 6 9)) |
| 53 | (list (peek-u8 p) (get-bytevector-n! p target 1 4) target |
| 54 | (read-bytevector! target p 0 1) target (eof-object? (read-bytevector! target p 0 1)) |
| 55 | (get-bytevector-n! p target 0 0))') == '(0 4 #u8(4 0 1 2 3 9) 1 #u8(4 0 1 2 3 9) #t 0)' |
| 56 | // List elements retain the same mutable target object, hence both show its final contents. |
| 57 | } |
| 58 | |
| 59 | fn test_custom_binary_input_into_preserves_destination_identity_and_absolute_ranges() { |
| 60 | mut vm := new() |
| 61 | assert custom_input_test_eval(mut vm, '(define requests (quote ())) (define target (make-bytevector 5 9)) |
| 62 | (define p (make-custom-binary-input-port "direct" |
| 63 | (lambda (b s n) (set! requests (cons (list (eq? b target) s n) requests)) |
| 64 | (bytevector-u8-set! b s s) 1) #f #f #f)) |
| 65 | (get-bytevector-n! p target 1 3) (list target (reverse requests))') == '(#u8(9 1 2 3 9) ((#t 1 3) (#t 2 2) (#t 3 1)))' |
| 66 | } |
| 67 | |
| 68 | fn test_custom_binary_input_retained_callback_buffer_keeps_shape_and_result_is_independent() { |
| 69 | mut vm := new() |
| 70 | assert custom_input_test_eval(mut vm, '(define retained #f) (define first #t) |
| 71 | (define p (make-custom-binary-input-port "retain" |
| 72 | (lambda (b s n) (set! retained b) |
| 73 | (if first (begin (set! first #f) (bytevector-u8-set! b s 65) |
| 74 | (bytevector-u8-set! b (+ s 1) 66) 2) 0)) #f #f #f)) |
| 75 | (define result (get-bytevector-all p)) (bytevector-u8-set! retained 0 99) |
| 76 | (list result (bytevector-length retained) (port-eof? p))') == '(#u8(65 66) 4096 #t)' |
| 77 | } |
| 78 | |
| 79 | fn test_custom_binary_input_count_errors_release_busy_and_do_not_commit_buffer_or_eof() { |
| 80 | for result in ['-1', '4097', '1.0', '#f', '(values)', '(values 1 2)'] { |
| 81 | mut vm := new() |
| 82 | vm.eval('(define bad #t) (define calls 0) |
| 83 | (define p (make-custom-binary-input-port "counts" |
| 84 | (lambda (b s n) (set! calls (+ calls 1)) |
| 85 | (if bad ${result} (begin (bytevector-u8-set! b s 65) 1))) #f #f #f))') or { panic(err.msg()) } |
| 86 | if _ := vm.eval('(get-u8 p)') { |
| 87 | assert false, result |
| 88 | } |
| 89 | p := vm.lookup('p') or { panic(err.msg()) } |
| 90 | assert !p.input_pointer().busy && !p.input_pointer().ended && !p.input_pointer().closed |
| 91 | assert isnil(custom_input_resource(p).active) |
| 92 | assert custom_input_test_eval(mut vm, '(set! bad #f) (list (get-u8 p) calls)') == '(65 2)' |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | fn test_custom_binary_input_position_subtracts_read_ahead_and_set_discards_it_after_callback() { |
| 97 | mut vm := new() |
| 98 | custom_input_test_source(mut vm) |
| 99 | assert custom_input_test_eval(mut vm, '(define p (make-source #u8(10 20 30 40 50) 3)) |
| 100 | (define first (peek-u8 p)) (define before (port-position p)) |
| 101 | (define byte (get-u8 p)) (define after (port-position p)) |
| 102 | (set-port-position! p 3) |
| 103 | (list first before byte after (get-u8 p) (port-position p) |
| 104 | (port-has-port-position? p) (port-has-set-port-position!? p))') == '(10 0 10 1 40 4 #t #t)' |
| 105 | } |
| 106 | |
| 107 | fn test_custom_binary_input_position_retains_arbitrary_exact_integer_precision() { |
| 108 | mut vm := new() |
| 109 | assert custom_input_test_eval(mut vm, '(define base 1267650600228229401496703205376) (define position base) |
| 110 | (define p (make-custom-binary-input-port "large-position" |
| 111 | (lambda (b s n) (bytevector-u8-set! b s 65) (set! position (+ position 1)) 1) |
| 112 | (lambda () position) (lambda (x) (set! position x)) #f)) |
| 113 | (peek-u8 p) (define before (port-position p)) (get-u8 p) |
| 114 | (define after (port-position p)) (set-port-position! p (+ base 9)) |
| 115 | (list before after (port-position p))') == '(1267650600228229401496703205376 1267650600228229401496703205377 1267650600228229401496703205385)' |
| 116 | } |
| 117 | |
| 118 | fn test_custom_binary_input_huge_requested_count_does_not_reserve_unreceived_bytes() { |
| 119 | mut vm := new() |
| 120 | custom_input_test_source(mut vm) |
| 121 | assert custom_input_test_eval(mut vm, '(define p (make-source #u8(65 66) 1)) |
| 122 | (get-bytevector-n p 9223372036854775807)') == '#u8(65 66)' |
| 123 | } |
| 124 | |
| 125 | fn test_custom_binary_input_omitted_port_uses_dynamic_parameter_after_name_rebinding() { |
| 126 | mut vm := new() |
| 127 | custom_input_test_source(mut vm) |
| 128 | assert custom_input_test_eval(mut vm, '(define p (make-source #u8(1 2 3 4 5) 2)) |
| 129 | (define input-parameter current-input-port) (define current-input-port 0) |
| 130 | (parameterize ((input-parameter p)) |
| 131 | (list (peek-u8) (read-u8) (read-bytevector 2) |
| 132 | (let ((b (make-bytevector 2))) (read-bytevector! b) b)))') == '(1 1 #u8(2 3) #u8(4 5))' |
| 133 | } |
| 134 | |
| 135 | fn test_custom_binary_input_bulk_capture_replay_keeps_completed_prefix_and_fresh_suffix() { |
| 136 | mut vm := new() |
| 137 | assert custom_input_test_eval(mut vm, '(define index 0) (define saved #f) |
| 138 | (define p (make-custom-binary-input-port "capture" |
| 139 | (lambda (b s n) (bytevector-u8-set! b s (+ 65 index)) |
| 140 | (set! index (+ index 1)) |
| 141 | (if (= index 2) (call/cc (lambda (k) (set! saved k) 1)) 1)) #f #f #f)) |
| 142 | (get-bytevector-n p 4)') == '#u8(65 66 67 68)' |
| 143 | // The pending callback buffer is shared physical state. Refill reused it; |
| 144 | // the old pending buffer now contains68. The completed private prefix65 is frozen. |
| 145 | assert custom_input_test_eval(mut vm, '(saved 1)') == '#u8(65 68 69 70)' |
| 146 | assert custom_input_test_eval(mut vm, '(saved 1)') == '#u8(65 70 71 72)' |
| 147 | p := vm.lookup('p') or { panic(err.msg()) } |
| 148 | assert !p.input_pointer().busy && isnil(custom_input_resource(p).active) |
| 149 | } |
| 150 | |
| 151 | fn test_custom_binary_input_into_capture_replay_keeps_private_offset_and_shared_target_mutations() { |
| 152 | mut vm := new() |
| 153 | assert custom_input_test_eval(mut vm, '(define saved #f) (define requests (quote ())) |
| 154 | (define target (make-bytevector 4 0)) |
| 155 | (define p (make-custom-binary-input-port "into-capture" |
| 156 | (lambda (b s n) (set! requests (cons (list s n) requests)) |
| 157 | (bytevector-u8-set! b s (+ 10 s)) |
| 158 | (if (= s 1) (call/cc (lambda (k) (set! saved k) 1)) n)) #f #f #f)) |
| 159 | (get-bytevector-n! p target 1 3) (list target (reverse requests))') == '(#u8(0 11 12 0) ((1 3) (2 2)))' |
| 160 | assert custom_input_test_eval(mut vm, '(saved 2)') == '(#u8(0 11 12 13) ((1 3) (2 2) (3 1)))' |
| 161 | assert custom_input_test_eval(mut vm, '(saved 1)') == '(#u8(0 11 12 13) ((1 3) (2 2) (3 1) (2 2)))' |
| 162 | } |
| 163 | |
| 164 | fn test_custom_binary_input_readiness_never_calls_potentially_blocking_scheme_reader() { |
| 165 | mut vm := new() |
| 166 | custom_input_test_source(mut vm) |
| 167 | vm.eval('(define p (make-source #u8(1) 1))') or { panic(err.msg()) } |
| 168 | if _ := vm.eval('(u8-ready? p)') { |
| 169 | assert false |
| 170 | } |
| 171 | assert custom_input_test_eval(mut vm, 'calls') == '0' |
| 172 | assert custom_input_test_eval(mut vm, '(peek-u8 p) (list (u8-ready? p) (get-u8 p) (port-eof? p) (u8-ready? p))') == '(#t 1 #t #t)' |
| 173 | } |
| 174 | |
| 175 | fn test_custom_binary_input_close_and_controller_preserve_multiple_values_and_captured_close() { |
| 176 | mut vm := new() |
| 177 | assert custom_input_test_eval(mut vm, '(define closes 0) (define close-k #f) |
| 178 | (define p (make-custom-binary-input-port "close" (lambda (b s n) 0) #f #f |
| 179 | (lambda () (set! closes (+ closes 1)) (call/cc (lambda (k) (set! close-k k) (values)))))) |
| 180 | (call-with-values (lambda () (call-with-port p (lambda (q) (values 7 8)))) list)') == '(7 8)' |
| 181 | assert custom_input_test_eval(mut vm, '(close-k 1 2 3)') == '(7 8)' |
| 182 | assert custom_input_test_eval(mut vm, '(close-input-port p) (list closes (input-port-open? p))') == '(1 #f)' |
| 183 | } |
| 184 | |
| 185 | fn test_custom_binary_input_native_embedding_tiny_reads_and_cold_borrowed_arenas() { |
| 186 | mut vm := new() |
| 187 | callback := vm.eval('(lambda (b s n) (bytevector-u8-set! b s 65) 1)') or { panic(err.msg()) } |
| 188 | for size in [1, 2, 3, 4096] { |
| 189 | mut region := arena.new() |
| 190 | region.push() |
| 191 | p := vm.make_custom_binary_input_port('host', callback, boolean(false), boolean(false), |
| 192 | boolean(false), |
| 193 | read_size: size |
| 194 | ) or { panic(err.msg()) } |
| 195 | assert (vm.peek_u8(p) or { panic(err.msg()) }).whole() == 65 |
| 196 | assert (vm.read_u8(p) or { panic(err.msg()) }).whole() == 65 |
| 197 | assert (vm.read_bytevector(p, 4) or { panic(err.msg()) }).bytevector_pointer().bytes == [ |
| 198 | u8(65), |
| 199 | 65, |
| 200 | 65, |
| 201 | 65, |
| 202 | ] |
| 203 | target := bytevector([]u8{len: 3}) |
| 204 | assert (vm.read_bytevector_into(p, target, 0, 3) or { panic(err.msg()) }).whole() == 3 |
| 205 | vm.close_input_port(p) or { panic(err.msg()) } |
| 206 | region.pop() |
| 207 | region.free() |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | fn custom_input_test_ack_one(mut _vm VM, args []Value) !Value { |
| 212 | mut data := args[0].bytevector_pointer() |
| 213 | data.bytes[int(args[1].whole())] = 65 |
| 214 | return integer(1) |
| 215 | } |
| 216 | |
| 217 | fn custom_input_test_into_allocation(count int) int { |
| 218 | mut vm := new() |
| 219 | vm.register('read-one', custom_input_test_ack_one) or { panic(err.msg()) } |
| 220 | callback := vm.lookup('read-one') or { panic(err.msg()) } |
| 221 | reader := vm.lookup('get-bytevector-n!') or { panic(err.msg()) } |
| 222 | mut region := arena.new() |
| 223 | region.push() |
| 224 | p := vm.make_custom_binary_input_port('allocation', callback, boolean(false), boolean(false), boolean(false)) or { panic(err.msg()) } |
| 225 | target := bytevector([]u8{len: count}) |
| 226 | args := [p, target, integer(0), integer(count)] |
| 227 | before := region.used() |
| 228 | assert (vm.call(reader, args) or { panic(err.msg()) }).whole() == count |
| 229 | used := region.used() - before |
| 230 | assert target.bytevector_pointer().bytes[0] == 65 && target.bytevector_pointer().bytes[count - 1] == 65 |
| 231 | vm.close_input_port(p) or { panic(err.msg()) } |
| 232 | region.pop() |
| 233 | region.free() |
| 234 | return used |
| 235 | } |
| 236 | |
| 237 | fn test_custom_binary_input_direct_partial_reads_allocate_no_per_callback_storage() { |
| 238 | assert custom_input_test_into_allocation(32) == custom_input_test_into_allocation(20000) |
| 239 | } |
| 240 | |
| 241 | fn custom_input_test_capture_allocation(count int) int { |
| 242 | mut vm := new() |
| 243 | callback := vm.eval('(lambda (b s n) (bytevector-u8-set! b s 65) (call/cc (lambda (k) 1)))') or { panic(err.msg()) } |
| 244 | reader := vm.lookup('get-bytevector-n') or { panic(err.msg()) } |
| 245 | mut region := arena.new() |
| 246 | region.push() |
| 247 | p := vm.make_custom_binary_input_port('capture-allocation', callback, boolean(false), |
| 248 | boolean(false), boolean(false), |
| 249 | read_size: 1 |
| 250 | ) or { panic(err.msg()) } |
| 251 | before := region.used() |
| 252 | result := vm.call(reader, [p, integer(count)]) or { panic(err.msg()) } |
| 253 | used := region.used() - before |
| 254 | assert result.bytevector_pointer().bytes.len == count |
| 255 | for byte in result.bytevector_pointer().bytes { |
| 256 | assert byte == 65 |
| 257 | } |
| 258 | vm.close_input_port(p) or { panic(err.msg()) } |
| 259 | region.pop() |
| 260 | region.free() |
| 261 | return used |
| 262 | } |
| 263 | |
| 264 | fn test_custom_binary_input_repeated_capture_accumulates_in_linear_space() { |
| 265 | small := custom_input_test_capture_allocation(512) |
| 266 | large := custom_input_test_capture_allocation(2048) |
| 267 | assert large < small * 6, 'capture accumulation grew from ${small} to ${large} bytes' |
| 268 | } |
| 269 | |
| 270 | fn test_custom_binary_input_bulk_refills_allocate_only_bounded_output_storage() { |
| 271 | mut vm := new() |
| 272 | vm.register('read-one', custom_input_test_ack_one) or { panic(err.msg()) } |
| 273 | callback := vm.lookup('read-one') or { panic(err.msg()) } |
| 274 | reader := vm.lookup('get-bytevector-n') or { panic(err.msg()) } |
| 275 | mut region := arena.new() |
| 276 | region.push() |
| 277 | p := vm.make_custom_binary_input_port('bulk-allocation', callback, boolean(false), boolean(false), boolean(false)) or { panic(err.msg()) } |
| 278 | count := 20000 |
| 279 | before := region.used() |
| 280 | result := vm.call(reader, [p, integer(count)]) or { panic(err.msg()) } |
| 281 | used := region.used() - before |
| 282 | assert result.bytevector_pointer().bytes.len == count |
| 283 | for byte in result.bytevector_pointer().bytes { |
| 284 | assert byte == 65 |
| 285 | } |
| 286 | // Allow geometric output growth and setup; even a small allocation per |
| 287 | // callback would exceed this bound. The read-ahead object is reused. |
| 288 | assert used < count * 8 + 32768, 'bulk input allocated ${used} bytes' |
| 289 | vm.close_input_port(p) or { panic(err.msg()) } |
| 290 | region.pop() |
| 291 | region.free() |
| 292 | } |
| 293 | |