| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | fn test_frozen_lexical_bindings_keep_domain_and_slot_without_function_backreferences() { |
| 7 | mut code := &Code{ doc: reader.read_all('x') or { panic(err) } } |
| 8 | function := &Function{ code: code } |
| 9 | mut scope := &Scope{ function: function } |
| 10 | scope.declare('x') |
| 11 | mut builder := Builder{ code: code, function: function, scope: scope } |
| 12 | live := builder.resolve_binding('x') |
| 13 | tree, roots := builder.freeze_syntax(code.doc.forms) or { panic(err) } |
| 14 | binding := tree.identifiers[tree.nodes[roots[0]].car].binding |
| 15 | assert binding.kind == .lexical && binding.slot == live.slot |
| 16 | assert isnil(binding.function) && isnil(binding.transformer) |
| 17 | assert isnil(binding.namespace) && isnil(binding.introduced_namespace) |
| 18 | assert !isnil(binding.lexical_domain) && binding.lexical_id != 0 |
| 19 | assert same_syntax_binding(binding, live) && same_syntax_binding(live, binding) |
| 20 | assert builder.syntax_binding_depth(binding) or { panic(err) } == 0 |
| 21 | assert function.syntax_identity == binding.lexical_id |
| 22 | code.freeze_syntax_locations() |
| 23 | assert voidptr(code.generated.lexical_domain) == voidptr(binding.lexical_domain) |
| 24 | } |
| 25 | |
| 26 | fn test_detached_lexical_binding_domains_distinguish_equal_local_ids_and_slots() { |
| 27 | mut bindings := []SyntaxBinding{} |
| 28 | for _ in 0 .. 2 { |
| 29 | code := &Code{ doc: reader.read_all('') or { panic(err) }, syntax_seed: 42 } |
| 30 | function := &Function{ code: code } |
| 31 | bindings << freeze_syntax_binding(SyntaxBinding{ kind: .lexical, function: function, slot: 0 }) or { panic(err) } |
| 32 | } |
| 33 | assert bindings[0].lexical_id == bindings[1].lexical_id |
| 34 | assert !same_syntax_binding(bindings[0], bindings[1]) |
| 35 | assert !isnil(bindings[0].lexical_domain) && !isnil(bindings[1].lexical_domain) |
| 36 | assert voidptr(bindings[0].lexical_domain) != voidptr(bindings[1].lexical_domain) |
| 37 | } |
| 38 | |
| 39 | fn test_detached_bindings_resolve_active_function_boundaries_without_scope_copies() { |
| 40 | mut code := &Code{ doc: reader.read_all('') or { panic(err) } } |
| 41 | outer := &Function{ code: code } |
| 42 | inner := &Function{ code: code } |
| 43 | root := &Scope{ function: outer } |
| 44 | shared := &Scope{ parent: root, function: outer } |
| 45 | child := &Scope{ parent: shared, function: inner } |
| 46 | region := &Scope{ parent: child, function: inner } |
| 47 | binding := freeze_syntax_binding(SyntaxBinding{ kind: .lexical, function: outer, slot: 7 }) or { panic(err) } |
| 48 | builder := Builder{ code: code, function: inner, scope: region } |
| 49 | assert builder.syntax_binding_depth(binding) or { panic(err) } == 1 |
| 50 | assert !syntax_binding_function_matches(binding, inner) |
| 51 | assert syntax_binding_function_matches(binding, outer) |
| 52 | unrelated := Builder{ code: code, function: inner, scope: &Scope{ function: inner } } |
| 53 | if _ := unrelated.syntax_binding_depth(binding) { |
| 54 | assert false |
| 55 | } else { |
| 56 | assert err.msg().contains('outside its binding region') |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | fn test_repeat_detachment_and_binding_comparison_allocate_no_additional_storage() { |
| 61 | mut region := arena.new() |
| 62 | region.push() |
| 63 | defer { |
| 64 | region.pop() |
| 65 | region.free() |
| 66 | } |
| 67 | code := &Code{ doc: reader.read_all('') or { panic(err) } } |
| 68 | function := &Function{ code: code } |
| 69 | live := SyntaxBinding{ kind: .lexical, name: 'x', function: function, slot: 3 } |
| 70 | detached := freeze_syntax_binding(live) or { panic(err) } |
| 71 | before := region.used() |
| 72 | for _ in 0 .. 1001 { |
| 73 | copy := freeze_syntax_binding(live) or { panic(err) } |
| 74 | assert same_syntax_binding(detached, copy) |
| 75 | assert same_syntax_binding(live, copy) |
| 76 | assert same_syntax_binding(copy, live) |
| 77 | assert isnil(copy.function) |
| 78 | } |
| 79 | assert region.used() == before |
| 80 | } |
| 81 | |
| 82 | fn test_lexical_identity_overflow_fails_before_publishing_or_reusing_an_id() { |
| 83 | mut code := &Code{ doc: reader.read_all('') or { panic(err) } } |
| 84 | mut domain := code.syntax_identity_domain() |
| 85 | domain.next = u32(0xffffffff) |
| 86 | function := &Function{ code: code } |
| 87 | if _ := freeze_syntax_binding(SyntaxBinding{ kind: .lexical, function: function, slot: 0 }) { |
| 88 | assert false |
| 89 | } else { |
| 90 | assert err.msg().contains('identity limit exceeded') |
| 91 | } |
| 92 | assert function.syntax_identity == 0 && domain.next == u32(0xffffffff) |
| 93 | } |
| 94 | |
| 95 | fn test_detached_lexical_syntax_releases_compiler_objects_but_keeps_publication_owned_metadata() { |
| 96 | mut published := arena.new() |
| 97 | published.push() |
| 98 | // Explicit clone prevents compile-time string folding from giving this |
| 99 | // ownership fixture static storage instead of arena storage. |
| 100 | owned_name := ('published-name-' + 'x'.repeat(4096)).clone() |
| 101 | domain := &SyntaxLexicalDomain{} |
| 102 | wrap := fresh_syntax_mark(unsafe { nil }) |
| 103 | assert published.owns(owned_name.str) |
| 104 | assert published.owns(domain) |
| 105 | assert published.owns(wrap) |
| 106 | mut scratch := arena.new() |
| 107 | scratch.push() |
| 108 | // The source, cached compiler objects, scope maps and generated arena are |
| 109 | // transient. Persistent handles are deliberately owned by the VM/publication |
| 110 | // region, as required by the normal compiler ownership contract. |
| 111 | source := owned_name.clone() |
| 112 | mut code := &Code{ doc: reader.read_all(source) or { panic(err) } } |
| 113 | code.names[code.doc.forms[0]] = owned_name |
| 114 | function := &Function{ code: code } |
| 115 | mut scope := &Scope{ function: function } |
| 116 | scope.declare(owned_name) |
| 117 | mut generated := code.syntax_arena() |
| 118 | generated.lexical_domain = domain |
| 119 | generated.aliases[owned_name] = IdentifierAlias{ binding: SyntaxBinding{ kind: .lexical, name: owned_name, function: function, slot: 0 }, use_site: true, use_scope: scope, use_name: owned_name, wrap: wrap } |
| 120 | mut builder := Builder{ code: code, function: function, scope: scope } |
| 121 | assert scratch.owns(code) && scratch.owns(function) && scratch.owns(source.str) |
| 122 | assert !scratch.owns(domain) && !scratch.owns(wrap) |
| 123 | scratch.pop() |
| 124 | tree, roots := builder.freeze_syntax(code.doc.forms) or { panic(err) } |
| 125 | assert isnil(tree.identifiers[0].binding.function) |
| 126 | assert published.owns(tree.identifiers[0].name.str) |
| 127 | assert voidptr(tree.identifiers[0].binding.lexical_domain) == voidptr(domain) |
| 128 | assert voidptr(tree.identifiers[0].wrap) == voidptr(wrap) |
| 129 | scratch.free() |
| 130 | gc_collect() |
| 131 | assert tree.nodes[roots[0]].kind == .symbol |
| 132 | binding := tree.identifiers[0].binding |
| 133 | assert binding.kind == .lexical && binding.lexical_id == 1 |
| 134 | assert same_syntax_binding(binding, binding) |
| 135 | object := wrapped_syntax(&SyntaxObjectData{ tree: tree }, int(roots[0])) or { panic(err) } |
| 136 | assert free_syntax_identifier_equal(object, object) or { panic(err) } |
| 137 | assert syntax_identifier_info(object) or { panic(err) }.raw_name == owned_name |
| 138 | published.pop() |
| 139 | published.free() |
| 140 | } |
| 141 | |
| 142 | fn test_frozen_lexical_template_aliases_preserve_assignment_under_shadowing() { |
| 143 | mut vm := new() |
| 144 | value := vm.eval('(let ((x 40)) |
| 145 | (let-syntax ((next (syntax-rules () ((_) (begin (set! x (+ x 1)) x))))) |
| 146 | (let ((x 99)) (list (next) (next) x))))') or { panic(err) } |
| 147 | assert write(value) or { panic(err) } == '(41 42 99)' |
| 148 | } |
| 149 | |
| 150 | fn test_local_enumeration_transformers_retain_detached_lexical_locations() { |
| 151 | mut vm := new() |
| 152 | assert vm.eval('(let () (define-enumeration color (red green) colors) |
| 153 | (let-syntax ((saved (syntax-rules () ((_) (colors red))))) |
| 154 | (let ((color 0) (colors 0)) (enum-set-member? (quote red) (saved)))))') or { panic(err) }.is_true() |
| 155 | } |
| 156 | |