| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import io |
| 5 | |
| 6 | @[heap] |
| 7 | struct RegionDatumReader { |
| 8 | text string |
| 9 | mut: |
| 10 | position int |
| 11 | } |
| 12 | |
| 13 | fn (mut source RegionDatumReader) read(mut destination []u8) !int { |
| 14 | if source.position == source.text.len { return io.Eof{} } |
| 15 | count := int_min(destination.len, int_min(7, source.text.len - source.position)) |
| 16 | unsafe { vmemcpy(destination.data, source.text.str + source.position, count) } |
| 17 | source.position += count |
| 18 | return count |
| 19 | } |
| 20 | |
| 21 | fn read_region_graph(region &arena.Arena, mut snapshot []u8) ! { |
| 22 | region.push() |
| 23 | defer { region.pop() } |
| 24 | mut vm := new() |
| 25 | mut source := &RegionDatumReader{ text: '#!no-fold-case Name #|' + ('trivia\r\n').repeat(200) + '|# (#1="a\\x0;b" #1# #2=#u8(7 8) #2# #3=(x . #3#))' } |
| 26 | input := vm.input_port(source, read_size: 11)! |
| 27 | assert vm.read(input)!.symbol_name()! == 'Name' |
| 28 | data := vm.read(input)! |
| 29 | mut port := input.input_pointer() |
| 30 | assert !isnil(port.location) && region.owns(port.location) |
| 31 | assert region.owns(data.pair_pointer()) |
| 32 | first := data.pair_pointer().car |
| 33 | assert first.pointer() == data.pair_pointer().cdr.pair_pointer().car.pointer() |
| 34 | assert region.owns(first.pointer()) |
| 35 | vm.define('data', data)! |
| 36 | vm.eval('(string-set! (car data) 0 #\\z) (bytevector-u8-set! (caddr data) 1 9)')! |
| 37 | text := first.as_string()! |
| 38 | assert text == 'z\x00b' |
| 39 | unsafe { vmemcpy(snapshot.data, text.str, text.len) } |
| 40 | assert vm.read(input)!.tag == .eof |
| 41 | vm.close_input_port(input)! |
| 42 | } |
| 43 | |
| 44 | fn test_region_owned_extended_port_results_and_location_metadata_do_not_escape_after_reset() { |
| 45 | mut snapshot := []u8{len: 3} |
| 46 | mut region := arena.new() |
| 47 | for _ in 0 .. 8 { |
| 48 | read_region_graph(®ion, mut snapshot) or { panic(err) } |
| 49 | assert !region.is_current() |
| 50 | region.reset() |
| 51 | assert snapshot == [u8(`z`), 0, `b`] |
| 52 | } |
| 53 | region.free() |
| 54 | gc_collect() |
| 55 | assert snapshot == [u8(`z`), 0, `b`] |
| 56 | } |
| 57 | |
| 58 | fn test_prepared_plain_port_reads_do_not_allocate_new_scanner_or_location_objects() { |
| 59 | mut region := arena.new() |
| 60 | region.push() |
| 61 | mut vm := new() |
| 62 | input := vm.open_input_string('123 '.repeat(1000)) or { panic(err) } |
| 63 | before := region.used() |
| 64 | for _ in 0 .. 1000 { |
| 65 | assert vm.read(input) or { panic(err) }.whole() == 123 |
| 66 | } |
| 67 | assert region.used() == before |
| 68 | assert isnil(input.input_pointer().location) |
| 69 | region.pop() |
| 70 | region.free() |
| 71 | } |
| 72 | |
| 73 | fn test_large_line_and_block_trivia_stay_bounded_and_discard_packets_release_their_high_water_buffer() { |
| 74 | mut vm := new() |
| 75 | for text in ['#|' + ('ignored #| nested |# λ\r\n').repeat(30000) + '|#42 ', |
| 76 | ';' + ('ignored λ').repeat(30000) + '\u202842 '] { |
| 77 | mut source := &RegionDatumReader{ text: text } |
| 78 | input := vm.input_port(source, read_size: 31) or { panic(err) } |
| 79 | assert vm.read(input) or { panic(err) }.whole() == 42 |
| 80 | assert input.input_pointer().buffer.cap < 4096 |
| 81 | } |
| 82 | // #; intentionally pins one packet for canonical structural/label checking. |
| 83 | // The allocation is O(packet bytes), then released before the returned data. |
| 84 | mut source := &RegionDatumReader{ text: '#;(' + ('x ').repeat(100000) + ') 42 ' } |
| 85 | input := vm.input_port(source, read_size: 31) or { panic(err) } |
| 86 | assert vm.read(input) or { panic(err) }.whole() == 42 |
| 87 | assert input.input_pointer().buffer.cap <= 65536 |
| 88 | // Consecutive comments count ignored datums rather than nesting frames. |
| 89 | large := vm.open_input_string('#;'.repeat(20000) + 'x '.repeat(20000) + '42 ') or { panic(err) } |
| 90 | assert vm.read(large) or { panic(err) }.whole() == 42 |
| 91 | } |
| 92 | |
| 93 | fn test_prepared_compaction_allocates_one_cold_checkpoint_and_reuses_it_without_boxing() { |
| 94 | mut region := arena.new() |
| 95 | region.push() |
| 96 | mut vm := new() |
| 97 | input := vm.open_input_string('42 ') or { panic(err) } |
| 98 | mut port := input.input_pointer() |
| 99 | port.position = 3 |
| 100 | port.checkpoint_input() |
| 101 | assert region.owns(port.location) |
| 102 | checkpoint := port.location |
| 103 | before := region.used() |
| 104 | for _ in 0 .. 1000 { |
| 105 | // The prepared buffer is unchanged: exercise checkpoint construction and |
| 106 | // assignment without including source growth or user Reader allocations. |
| 107 | port.checkpoint_input() |
| 108 | } |
| 109 | assert port.location == checkpoint |
| 110 | assert region.used() == before |
| 111 | region.pop() |
| 112 | region.free() |
| 113 | } |
| 114 | |
| 115 | fn test_many_separate_block_comments_do_not_allocate_a_location_object_per_comment() { |
| 116 | mut region := arena.new() |
| 117 | region.push() |
| 118 | mut vm := new() |
| 119 | flat := vm.open_input_string('(' + ('#|x|# ').repeat(10000) + '42)') or { panic(err) } |
| 120 | before := region.used() |
| 121 | value := vm.read(flat) or { panic(err) } |
| 122 | assert value.pair_pointer().car.whole() == 42 |
| 123 | assert isnil(flat.input_pointer().location) |
| 124 | // Cells, one pair and reader working storage are bounded independently of |
| 125 | // the comment count. No retained or temporary per-comment location object. |
| 126 | assert region.used() - before < 65536 |
| 127 | region.pop() |
| 128 | region.free() |
| 129 | } |
| 130 | |