| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | // Read-only review's independent oracles. These intentionally use the private |
| 6 | // location API to isolate birth-time rules from macro expansion's alias naming. |
| 7 | fn review_birth_builder(mut vm VM) &Builder { |
| 8 | 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 review_birth_pending(mut builder Builder, fallback SyntaxBinding, key string) SyntaxBinding { |
| 13 | return SyntaxBinding{ |
| 14 | ...fallback |
| 15 | introduced: key |
| 16 | introduced_namespace: builder.code.namespace |
| 17 | introduced_globals: builder.syntax_global_count() |
| 18 | introduced_generation: builder.code.syntax_global_generation_floor() or { panic(err.msg()) } |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | fn review_birth_identifier(mut vm VM, binding SyntaxBinding, context &SyntaxLookupContext, wrap &SyntaxWrap) Value { |
| 23 | data := &SyntaxObjectData{ |
| 24 | owner: vm.owner |
| 25 | context: context |
| 26 | tree: &SyntaxTree{ |
| 27 | context: context |
| 28 | nodes: [SyntaxTreeNode{ kind: .symbol, car: 0 }] |
| 29 | identifiers: [SyntaxIdentifier{ name: 'x', raw_name: 'x', binding: binding, wrap: wrap, context: context }] |
| 30 | } |
| 31 | } |
| 32 | return wrapped_syntax(data, 0) or { panic(err.msg()) } |
| 33 | } |
| 34 | |
| 35 | fn test_independent_private_birth_transport_does_not_discard_either_fence() { |
| 36 | mut vm := new() |
| 37 | public_slot := vm.bind_global('x', integer(1)) |
| 38 | key := '\x00~h-birth-transport/x' |
| 39 | older_slot := vm.bind_global(key, integer(99)) |
| 40 | mut builder := review_birth_builder(mut vm) |
| 41 | pending := review_birth_pending(mut builder, SyntaxBinding{ |
| 42 | kind: .global |
| 43 | name: 'x' |
| 44 | global_slot: public_slot |
| 45 | assignable: true |
| 46 | }, key) |
| 47 | assert runtime_syntax_binding(vm.owner, pending).binding.global_slot == public_slot |
| 48 | once := builder.current_syntax_binding(pending) |
| 49 | assert once.introduced == pending.introduced |
| 50 | assert once.introduced_globals == pending.introduced_globals |
| 51 | assert once.introduced_generation == pending.introduced_generation |
| 52 | twice := builder.current_syntax_binding(once) |
| 53 | assert twice.global_slot == public_slot |
| 54 | assert twice.global_slot != older_slot |
| 55 | assert runtime_syntax_binding(vm.owner, once).binding.global_slot == public_slot |
| 56 | builder.code.release_compile_borrows() |
| 57 | } |
| 58 | |
| 59 | fn test_independent_private_birth_set_does_not_redeclare_but_same_slot_definition_does() { |
| 60 | mut vm := new() |
| 61 | public_slot := vm.bind_global('x', integer(1)) |
| 62 | key := '\x00~h-birth-redeclare/x' |
| 63 | old_private_slot := vm.bind_global(key, integer(99)) |
| 64 | mut builder := review_birth_builder(mut vm) |
| 65 | pending := review_birth_pending(mut builder, SyntaxBinding{ |
| 66 | kind: .global |
| 67 | name: 'x' |
| 68 | global_slot: public_slot |
| 69 | assignable: true |
| 70 | }, key) |
| 71 | before := vm.owner.syntax_generation |
| 72 | // This mirrors resolved-global set!: value storage, no declaration effect. |
| 73 | container_store(mut vm.globals, old_private_slot, integer(100)) |
| 74 | assert vm.owner.syntax_generation == before |
| 75 | assert runtime_syntax_binding(vm.owner, pending).binding.global_slot == public_slot |
| 76 | // A real declaration may reuse an existing slot but has a new birth event. |
| 77 | assert vm.bind_global(key, integer(42)) == old_private_slot |
| 78 | assert vm.owner.syntax_generation >= pending.introduced_generation |
| 79 | assert runtime_syntax_binding(vm.owner, pending).binding.global_slot == old_private_slot |
| 80 | assert builder.current_syntax_binding(pending).global_slot == old_private_slot |
| 81 | builder.code.release_compile_borrows() |
| 82 | } |
| 83 | |
| 84 | fn test_independent_private_birth_new_syntax_alias_can_point_to_an_older_provider() { |
| 85 | mut vm := new() |
| 86 | provider := vm.new_environment() or { panic(err.msg()) } |
| 87 | vm.eval_in('(define-syntax old-provider (syntax-rules () ((_) 42)))', provider) or { panic(err.msg()) } |
| 88 | provider_space := vm.environment_space(provider) or { panic(err.msg()) } |
| 89 | original := vm.namespace_binding(provider_space, 'old-provider') |
| 90 | provider_location := syntax_global_location(vm.owner, provider_space, 'old-provider') or { panic('missing provider location') } |
| 91 | public_slot := vm.bind_global('x', integer(1)) |
| 92 | mut builder := review_birth_builder(mut vm) |
| 93 | key := '\x00~h-birth-import/x' |
| 94 | pending := review_birth_pending(mut builder, SyntaxBinding{ |
| 95 | kind: .global |
| 96 | name: 'x' |
| 97 | global_slot: public_slot |
| 98 | }, key) |
| 99 | assert provider_location.generation < pending.introduced_generation |
| 100 | vm.install_import(unsafe { nil }, NamespaceImport{ name: key, binding: original }) |
| 101 | destination := syntax_global_location(vm.owner, unsafe { nil }, key) or { panic('missing destination location') } |
| 102 | assert destination.generation >= pending.introduced_generation |
| 103 | assert destination.canonical().generation < pending.introduced_generation |
| 104 | assert runtime_syntax_binding_equal(pending, original, vm.owner, vm.owner) |
| 105 | assert same_syntax_binding(builder.current_syntax_binding(pending), original) |
| 106 | builder.code.release_compile_borrows() |
| 107 | } |
| 108 | |
| 109 | fn test_independent_private_birth_owner_generation_is_not_borrowed_from_another_vm() { |
| 110 | mut a := new() |
| 111 | mut b := new() |
| 112 | original_a := a.bind_global('x', integer(1)) |
| 113 | original_b := b.bind_global('x', integer(1)) |
| 114 | key := '\x00~h-birth-owner/x' |
| 115 | mut ab := review_birth_builder(mut a) |
| 116 | mut bb := review_birth_builder(mut b) |
| 117 | left := review_birth_pending(mut ab, SyntaxBinding{ kind: .global, name: 'x', global_slot: original_a }, key) |
| 118 | right := review_birth_pending(mut bb, SyntaxBinding{ kind: .global, name: 'x', global_slot: original_b }, key) |
| 119 | private_a := a.bind_global(key, integer(42)) |
| 120 | assert runtime_syntax_binding(a.owner, left).binding.global_slot == private_a |
| 121 | assert runtime_syntax_binding(b.owner, right).binding.global_slot == original_b |
| 122 | assert !runtime_syntax_binding_equal(left, right, a.owner, b.owner) |
| 123 | assert !runtime_syntax_binding_equal(left, a.namespace_binding(unsafe { nil }, key), b.owner, a.owner) |
| 124 | ab.code.release_compile_borrows() |
| 125 | bb.code.release_compile_borrows() |
| 126 | } |
| 127 | |
| 128 | fn test_independent_private_birth_datum_to_syntax_keeps_pending_binding_identity() { |
| 129 | mut vm := new() |
| 130 | public_slot := vm.bind_global('x', integer(1)) |
| 131 | mut builder := review_birth_builder(mut vm) |
| 132 | mut expansion := SyntaxExpansion{ domain: builder.syntax_mark_domain() } |
| 133 | wrap := expansion.transcription_wrap(unsafe { nil }) |
| 134 | key := '\x00~h-birth-datum/x' |
| 135 | pending := review_birth_pending(mut builder, SyntaxBinding{ |
| 136 | kind: .global |
| 137 | name: 'x' |
| 138 | global_slot: public_slot |
| 139 | }, key) |
| 140 | // The anchor's complete context has only the public x. Manufacturing x must |
| 141 | // preserve the anchor's controlled-capture identity and pending private fence. |
| 142 | context := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 143 | anchor := review_birth_identifier(mut vm, pending, context, wrap) |
| 144 | copy := datum_to_syntax(anchor, text_value(.symbol, 'x')) or { panic(err.msg()) } |
| 145 | assert bound_syntax_identifier_equal(anchor, copy) or { panic(err.msg()) } |
| 146 | assert free_syntax_identifier_equal(anchor, copy) or { panic(err.msg()) } |
| 147 | private_slot := vm.bind_global(key, integer(42)) |
| 148 | assert private_slot != public_slot |
| 149 | assert free_syntax_identifier_equal(anchor, copy) or { panic(err.msg()) } |
| 150 | assert runtime_syntax_binding(vm.owner, (syntax_identifier_info(copy) or { panic(err.msg()) }).binding).binding.global_slot == private_slot |
| 151 | builder.code.release_compile_borrows() |
| 152 | } |
| 153 | |
| 154 | fn test_independent_private_birth_real_set_preserves_location_and_declaration_generation() { |
| 155 | mut vm := new() |
| 156 | vm.eval('(define x 1)') or { panic(err.msg()) } |
| 157 | before := vm.eval('(syntax x)') or { panic(err.msg()) } |
| 158 | // Ensure location recording is active; normal public comparisons need no |
| 159 | // private generation fence, but writes still must not create new locations. |
| 160 | mut builder := review_birth_builder(mut vm) |
| 161 | builder.code.syntax_global_generation_floor() or { panic(err.msg()) } |
| 162 | vm.eval('(define y 0)') or { panic(err.msg()) } |
| 163 | generation := vm.owner.syntax_generation |
| 164 | vm.eval('(set! x 42)') or { panic(err.msg()) } |
| 165 | assert vm.owner.syntax_generation == generation |
| 166 | after := vm.eval('(syntax x)') or { panic(err.msg()) } |
| 167 | assert free_syntax_identifier_equal(before, after) or { panic(err.msg()) } |
| 168 | assert (vm.lookup('x') or { panic(err.msg()) }).as_integer() or { panic(err.msg()) } == 42 |
| 169 | builder.code.release_compile_borrows() |
| 170 | } |
| 171 | |
| 172 | fn test_independent_private_birth_datum_to_syntax_from_real_forward_anchor() { |
| 173 | mut vm := new() |
| 174 | vm.eval("(define x 100) |
| 175 | (define-syntax private-copy |
| 176 | (lambda (stx) |
| 177 | (syntax-case stx () |
| 178 | ((_ first copied second) |
| 179 | #'(begin |
| 180 | (define (first) #'x) |
| 181 | (define (copied) (datum->syntax #'x 'x)) |
| 182 | (define x 42) |
| 183 | (define (second) #'x)))))) |
| 184 | (private-copy before-private copied-private after-private)") or { panic(err.msg()) } |
| 185 | before := vm.eval('(before-private)') or { panic(err.msg()) } |
| 186 | copied := vm.eval('(copied-private)') or { panic(err.msg()) } |
| 187 | after := vm.eval('(after-private)') or { panic(err.msg()) } |
| 188 | public := vm.eval('(syntax x)') or { panic(err.msg()) } |
| 189 | assert free_syntax_identifier_equal(before, after) or { panic(err.msg()) } |
| 190 | assert bound_syntax_identifier_equal(before, copied) or { panic(err.msg()) } |
| 191 | assert free_syntax_identifier_equal(before, copied) or { panic(err.msg()) } |
| 192 | assert !(free_syntax_identifier_equal(copied, public) or { panic(err.msg()) }) |
| 193 | } |
| 194 | |