vile / scheme / syntax_partial_independent_test.v
194 lines · 183 sloc · 9.42 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4import reader
5
6// Authored against private source21; no compiler/formatter execution here.
7fn review_partial_template() Value {
8 mut globals := {
9 'global': 42
10 }
11 code := &Code{ doc: reader.read_all('template') or { panic(err.msg()) }, known_globals: &globals }
12 function := &Function{ code: code }
13 mut scope := &Scope{ function: function }
14 scope.declare('absent-in-input')
15 mut builder := Builder{ code: code, function: function, scope: scope }
16 context := builder.capture_syntax_lookup_context() or { panic(err.msg()) }
17 mut tree, roots := builder.freeze_syntax_input(code.doc.forms) or { panic(err.msg()) }
18 tree.identifiers[0] = SyntaxIdentifier{ ...tree.identifiers[0], wrap: fresh_syntax_mark(unsafe { nil }) }
19 return wrapped_syntax(&SyntaxObjectData{ tree: tree, owner: &Owner{}, context: context }, int(roots[0])) or { panic(err.msg()) }
20}
21
22fn review_partial_wrapped(source string) Value {
23 code := &Code{ doc: reader.read_all(source) or { panic(err.msg()) } }
24 mut builder := Builder{ code: code, function: &Function{ code: code } }
25 tree, roots := builder.freeze_syntax_input(code.doc.forms) or { panic(err.msg()) }
26 return wrapped_syntax(&SyntaxObjectData{ tree: tree, owner: &Owner{} }, int(roots[0])) or { panic(err.msg()) }
27}
28
29fn review_partial_retained(input Value, template Value, wrapping bool) int {
30 mut output := arena.new(chunk_size: 1024 * 1024)
31 output.push()
32 value := if wrapping {
33 datum_to_syntax(template, input) or { panic(err.msg()) }
34 } else {
35 syntax_to_datum(input) or { panic(err.msg()) }
36 }
37 assert output.is_current() && output.owns(value.pointer())
38 used := output.used()
39 output.pop()
40 output.free()
41 return used
42}
43
44fn test_independent_partial_manufactured_identifiers_keep_complete_context_marks_and_owner() {
45 template := review_partial_template()
46 object := syntax_object_ref(template) or { panic(err.msg()) }
47 for name in ['absent-in-input', 'global', 'not-bound', '', '\x00source/name', '\x001.2/x',
48 'MiXeD/a', 'λ😀'] {
49 value := datum_to_syntax(template, text_value(.symbol, name)) or { panic(err.msg()) }
50 info := syntax_identifier_info(value) or { panic(err.msg()) }
51 assert info.name == external_identifier_key(name) && info.raw_name == name
52 assert same_syntax_binding(info.binding, object.data.context.binding(external_identifier_key(name)))
53 assert same_syntax_marks(info.wrap, syntax_identifier_info(template) or { panic(err.msg()) }.wrap)
54 assert voidptr((syntax_object_ref(value) or { panic(err.msg()) }).data.owner) == voidptr(object.data.owner)
55 assert syntax_to_datum(value) or { panic(err.msg()) }.symbol_name() or { panic(err.msg()) } == name
56 }
57}
58
59fn test_independent_partial_keeps_foreign_fragments_exact_and_does_not_retag_their_bindings() {
60 template := review_partial_template()
61 foreign := wrapped_syntax(&SyntaxObjectData{
62 owner: &Owner{}
63 tree: &SyntaxTree{
64 nodes: [SyntaxTreeNode{ kind: .symbol, car: 0 }]
65 identifiers: [SyntaxIdentifier{ name: 'global', raw_name: 'global', binding: SyntaxBinding{ kind: .global, name: 'global', global_slot: 42 }, wrap: fresh_syntax_mark(unsafe { nil }) }]
66 }
67 }, 0) or { panic(err.msg()) }
68 output := datum_to_syntax(template, vector([text_value(.symbol, 'global'), foreign,
69 text_value(.symbol, 'global'), foreign])) or { panic(err.msg()) }
70 items := output.vector_pointer().items
71 assert eqv(items[0], items[2]) && eqv(items[1], foreign) && eqv(items[3], foreign)
72 assert !eqv(items[0], foreign)
73 assert !(free_syntax_identifier_equal(items[0], foreign) or { panic(err.msg()) })
74 assert !(bound_syntax_identifier_equal(items[0], foreign) or { panic(err.msg()) })
75 assert eqv(datum_to_syntax(template, foreign) or { panic(err.msg()) }, foreign)
76 another := datum_to_syntax(template, text_value(.symbol, 'global')) or { panic(err.msg()) }
77 assert !eqv(another, items[0])
78 assert free_syntax_identifier_equal(another, items[0]) or { panic(err.msg()) }
79}
80
81fn test_independent_partial_external_nul_symbol_cannot_impersonate_exact_private_alias() {
82 mut code := &Code{ doc: reader.read_all('template') or { panic(err.msg()) } }
83 private := code.identifier_alias(IdentifierAlias{ binding: SyntaxBinding{ kind: .global, name: 'secret', global_slot: 99 } }) or { panic(err.msg()) }
84 mut builder := Builder{ code: code, function: &Function{ code: code } }
85 context := builder.capture_syntax_lookup_context() or { panic(err.msg()) }
86 assert context.binding(private).global_slot == 99
87 tree, roots := builder.freeze_syntax_input(code.doc.forms) or { panic(err.msg()) }
88 template := wrapped_syntax(&SyntaxObjectData{ tree: tree, context: context, owner: &Owner{} }, int(roots[0])) or { panic(err.msg()) }
89 value := datum_to_syntax(template, text_value(.symbol, private)) or { panic(err.msg()) }
90 info := syntax_identifier_info(value) or { panic(err.msg()) }
91 assert info.raw_name == private && info.name == external_identifier_key(private)
92 assert info.binding.unbound && info.binding.global_slot < 0
93 assert syntax_to_datum(value) or { panic(err.msg()) }.symbol_name() or { panic(err.msg()) } == private
94}
95
96fn test_independent_partial_mixed_raw_mutual_cycle_preserves_cross_document_backreferences() {
97 a := review_partial_wrapped('#1=#(#2=(7 . #1#) #2#)')
98 b := review_partial_wrapped('#1=#(#2=(7 . #1#) #2#)')
99 a_child := syntax_vector_ref(a, 0) or { panic(err.msg()) }
100 input := cons(a, nil_value())
101 links := vector([input, a_child, b])
102 mut pair := input.pair_pointer()
103 pair.cdr = links
104 partial := datum_to_syntax(review_partial_template(), input) or { panic(err.msg()) }
105 assert eqv(partial.pair_pointer().car, a)
106 assert eqv(partial, partial.pair_pointer().cdr.vector_pointer().items[0])
107 assert eqv(a_child, partial.pair_pointer().cdr.vector_pointer().items[1])
108 assert eqv(b, partial.pair_pointer().cdr.vector_pointer().items[2])
109 plain := syntax_to_datum(partial) or { panic(err.msg()) }
110 a_plain := plain.pair_pointer().car
111 plain_links := plain.pair_pointer().cdr.vector_pointer().items
112 b_plain := plain_links[2]
113 assert eqv(plain, plain_links[0])
114 assert eqv(a_plain.vector_pointer().items[0], plain_links[1])
115 assert eqv(a_plain, plain_links[1].pair_pointer().cdr)
116 assert eqv(b_plain, b_plain.vector_pointer().items[0].pair_pointer().cdr)
117 assert !eqv(a_plain, b_plain) && !eqv(plain_links[1], b_plain.vector_pointer().items[0])
118 gc_collect()
119 assert plain_links[1].pair_pointer().car.as_integer() or { panic(err.msg()) } == 7
120}
121
122fn test_independent_partial_new_ids_and_containers_outlive_symbol_source_scratch() {
123 mut published := arena.new()
124 published.push()
125 template := review_partial_template()
126 mut scratch := arena.new()
127 scratch.push()
128 ordinary := ('owned-' + 'x'.repeat(4096)).clone()
129 nul_name := ('\x00source/' + 'y'.repeat(2048)).clone()
130 input := vector([text_value(.symbol, ordinary), text_value(.symbol, nul_name),
131 text_value(.symbol, ordinary)])
132 scratch.pop()
133 output := datum_to_syntax(template, input) or { panic(err.msg()) }
134 assert published.is_current() && published.owns(output.pointer())
135 assert published.owns(output.vector_pointer().items.data)
136 first := output.vector_pointer().items[0]
137 second := output.vector_pointer().items[1]
138 assert eqv(first, output.vector_pointer().items[2])
139 a := syntax_identifier_info(first) or { panic(err.msg()) }
140 b := syntax_identifier_info(second) or { panic(err.msg()) }
141 assert published.owns(a.name.str) && published.owns(a.raw_name.str)
142 assert published.owns(b.name.str) && published.owns(b.raw_name.str)
143 scratch.free()
144 gc_collect()
145 assert a.raw_name.len == 4102 && a.raw_name.starts_with('owned-') && a.raw_name[4101] == `x`
146 assert b.raw_name.len == 2056 && b.raw_name.starts_with('\x00source/') && b.raw_name[2055] == `y`
147 assert syntax_to_datum(first) or { panic(err.msg()) }.symbol_name() or { panic(err.msg()) } == a.raw_name
148 published.pop()
149 published.free()
150}
151
152fn test_independent_partial_failures_restore_allocator_and_do_not_modify_input_or_template() {
153 mut published := arena.new()
154 published.push()
155 valid := review_partial_template()
156 missing_context := review_partial_wrapped('k')
157 foreign := review_partial_wrapped('(kept)')
158 input := vector([foreign, cons(integer(1), text_value(.symbol, 'bad'))])
159 for _ in 0 .. 4 {
160 if _ := datum_to_syntax(missing_context, input) {
161 assert false
162 } else {
163 assert err.msg().contains('syntax context')
164 }
165 assert published.is_current()
166 if _ := syntax_to_datum(input) {
167 assert false
168 } else {
169 assert err.msg().contains('unwrapped symbol')
170 }
171 assert published.is_current()
172 assert eqv(input.vector_pointer().items[0], foreign)
173 assert input.vector_pointer().items[1].pair_pointer().cdr.tag == .symbol
174 }
175 output := datum_to_syntax(valid, input) or { panic(err.msg()) }
176 assert eqv(output.vector_pointer().items[0], foreign)
177 assert syntax_identifier(output.vector_pointer().items[1].pair_pointer().cdr)
178 assert syntax_to_datum(output) or { panic(err.msg()) }.tag == .vector
179 assert published.is_current()
180 published.pop()
181 published.free()
182}
183
184fn test_independent_partial_work_storage_is_not_retained_with_pair_or_vector_outputs() {
185 template := review_partial_template()
186 mut small_items := []Value{len: 8, init: integer(1)}
187 mut large_items := []Value{len: 4096, init: integer(1)}
188 for wrapping in [false, true] {
189 small_pairs := list(small_items)
190 large_pairs := list(large_items)
191 assert review_partial_retained(large_pairs, template, wrapping) - review_partial_retained(small_pairs, template, wrapping) == (4096 - 8) * int(sizeof(Pair))
192 assert review_partial_retained(vector(large_items), template, wrapping) - review_partial_retained(vector(small_items), template, wrapping) == (4096 - 8) * int(sizeof(Value))
193 }
194}
195