module scheme import arena import io @[heap] struct RegionDatumReader { text string mut: position int } fn (mut source RegionDatumReader) read(mut destination []u8) !int { if source.position == source.text.len { return io.Eof{} } count := int_min(destination.len, int_min(7, source.text.len - source.position)) unsafe { vmemcpy(destination.data, source.text.str + source.position, count) } source.position += count return count } fn read_region_graph(region &arena.Arena, mut snapshot []u8) ! { region.push() defer { region.pop() } mut vm := new() 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#))' } input := vm.input_port(source, read_size: 11)! assert vm.read(input)!.symbol_name()! == 'Name' data := vm.read(input)! mut port := input.input_pointer() assert !isnil(port.location) && region.owns(port.location) assert region.owns(data.pair_pointer()) first := data.pair_pointer().car assert first.pointer() == data.pair_pointer().cdr.pair_pointer().car.pointer() assert region.owns(first.pointer()) vm.define('data', data)! vm.eval('(string-set! (car data) 0 #\\z) (bytevector-u8-set! (caddr data) 1 9)')! text := first.as_string()! assert text == 'z\x00b' unsafe { vmemcpy(snapshot.data, text.str, text.len) } assert vm.read(input)!.tag == .eof vm.close_input_port(input)! } fn test_region_owned_extended_port_results_and_location_metadata_do_not_escape_after_reset() { mut snapshot := []u8{len: 3} mut region := arena.new() for _ in 0 .. 8 { read_region_graph(®ion, mut snapshot) or { panic(err) } assert !region.is_current() region.reset() assert snapshot == [u8(`z`), 0, `b`] } region.free() gc_collect() assert snapshot == [u8(`z`), 0, `b`] } fn test_prepared_plain_port_reads_do_not_allocate_new_scanner_or_location_objects() { mut region := arena.new() region.push() mut vm := new() input := vm.open_input_string('123 '.repeat(1000)) or { panic(err) } before := region.used() for _ in 0 .. 1000 { assert vm.read(input) or { panic(err) }.whole() == 123 } assert region.used() == before assert isnil(input.input_pointer().location) region.pop() region.free() } fn test_large_line_and_block_trivia_stay_bounded_and_discard_packets_release_their_high_water_buffer() { mut vm := new() for text in ['#|' + ('ignored #| nested |# λ\r\n').repeat(30000) + '|#42 ', ';' + ('ignored λ').repeat(30000) + '\u202842 '] { mut source := &RegionDatumReader{ text: text } input := vm.input_port(source, read_size: 31) or { panic(err) } assert vm.read(input) or { panic(err) }.whole() == 42 assert input.input_pointer().buffer.cap < 4096 } // #; intentionally pins one packet for canonical structural/label checking. // The allocation is O(packet bytes), then released before the returned data. mut source := &RegionDatumReader{ text: '#;(' + ('x ').repeat(100000) + ') 42 ' } input := vm.input_port(source, read_size: 31) or { panic(err) } assert vm.read(input) or { panic(err) }.whole() == 42 assert input.input_pointer().buffer.cap <= 65536 // Consecutive comments count ignored datums rather than nesting frames. large := vm.open_input_string('#;'.repeat(20000) + 'x '.repeat(20000) + '42 ') or { panic(err) } assert vm.read(large) or { panic(err) }.whole() == 42 } fn test_prepared_compaction_allocates_one_cold_checkpoint_and_reuses_it_without_boxing() { mut region := arena.new() region.push() mut vm := new() input := vm.open_input_string('42 ') or { panic(err) } mut port := input.input_pointer() port.position = 3 port.checkpoint_input() assert region.owns(port.location) checkpoint := port.location before := region.used() for _ in 0 .. 1000 { // The prepared buffer is unchanged: exercise checkpoint construction and // assignment without including source growth or user Reader allocations. port.checkpoint_input() } assert port.location == checkpoint assert region.used() == before region.pop() region.free() } fn test_many_separate_block_comments_do_not_allocate_a_location_object_per_comment() { mut region := arena.new() region.push() mut vm := new() flat := vm.open_input_string('(' + ('#|x|# ').repeat(10000) + '42)') or { panic(err) } before := region.used() value := vm.read(flat) or { panic(err) } assert value.pair_pointer().car.whole() == 42 assert isnil(flat.input_pointer().location) // Cells, one pair and reader working storage are bounded independently of // the comment count. No retained or temporary per-comment location object. assert region.used() - before < 65536 region.pop() region.free() }