| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | fn test_private_snapshot_carries_keep_newest_updates_reversions_and_deletions() { |
| 7 | mut runs := []&SyntaxPrivateRun{} |
| 8 | mut snapshots := []&SyntaxPrivateSnapshot{} |
| 9 | for value in [7, 8, 7, -1, 9] { |
| 10 | runs = runs.clone() |
| 11 | row := &SyntaxPrivateRow{ name: 'x', binding: SyntaxBinding{ kind: .global, name: 'x', global_slot: value }, deleted: value < 0 } |
| 12 | append_syntax_private_row(mut runs, row) |
| 13 | snapshots << &SyntaxPrivateSnapshot{ runs: runs } |
| 14 | } |
| 15 | for i, expected in [7, 8, 7, -1, 9] { |
| 16 | if expected < 0 { |
| 17 | assert snapshots[i].binding('x') == none |
| 18 | } else { |
| 19 | assert (snapshots[i].binding('x') or { panic('missing x') }).global_slot == expected |
| 20 | } |
| 21 | } |
| 22 | assert snapshots[0].binding('future') == none |
| 23 | } |
| 24 | |
| 25 | fn test_private_context_direct_removal_and_same_size_replacement_keep_prior_snapshots() { |
| 26 | mut vm := new() |
| 27 | mut code := vm.new_code(reader.read_all('') or { panic(err) }, vm.next_context(), unsafe { nil }, '') |
| 28 | mut builder := Builder{ code: code, function: &Function{ code: code } } |
| 29 | name := code.identifier_alias(IdentifierAlias{ binding: SyntaxBinding{ kind: .global, name: 'x', global_slot: 7 } }) or { panic(err) } |
| 30 | first := builder.capture_syntax_lookup_context() or { panic(err) } |
| 31 | mut generated := code.generated |
| 32 | generated.aliases.delete(name) |
| 33 | generated.aliases['replacement'] = IdentifierAlias{ binding: SyntaxBinding{ kind: .global, name: 'y', global_slot: 8 } } |
| 34 | second := builder.capture_syntax_lookup_context() or { panic(err) } |
| 35 | assert first.binding(name).global_slot == 7 |
| 36 | assert first.binding('replacement').unbound |
| 37 | assert second.binding(name).unbound |
| 38 | assert second.binding('replacement').global_slot == 8 |
| 39 | generated.globals['replacement'] = SyntaxBinding{ kind: .global, name: 'z', global_slot: 9 } |
| 40 | third := builder.capture_syntax_lookup_context() or { panic(err) } |
| 41 | assert third.binding('replacement').global_slot == 9 |
| 42 | assert second.binding('replacement').global_slot == 8 |
| 43 | generated.globals.delete('replacement') |
| 44 | fourth := builder.capture_syntax_lookup_context() or { panic(err) } |
| 45 | assert fourth.binding('replacement').global_slot == 8 |
| 46 | assert third.binding('replacement').global_slot == 9 |
| 47 | code.release_compile_borrows() |
| 48 | assert isnil(code.procedural.private_cache) |
| 49 | } |
| 50 | |
| 51 | fn introduced_macro_retained(count int) int { |
| 52 | mut vm := new_with_options(Options{ max_expansions: 1024 }) or { panic(err) } |
| 53 | vm.eval('(define-syntax pass (lambda (stx) (syntax-case stx () ((_ e) (syntax (+ e 0)))))) (pass 0)') or { panic(err) } |
| 54 | source := '(list ' + '(pass 1) '.repeat(count) + ')' |
| 55 | mut publication := arena.new(chunk_size: 1048576) |
| 56 | publication.push() |
| 57 | result := vm.eval(source) or { panic(err) } |
| 58 | used := publication.used() |
| 59 | publication.pop() |
| 60 | mut cursor := result |
| 61 | for _ in 0 .. count { |
| 62 | assert cursor.pair_pointer().car.as_integer() or { panic(err) } == 1 |
| 63 | cursor = cursor.pair_pointer().cdr |
| 64 | } |
| 65 | assert cursor.tag == .nil |
| 66 | publication.free() |
| 67 | return used |
| 68 | } |
| 69 | |
| 70 | fn test_introducing_macro_allocation_avoids_quadratic_alias_snapshot_copies() { |
| 71 | small := introduced_macro_retained(128) |
| 72 | large := introduced_macro_retained(512) |
| 73 | assert large - small < 8 * 1048576, '128:${small},512:${large}' |
| 74 | } |
| 75 | |