| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | fn ctext_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 ctext_test_source(mut vm VM) { |
| 10 | vm.eval('(define calls 0) (define last-buffer #f) |
| 11 | (define (text-source text stride) |
| 12 | (let ((index 0)) |
| 13 | (make-custom-textual-input-port "text" |
| 14 | (lambda (b s n) (set! calls (+ calls 1)) (set! last-buffer b) |
| 15 | (let ((count (min n stride (- (string-length text) index)))) |
| 16 | (string-copy! b s text index (+ index count)) |
| 17 | (set! index (+ index count)) count)) |
| 18 | (lambda () (list index)) |
| 19 | (lambda (token) (set! index (car token)) (values 1 2)) #f)))') or { panic(err.msg()) } |
| 20 | } |
| 21 | |
| 22 | fn test_custom_text_characters_and_counted_strings_handle_utf8_width_changes() { |
| 23 | mut vm := new() |
| 24 | ctext_test_source(mut vm) |
| 25 | assert ctext_test_eval(mut vm, '(define p (text-source "λ😀AB" 2)) |
| 26 | (list (char->integer (peek-char p)) (char->integer (get-char p)) |
| 27 | (read-string 2 p) (get-string-n p 20) (get-string-n p 0) |
| 28 | (eof-object? (get-char p)) calls (string-length last-buffer))') == '(955 955 "😀A" "B" "" #t 3 4096)' |
| 29 | } |
| 30 | |
| 31 | fn test_custom_text_get_line_is_lf_only_and_read_line_preserves_existing_unicode_endings() { |
| 32 | mut vm := new() |
| 33 | ctext_test_source(mut vm) |
| 34 | assert ctext_test_eval(mut vm, '(define p (text-source "a\\r\\nb\\r\\x85;c\\x2028;d\\x85;e" 1)) |
| 35 | (list (read-line p) (read-line p) (read-line p) (read-line p) (read-line p) (eof-object? (read-line p)))') == '("a" "b" "c" "d" "e" #t)' |
| 36 | assert ctext_test_eval(mut vm, '(define q (text-source "a\\r\\nb\\x85;c\\n" 1)) |
| 37 | (list (get-line q) (get-line q) (eof-object? (get-line q)))') == '("a\\r" "b\\x85;c" #t)' |
| 38 | } |
| 39 | |
| 40 | fn test_custom_text_empty_lines_crlf_boundary_and_zero_count_do_not_become_eof() { |
| 41 | mut vm := new() |
| 42 | ctext_test_source(mut vm) |
| 43 | assert ctext_test_eval(mut vm, '(define p (text-source "\\r\\n\\rX\\n\\n" 1)) |
| 44 | (list (get-string-n p 0) calls (read-line p) (read-line p) |
| 45 | (read-line p) (read-line p) (eof-object? (read-line p)))') == '("" 0 "" "" "X" "" #t)' |
| 46 | } |
| 47 | |
| 48 | fn test_custom_text_into_replaces_character_ranges_and_preserves_alias_identity() { |
| 49 | mut vm := new() |
| 50 | ctext_test_source(mut vm) |
| 51 | assert ctext_test_eval(mut vm, '(define target (string-copy "A😀BλZ")) (define alias target) |
| 52 | (define p (text-source "éΩ" 1)) |
| 53 | (list (get-string-n! p target 1 2) (eq? target alias) target (string-length target) |
| 54 | (get-string-n! p target 0 0) (eof-object? (get-string-n! p target 0 1)))') == '(2 #t "AéΩλZ" 5 0 #t)' |
| 55 | } |
| 56 | |
| 57 | fn test_custom_text_into_detects_callback_mutation_before_publishing_replacement() { |
| 58 | mut vm := new() |
| 59 | vm.eval('(define target (string-copy "abc")) |
| 60 | (define p (make-custom-textual-input-port "mutation" |
| 61 | (lambda (b s n) (string-set! target 0 #\\X) (string-set! b s #\\λ) 1) #f #f #f))') or { panic(err.msg()) } |
| 62 | if _ := vm.eval('(get-string-n! p target 1 1)') { |
| 63 | assert false |
| 64 | } else { |
| 65 | assert err.msg().contains('target changed') |
| 66 | } |
| 67 | assert ctext_test_eval(mut vm, 'target') == '"Xbc"' |
| 68 | p := vm.lookup('p') or { panic(err.msg()) } |
| 69 | assert !p.input_pointer().busy && isnil(custom_input_resource(p).active) |
| 70 | } |
| 71 | |
| 72 | fn test_custom_text_positions_restore_unread_snapshot_and_arbitrary_source_tokens() { |
| 73 | mut vm := new() |
| 74 | ctext_test_source(mut vm) |
| 75 | assert ctext_test_eval(mut vm, '(define p (text-source "λ😀ABC" 3)) |
| 76 | (get-char p) (define saved (port-position p)) |
| 77 | (get-string-n p 3) (string-set! last-buffer 1 #\\Z) |
| 78 | (set-port-position! p saved) (define a (get-string-n p 3)) |
| 79 | (set-port-position! p saved) (list a (get-string-n p 3) (port-has-port-position? p))') == '("😀AB" "😀AB" #t)' |
| 80 | assert ctext_test_eval(mut vm, '(define q (text-source "other" 2)) (port-position q)') == '#<port-position>' |
| 81 | if _ := vm.eval('(set-port-position! q saved)') { |
| 82 | assert false |
| 83 | } |
| 84 | if _ := vm.eval('(set-port-position! p (quote (1)))') { |
| 85 | assert false |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | fn test_custom_text_position_false_token_is_valid_and_setter_receives_same_object() { |
| 90 | mut vm := new() |
| 91 | assert ctext_test_eval(mut vm, '(define seen #t) |
| 92 | (define p (make-custom-textual-input-port "false-token" (lambda (b s n) 0) |
| 93 | (lambda () #f) (lambda (x) (set! seen x) (values)) #f)) |
| 94 | (define token (port-position p)) (set-port-position! p token) |
| 95 | (list seen (eq? token token) (port-eof? p))') == '(#f #t #t)' |
| 96 | } |
| 97 | |
| 98 | fn test_custom_text_output_counts_characters_and_keeps_mutable_source_identity() { |
| 99 | mut vm := new() |
| 100 | assert ctext_test_eval(mut vm, '(define requests (quote ())) (define source (string-copy "Aλ😀Z")) |
| 101 | (define sink (open-output-string)) |
| 102 | (define p (make-custom-textual-output-port "out" |
| 103 | (lambda (b s n) (set! requests (cons (list (eq? b source) s n) requests)) |
| 104 | (write-char (string-ref b s) sink) 1) #f #f #f)) |
| 105 | (put-string p source 1 2) (write-string source p 3 4) |
| 106 | (list (get-output-string sink) (reverse requests))') == '("λ😀Z" ((#t 1 2) (#t 2 1) (#t 3 1)))' |
| 107 | } |
| 108 | |
| 109 | fn test_custom_text_output_all_printers_and_character_newline_operations_share_driver() { |
| 110 | mut vm := new() |
| 111 | assert ctext_test_eval(mut vm, '(define sink (open-output-string)) |
| 112 | (define p (make-custom-textual-output-port "printer" |
| 113 | (lambda (b s n) (put-string sink b s n) n) #f #f #f)) |
| 114 | (display "λ" p) (write-char #\\space p) (put-char p #\\😀) |
| 115 | (write (quote (1 2)) p) (write-shared (vector 3) p) (write-simple #t p) |
| 116 | (put-datum p (quote x)) (newline p) (get-output-string sink)') == '"λ 😀(1 2)#(3)#tx\\n"' |
| 117 | } |
| 118 | |
| 119 | fn test_custom_text_output_positions_keep_arbitrary_getter_value_identity_and_reject_foreign_tokens() { |
| 120 | mut vm := new() |
| 121 | assert ctext_test_eval(mut vm, '(define raw (list 7)) (define seen #f) |
| 122 | (define p (make-custom-textual-output-port "position" (lambda (b s n) n) |
| 123 | (lambda () raw) (lambda (x) (set! seen (eq? x raw)) (values 1 2)) #f)) |
| 124 | (define token (port-position p)) (set-port-position! p token) (list seen (port-transcoder p))') == '(#t #f)' |
| 125 | if _ := vm.eval('(set-port-position! p 7)') { |
| 126 | assert false |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | fn test_custom_text_callbacks_use_dynamic_current_ports_after_parameter_name_rebinding() { |
| 131 | mut vm := new() |
| 132 | ctext_test_source(mut vm) |
| 133 | assert ctext_test_eval(mut vm, '(define input-param current-input-port) (define output-param current-output-port) |
| 134 | (define current-input-port 0) (define current-output-port 0) |
| 135 | (define sink (open-output-string)) |
| 136 | (define out (make-custom-textual-output-port "dynamic" (lambda (b s n) (put-string sink b s n) n) #f #f #f)) |
| 137 | (parameterize ((input-param (text-source "λ😀" 1)) (output-param out)) |
| 138 | (write-char (read-char)) (display (read-string 1)) (newline)) |
| 139 | (get-output-string sink)') == '"λ😀\\n"' |
| 140 | } |
| 141 | |
| 142 | fn test_custom_text_output_multishot_capture_keeps_private_character_offsets() { |
| 143 | mut vm := new() |
| 144 | assert ctext_test_eval(mut vm, '(define saved #f) (define requests (quote ())) |
| 145 | (define p (make-custom-textual-output-port "capture" |
| 146 | (lambda (b s n) (set! requests (cons (list s n) requests)) |
| 147 | (if (= s 0) (call/cc (lambda (k) (set! saved k) 1)) n)) #f #f #f)) |
| 148 | (put-string p "λ😀Z") (reverse requests)') == '((0 3) (1 2))' |
| 149 | assert ctext_test_eval(mut vm, '(saved 2)') == '((0 3) (1 2) (2 1))' |
| 150 | } |
| 151 | |
| 152 | fn test_custom_text_input_capture_keeps_utf8_prefix_chunks_and_shared_pending_buffer() { |
| 153 | mut vm := new() |
| 154 | assert ctext_test_eval(mut vm, '(define index 0) (define saved #f) |
| 155 | (define p (make-custom-textual-input-port "capture" |
| 156 | (lambda (b s n) (string-set! b s (if (= index 0) #\\λ (if (= index 1) #\\😀 #\\Z))) |
| 157 | (set! index (+ index 1)) |
| 158 | (if (= index 2) (call/cc (lambda (k) (set! saved k) 1)) 1)) #f #f #f)) |
| 159 | (get-string-n p 3)') == '"λ😀Z"' |
| 160 | assert ctext_test_eval(mut vm, '(saved 1)') == '"λZZ"' |
| 161 | } |
| 162 | |
| 163 | fn test_custom_text_input_positions_restore_byte_locations_after_reused_buffer_mutation() { |
| 164 | mut vm := new() |
| 165 | ctext_test_source(mut vm) |
| 166 | vm.eval('(define p (text-source "λ\\n😀X" 1)) (get-char p) (get-char p) |
| 167 | (define saved (port-position p)) (get-string-all p) (set-port-position! p saved)') or { panic(err.msg()) } |
| 168 | p := vm.lookup('p') or { panic(err.msg()) } |
| 169 | point := p.input_pointer().input_location(p.input_pointer().position) |
| 170 | assert point.offset == 3 && point.line == 2 && point.column == 1 |
| 171 | assert ctext_test_eval(mut vm, '(get-string-all p)') == '"😀X"' |
| 172 | } |
| 173 | |
| 174 | fn test_custom_text_invalid_callback_counts_release_lease_and_allow_retry() { |
| 175 | for result in ['-1', '(+ n 1)', '1.0', '(values)', '(values 1 2)'] { |
| 176 | mut vm := new() |
| 177 | vm.eval('(define bad #t) (define p (make-custom-textual-input-port "bad" |
| 178 | (lambda (b s n) (if bad ${result} (begin (string-set! b s #\\λ) 1))) #f #f #f))') or { panic(err.msg()) } |
| 179 | if _ := vm.eval('(get-char p)') { |
| 180 | assert false |
| 181 | } |
| 182 | assert ctext_test_eval(mut vm, '(set! bad #f) (char->integer (get-char p))') == '955' |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | fn test_custom_text_zero_count_and_huge_count_do_not_allocate_requested_storage() { |
| 187 | mut vm := new() |
| 188 | ctext_test_source(mut vm) |
| 189 | assert ctext_test_eval(mut vm, '(define p (text-source "λ😀" 1)) |
| 190 | (list (get-string-n p 0) calls (get-string-n p 9223372036854775807) |
| 191 | (eof-object? (get-string-all p)))') == '("" 0 "λ😀" #t)' |
| 192 | } |
| 193 | |
| 194 | fn test_custom_text_native_host_entries_and_borrowed_arena_bridges_do_not_cache_storage() { |
| 195 | mut vm := new() |
| 196 | reader := vm.eval('(lambda (b s n) (string-set! b s #\\λ) 1)') or { panic(err.msg()) } |
| 197 | writer := vm.eval('(lambda (b s n) n)') or { panic(err.msg()) } |
| 198 | for size in [1, 3, 20] { |
| 199 | mut region := arena.new() |
| 200 | region.push() |
| 201 | p := vm.make_custom_textual_input_port('host', reader, boolean(false), boolean(false), |
| 202 | boolean(false), |
| 203 | read_size: size |
| 204 | ) or { panic(err.msg()) } |
| 205 | q := vm.make_custom_textual_output_port('host', writer, boolean(false), boolean(false), boolean(false)) or { panic(err.msg()) } |
| 206 | assert (vm.read_char(p) or { panic(err.msg()) }).whole() == 955 |
| 207 | assert (vm.read_string(p, 2) or { panic(err.msg()) }).text() == 'λλ' |
| 208 | target := string_value('abc') |
| 209 | assert (vm.read_string_into(p, target, 1, 1) or { panic(err.msg()) }).whole() == 1 |
| 210 | assert target.text() == 'aλc' |
| 211 | vm.close_input_port(p) or { panic(err.msg()) } |
| 212 | vm.close_output_port(q) or { panic(err.msg()) } |
| 213 | region.pop() |
| 214 | region.free() |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | fn test_custom_text_retained_buffer_replacement_is_live_for_unread_characters() { |
| 219 | mut vm := new() |
| 220 | ctext_test_source(mut vm) |
| 221 | assert ctext_test_eval(mut vm, '(define p (text-source "λAB" 3)) |
| 222 | (peek-char p) (string-set! last-buffer 0 #\\😀) |
| 223 | (get-string-n p 3)') == '"😀AB"' |
| 224 | } |
| 225 | |
| 226 | fn test_custom_text_consumed_prefix_mutation_does_not_rewrite_location_history() { |
| 227 | mut vm := new() |
| 228 | ctext_test_source(mut vm) |
| 229 | vm.eval('(define p (text-source "λ\\n😀X" 4)) |
| 230 | (get-char p) (get-char p) |
| 231 | (string-set! last-buffer 0 #\\A) (string-set! last-buffer 1 #\\B) |
| 232 | (string-set! last-buffer 2 #\\é)') or { panic(err.msg()) } |
| 233 | assert ctext_test_eval(mut vm, '(char->integer (get-char p))') == '233' |
| 234 | p := vm.lookup('p') or { panic(err.msg()) } |
| 235 | point := p.input_pointer().input_location(p.input_pointer().position) |
| 236 | assert point.offset == 5 && point.line == 2 && point.column == 3 |
| 237 | } |
| 238 | |
| 239 | fn ctext_test_native_one(mut _vm VM, args []Value) !Value { |
| 240 | mut data := args[0].string_pointer() |
| 241 | overwrite_owned_byte(data, int(args[1].whole()), 65) |
| 242 | data.revision++ |
| 243 | return integer(1) |
| 244 | } |
| 245 | |
| 246 | fn test_custom_text_bulk_callback_storage_is_bounded_by_received_output() { |
| 247 | mut vm := new() |
| 248 | vm.register('text-one', ctext_test_native_one) or { panic(err.msg()) } |
| 249 | callback := vm.lookup('text-one') or { panic(err.msg()) } |
| 250 | reader := vm.lookup('get-string-n') or { panic(err.msg()) } |
| 251 | mut region := arena.new() |
| 252 | region.push() |
| 253 | p := vm.make_custom_textual_input_port('allocation', callback, boolean(false), boolean(false), boolean(false)) or { panic(err.msg()) } |
| 254 | count := 20000 |
| 255 | before := region.used() |
| 256 | result := vm.call(reader, [p, integer(count)]) or { panic(err.msg()) } |
| 257 | used := region.used() - before |
| 258 | assert result.text() == 'A'.repeat(count) |
| 259 | assert used < count * 12 + 32768, 'text callbacks allocated ${used} bytes' |
| 260 | vm.close_input_port(p) or { panic(err.msg()) } |
| 261 | region.pop() |
| 262 | region.free() |
| 263 | } |
| 264 | |
| 265 | fn test_custom_text_into_callbacks_do_not_allocate_per_character() { |
| 266 | mut vm := new() |
| 267 | vm.register('text-one', ctext_test_native_one) or { panic(err.msg()) } |
| 268 | callback := vm.lookup('text-one') or { panic(err.msg()) } |
| 269 | mut region := arena.new() |
| 270 | region.push() |
| 271 | p := vm.make_custom_textual_input_port('into-allocation', callback, boolean(false), boolean(false), boolean(false)) or { panic(err.msg()) } |
| 272 | count := 20000 |
| 273 | target := string_value('x'.repeat(count)) |
| 274 | before := region.used() |
| 275 | result := vm.read_string_into(p, target, 0, count) or { panic(err.msg()) } |
| 276 | used := region.used() - before |
| 277 | assert result.whole() == count && target.text() == 'A'.repeat(count) |
| 278 | assert used < count * 12 + 32768, 'text into callbacks allocated ${used} bytes' |
| 279 | vm.close_input_port(p) or { panic(err.msg()) } |
| 280 | region.pop() |
| 281 | region.free() |
| 282 | } |
| 283 | |