| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | // Independent immutable private-alias snapshot oracles. Authored, not executed. |
| 7 | fn review_alias_builder(mut vm VM) &Builder { |
| 8 | mut code := vm.new_code(reader.read_all('') or { panic(err.msg()) }, vm.next_context(), unsafe { nil }, '') |
| 9 | return &Builder{ code: code, function: &Function{ code: code } } |
| 10 | } |
| 11 | |
| 12 | fn test_independent_alias_snapshots_revisit_binding_after_scope_boundary_changes_without_registry_growth() { |
| 13 | mut vm := new() |
| 14 | mut builder := review_alias_builder(mut vm) |
| 15 | mark := &SyntaxWrap{ domain: &SyntaxLexicalDomain{}, mark: 1 } |
| 16 | mut scratch := arena.new(chunk_size: 1024) |
| 17 | key := syntax_capture_family(mark, scratch) + '/x' |
| 18 | scratch.free() |
| 19 | mut boundary := &Scope{ function: builder.function } |
| 20 | boundary.declare(key) |
| 21 | origin := &Function{ code: builder.code } |
| 22 | builder.scope = boundary |
| 23 | mut code := builder.code |
| 24 | name := code.identifier_alias(IdentifierAlias{ binding: SyntaxBinding{ kind: .lexical, name: 'x', function: origin, slot: 7 }, wrap: mark, capture_key: key, capture_scope: boundary, capture_slots: builder.function.slot_count }) or { panic(err.msg()) } |
| 25 | first := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 26 | mut child := &Scope{ parent: boundary, function: builder.function } |
| 27 | new_slot := child.declare(key) |
| 28 | builder.scope = child |
| 29 | middle := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 30 | builder.scope = boundary |
| 31 | last := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 32 | assert code.generated.aliases.len == 1 |
| 33 | assert first.binding(name).slot == 7 |
| 34 | assert middle.binding(name).slot == new_slot |
| 35 | assert last.binding(name).slot == 7 |
| 36 | assert voidptr(first.binding(name).lexical_domain) == voidptr(last.binding(name).lexical_domain) |
| 37 | assert first.binding(name).lexical_id == last.binding(name).lexical_id |
| 38 | assert middle.binding(name).lexical_id != first.binding(name).lexical_id |
| 39 | code.release_compile_borrows() |
| 40 | } |
| 41 | |
| 42 | fn test_independent_alias_snapshots_preserve_transport_policy_through_same_key_a_b_a_updates() { |
| 43 | mut vm := new() |
| 44 | mut builder := review_alias_builder(mut vm) |
| 45 | mut code := builder.code |
| 46 | left := &Namespace{ writable: false } |
| 47 | right := &Namespace{ writable: true } |
| 48 | a := SyntaxBinding{ kind: .global, name: 'private-policy', namespace: left, global_slot: 7, assignable: false } |
| 49 | b := SyntaxBinding{ kind: .global, name: 'private-policy', namespace: right, global_slot: 7, assignable: true } |
| 50 | assert same_syntax_binding(a, b) |
| 51 | assert !same_syntax_transport(a, b) |
| 52 | name := code.identifier_alias(IdentifierAlias{ binding: a }) or { panic(err.msg()) } |
| 53 | first := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 54 | original := code.generated.aliases[name] |
| 55 | code.generated.aliases[name] = IdentifierAlias{ ...original, binding: b } |
| 56 | middle := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 57 | code.generated.aliases[name] = IdentifierAlias{ ...original, binding: a } |
| 58 | last := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 59 | assert code.generated.aliases.len == 1 |
| 60 | assert voidptr(first.binding(name).namespace) == voidptr(left) && !first.binding(name).assignable |
| 61 | assert voidptr(middle.binding(name).namespace) == voidptr(right) && middle.binding(name).assignable |
| 62 | assert voidptr(last.binding(name).namespace) == voidptr(left) && !last.binding(name).assignable |
| 63 | code.release_compile_borrows() |
| 64 | } |
| 65 | |
| 66 | fn test_independent_alias_snapshots_old_versions_never_see_future_names_across_binary_carries() { |
| 67 | mut vm := new() |
| 68 | mut builder := review_alias_builder(mut vm) |
| 69 | mut code := builder.code |
| 70 | mut names := []string{} |
| 71 | mut versions := []&SyntaxLookupContext{} |
| 72 | versions << (builder.capture_syntax_lookup_context() or { panic(err.msg()) }) |
| 73 | for i in 0 .. 129 { |
| 74 | name := code.identifier_alias(IdentifierAlias{ binding: SyntaxBinding{ kind: .global, name: 'origin-${i}', global_slot: i + 7 } }) or { panic(err.msg()) } |
| 75 | names << name |
| 76 | versions << (builder.capture_syntax_lookup_context() or { panic(err.msg()) }) |
| 77 | } |
| 78 | for version_index, context in versions { |
| 79 | for i, name in names { |
| 80 | if i < version_index { |
| 81 | assert context.binding(name).global_slot == i + 7 |
| 82 | } else { |
| 83 | assert context.binding(name).unbound |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | code.release_compile_borrows() |
| 88 | } |
| 89 | |
| 90 | fn test_independent_alias_snapshots_survive_compiler_scratch_release_with_detached_rows() { |
| 91 | mut vm := new() |
| 92 | mut published := arena.new(chunk_size: 1048576) |
| 93 | published.push() |
| 94 | mut scratch := arena.new(chunk_size: 4096) |
| 95 | scratch.push() |
| 96 | mut code := vm.new_code(reader.read_all('') or { panic(err.msg()) }, vm.next_context(), unsafe { nil }, '') |
| 97 | function := &Function{ code: code } |
| 98 | raw := 'dynamic-row-name'.clone() |
| 99 | mut builder := Builder{ code: code, function: function } |
| 100 | scratch.pop() |
| 101 | mut published_code := code |
| 102 | name := published_code.identifier_alias(IdentifierAlias{ binding: SyntaxBinding{ kind: .lexical, name: raw, function: function, slot: 0 } }) or { panic(err.msg()) } |
| 103 | first := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 104 | prior := code.generated.aliases[name] |
| 105 | code.generated.aliases[name] = IdentifierAlias{ ...prior, binding: SyntaxBinding{ ...prior.binding, slot: 1 } } |
| 106 | second := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 107 | published_code.release_compile_borrows() |
| 108 | scratch.free() |
| 109 | gc_collect() |
| 110 | for i, context in [first, second] { |
| 111 | actual := context.binding(name) |
| 112 | assert actual.kind == .lexical && actual.slot == i |
| 113 | assert isnil(actual.function) |
| 114 | assert published.owns(actual.lexical_domain) |
| 115 | assert published.owns(actual.name.str) |
| 116 | assert actual.name == 'dynamic-row-name' |
| 117 | } |
| 118 | assert first.binding(name).slot == 0 |
| 119 | assert second.binding(name).slot == 1 |
| 120 | published.pop() |
| 121 | published.free() |
| 122 | } |
| 123 | |
| 124 | fn review_alias_retained_growth(width int) int { |
| 125 | mut vm := new() |
| 126 | mut region := arena.new(chunk_size: 1048576) |
| 127 | region.push() |
| 128 | mut builder := review_alias_builder(mut vm) |
| 129 | mut code := builder.code |
| 130 | _ := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 131 | before := region.used() |
| 132 | for i in 0 .. width { |
| 133 | _ := code.identifier_alias(IdentifierAlias{ binding: SyntaxBinding{ kind: .global, name: 'alias-origin-${i}', global_slot: i + 7 } }) or { panic(err.msg()) } |
| 134 | _ := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 135 | } |
| 136 | retained := region.used() - before |
| 137 | code.release_compile_borrows() |
| 138 | region.pop() |
| 139 | region.free() |
| 140 | return retained |
| 141 | } |
| 142 | |
| 143 | fn test_independent_alias_snapshot_retained_growth_is_below_repeated_full_flat_map_copying() { |
| 144 | small := review_alias_retained_growth(128) |
| 145 | large := review_alias_retained_growth(512) |
| 146 | // Four times as many versions/rows can incur logarithmic forest maintenance; |
| 147 | // repeated full-map copies grow quadratically. This is retained arena storage, |
| 148 | // excluding one warmed global base, not a time/RSS or exact-layout assertion. |
| 149 | assert large <= small * 8 + 65536, '${small} -> ${large}' |
| 150 | } |
| 151 | |
| 152 | fn test_independent_manual_code_flat_snapshots_handle_same_size_edits_and_deletions() { |
| 153 | code := &Code{ doc: reader.read_all('') or { panic(err.msg()) } } |
| 154 | mut builder := Builder{ code: code, function: &Function{ code: code } } |
| 155 | mut generated := code.syntax_arena() |
| 156 | generated.aliases['manual-alias'] = IdentifierAlias{ binding: SyntaxBinding{ kind: .global, name: 'first-origin', global_slot: 7 } } |
| 157 | first := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 158 | generated.aliases['manual-alias'] = IdentifierAlias{ binding: SyntaxBinding{ kind: .global, name: 'second-origin', global_slot: 8 } } |
| 159 | second := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 160 | generated.aliases.delete('manual-alias') |
| 161 | generated.aliases['replacement-alias'] = IdentifierAlias{ binding: SyntaxBinding{ kind: .global, name: 'third-origin', global_slot: 9 } } |
| 162 | last := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 163 | assert generated.aliases.len == 1 |
| 164 | assert first.binding('manual-alias').global_slot == 7 |
| 165 | assert second.binding('manual-alias').global_slot == 8 |
| 166 | assert last.binding('manual-alias').unbound |
| 167 | assert last.binding('replacement-alias').global_slot == 9 |
| 168 | assert first.binding('replacement-alias').unbound |
| 169 | assert second.binding('replacement-alias').unbound |
| 170 | } |
| 171 | |
| 172 | fn test_independent_alias_snapshot_large_new_delta_wins_over_older_small_run() { |
| 173 | mut vm := new() |
| 174 | mut builder := review_alias_builder(mut vm) |
| 175 | mut code := builder.code |
| 176 | name := code.identifier_alias(IdentifierAlias{ binding: SyntaxBinding{ kind: .global, name: 'changed-origin', global_slot: 7 } }) or { panic(err.msg()) } |
| 177 | first := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 178 | prior := code.generated.aliases[name] |
| 179 | code.generated.aliases[name] = IdentifierAlias{ ...prior, binding: SyntaxBinding{ ...prior.binding, global_slot: 17 } } |
| 180 | mut future := []string{} |
| 181 | for i in 0 .. 32 { |
| 182 | future << (code.identifier_alias(IdentifierAlias{ binding: SyntaxBinding{ kind: .global, name: 'batch-origin-${i}', global_slot: i + 8 } }) or { panic(err.msg()) }) |
| 183 | } |
| 184 | last := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 185 | assert first.binding(name).global_slot == 7 |
| 186 | assert last.binding(name).global_slot == 17 |
| 187 | for i, added in future { |
| 188 | assert first.binding(added).unbound |
| 189 | assert last.binding(added).global_slot == i + 8 |
| 190 | } |
| 191 | code.release_compile_borrows() |
| 192 | } |
| 193 | |