| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | fn string_into_text(mut vm VM, source string) string { |
| 6 | return write(vm.eval(source) or { panic(err.msg()) }) or { panic(err.msg()) } |
| 7 | } |
| 8 | |
| 9 | @[heap] |
| 10 | struct StringIntoChunkReader { |
| 11 | bytes []u8 |
| 12 | mut: |
| 13 | position int |
| 14 | calls int |
| 15 | } |
| 16 | |
| 17 | fn (mut source StringIntoChunkReader) read(mut bytes []u8) !int { |
| 18 | source.calls++ |
| 19 | count := int_min(int_min(2, bytes.len), source.bytes.len - source.position) |
| 20 | for i in 0 .. count { bytes[i] = source.bytes[source.position + i] } |
| 21 | source.position += count |
| 22 | return count |
| 23 | } |
| 24 | |
| 25 | fn test_get_string_into_overwrites_character_ranges_without_changing_length_or_aliases() { |
| 26 | mut vm := new() |
| 27 | target := string_value('AλλZ') |
| 28 | snapshot := target.as_string() or { panic(err.msg()) } |
| 29 | port := vm.open_input_string('😀é') or { panic(err.msg()) } |
| 30 | vm.define('target', target) or { panic(err.msg()) } |
| 31 | vm.define('input', port) or { panic(err.msg()) } |
| 32 | assert string_into_text(mut vm, '(define alias target) |
| 33 | (list (get-string-n! input target 1 3) alias (string-length target) |
| 34 | (eq? target alias) (eof-object? (get-string-n! input target 0 1)))') == '(2 "A😀éZ" 4 #t #t)' |
| 35 | assert snapshot == 'AλλZ' |
| 36 | assert target.as_string() or { panic(err.msg()) } == 'A😀éZ' |
| 37 | } |
| 38 | |
| 39 | fn test_get_string_into_zero_eof_invalid_ranges_and_literals_do_not_change_target() { |
| 40 | mut vm := new() |
| 41 | target := string_value('ABCD') |
| 42 | mut reader := &StringIntoChunkReader{ bytes: 'XY'.bytes() } |
| 43 | port := vm.input_port(reader) or { panic(err.msg()) } |
| 44 | assert (vm.read_string_into(port, target, 4, 0) or { panic(err.msg()) }).whole() == 0 |
| 45 | assert reader.calls == 0 |
| 46 | for start, count in { |
| 47 | -1: 1 |
| 48 | 3: 2 |
| 49 | 5: 0 |
| 50 | 0: -1 |
| 51 | } { |
| 52 | if _ := vm.read_string_into(port, target, start, count) { |
| 53 | assert false |
| 54 | } |
| 55 | } |
| 56 | assert reader.calls == 0 |
| 57 | literal := vm.eval('"immutable"') or { panic(err.msg()) } |
| 58 | if _ := vm.read_string_into(port, literal, 0, 1) { |
| 59 | assert false |
| 60 | } |
| 61 | assert reader.calls == 0 |
| 62 | assert target.as_string() or { panic(err.msg()) } == 'ABCD' |
| 63 | assert (vm.read_string_into(port, target, 1, 2) or { panic(err.msg()) }).whole() == 2 |
| 64 | assert target.as_string() or { panic(err.msg()) } == 'AXYD' |
| 65 | assert (vm.read_string_into(port, target, 0, 1) or { panic(err.msg()) }).tag == .eof |
| 66 | assert target.as_string() or { panic(err.msg()) } == 'AXYD' |
| 67 | } |
| 68 | |
| 69 | @[heap] |
| 70 | struct GetPutMutatingReader { |
| 71 | vm &VM |
| 72 | mut: |
| 73 | changed bool |
| 74 | finished bool |
| 75 | } |
| 76 | |
| 77 | fn (mut source GetPutMutatingReader) read(mut bytes []u8) !int { |
| 78 | if source.finished { return 0 } |
| 79 | if !source.changed { |
| 80 | source.changed = true |
| 81 | source.vm.eval('(string-set! target 0 (integer->char 33))')! |
| 82 | gc_collect() |
| 83 | } |
| 84 | if bytes.len < 2 { return failure('test reader needs two bytes') } |
| 85 | bytes[0] = `X` |
| 86 | bytes[1] = `Y` |
| 87 | source.finished = true |
| 88 | return 2 |
| 89 | } |
| 90 | |
| 91 | fn test_get_string_into_reentrant_target_mutation_is_preserved_and_port_unlocks() { |
| 92 | mut vm := new() |
| 93 | target := string_value('ABCD') |
| 94 | vm.define('target', target) or { panic(err.msg()) } |
| 95 | port := vm.input_port(&GetPutMutatingReader{ vm: vm }) or { panic(err.msg()) } |
| 96 | if _ := vm.read_string_into(port, target, 1, 2) { |
| 97 | assert false |
| 98 | } else { |
| 99 | assert err.msg().contains('string target changed during input') |
| 100 | } |
| 101 | assert target.as_string() or { panic(err.msg()) } == '!BCD' |
| 102 | assert !port.input_pointer().busy |
| 103 | assert vm.active.len == 0 |
| 104 | assert (vm.read_char(port) or { panic(err.msg()) }).tag == .eof |
| 105 | } |
| 106 | |
| 107 | fn test_get_string_into_decoding_failure_does_not_publish_an_incomplete_replacement() { |
| 108 | mut vm := new() |
| 109 | target := string_value('ABCD') |
| 110 | port := vm.input_port(&StringIntoChunkReader{ bytes: [u8(88), 255] }) or { panic(err.msg()) } |
| 111 | if _ := vm.read_string_into(port, target, 1, 2) { |
| 112 | assert false |
| 113 | } else { |
| 114 | assert err.msg().contains('UTF-8') |
| 115 | } |
| 116 | assert target.as_string() or { panic(err.msg()) } == 'ABCD' |
| 117 | assert !port.input_pointer().busy |
| 118 | } |
| 119 | |
| 120 | fn test_get_string_into_allocates_one_final_utf8_buffer_without_an_intermediate_string() { |
| 121 | mut region := arena.new() |
| 122 | region.push() |
| 123 | defer { |
| 124 | region.pop() |
| 125 | region.free() |
| 126 | } |
| 127 | mut vm := new() |
| 128 | target := string_value('a'.repeat(1024)) |
| 129 | port := vm.open_input_string('x'.repeat(128)) or { panic(err.msg()) } |
| 130 | before := region.used() |
| 131 | assert (vm.read_string_into(port, target, 512, 128) or { panic(err.msg()) }).whole() == 128 |
| 132 | allocated := region.used() - before |
| 133 | assert allocated >= 1024 && allocated <= 1280, 'allocated ${allocated} bytes' |
| 134 | assert target.as_string() or { panic(err.msg()) } == 'a'.repeat(512) + 'x'.repeat(128) + 'a'.repeat(384) |
| 135 | } |
| 136 | |