module scheme import arena import reader // Authored against private source21; no compiler/formatter execution here. fn review_partial_template() Value { mut globals := { 'global': 42 } code := &Code{ doc: reader.read_all('template') or { panic(err.msg()) }, known_globals: &globals } function := &Function{ code: code } mut scope := &Scope{ function: function } scope.declare('absent-in-input') mut builder := Builder{ code: code, function: function, scope: scope } context := builder.capture_syntax_lookup_context() or { panic(err.msg()) } mut tree, roots := builder.freeze_syntax_input(code.doc.forms) or { panic(err.msg()) } tree.identifiers[0] = SyntaxIdentifier{ ...tree.identifiers[0], wrap: fresh_syntax_mark(unsafe { nil }) } return wrapped_syntax(&SyntaxObjectData{ tree: tree, owner: &Owner{}, context: context }, int(roots[0])) or { panic(err.msg()) } } fn review_partial_wrapped(source string) Value { code := &Code{ doc: reader.read_all(source) or { panic(err.msg()) } } mut builder := Builder{ code: code, function: &Function{ code: code } } tree, roots := builder.freeze_syntax_input(code.doc.forms) or { panic(err.msg()) } return wrapped_syntax(&SyntaxObjectData{ tree: tree, owner: &Owner{} }, int(roots[0])) or { panic(err.msg()) } } fn review_partial_retained(input Value, template Value, wrapping bool) int { mut output := arena.new(chunk_size: 1024 * 1024) output.push() value := if wrapping { datum_to_syntax(template, input) or { panic(err.msg()) } } else { syntax_to_datum(input) or { panic(err.msg()) } } assert output.is_current() && output.owns(value.pointer()) used := output.used() output.pop() output.free() return used } fn test_independent_partial_manufactured_identifiers_keep_complete_context_marks_and_owner() { template := review_partial_template() object := syntax_object_ref(template) or { panic(err.msg()) } for name in ['absent-in-input', 'global', 'not-bound', '', '\x00source/name', '\x001.2/x', 'MiXeD/a', 'λ😀'] { value := datum_to_syntax(template, text_value(.symbol, name)) or { panic(err.msg()) } info := syntax_identifier_info(value) or { panic(err.msg()) } assert info.name == external_identifier_key(name) && info.raw_name == name assert same_syntax_binding(info.binding, object.data.context.binding(external_identifier_key(name))) assert same_syntax_marks(info.wrap, syntax_identifier_info(template) or { panic(err.msg()) }.wrap) assert voidptr((syntax_object_ref(value) or { panic(err.msg()) }).data.owner) == voidptr(object.data.owner) assert syntax_to_datum(value) or { panic(err.msg()) }.symbol_name() or { panic(err.msg()) } == name } } fn test_independent_partial_keeps_foreign_fragments_exact_and_does_not_retag_their_bindings() { template := review_partial_template() foreign := wrapped_syntax(&SyntaxObjectData{ owner: &Owner{} tree: &SyntaxTree{ nodes: [SyntaxTreeNode{ kind: .symbol, car: 0 }] identifiers: [SyntaxIdentifier{ name: 'global', raw_name: 'global', binding: SyntaxBinding{ kind: .global, name: 'global', global_slot: 42 }, wrap: fresh_syntax_mark(unsafe { nil }) }] } }, 0) or { panic(err.msg()) } output := datum_to_syntax(template, vector([text_value(.symbol, 'global'), foreign, text_value(.symbol, 'global'), foreign])) or { panic(err.msg()) } items := output.vector_pointer().items assert eqv(items[0], items[2]) && eqv(items[1], foreign) && eqv(items[3], foreign) assert !eqv(items[0], foreign) assert !(free_syntax_identifier_equal(items[0], foreign) or { panic(err.msg()) }) assert !(bound_syntax_identifier_equal(items[0], foreign) or { panic(err.msg()) }) assert eqv(datum_to_syntax(template, foreign) or { panic(err.msg()) }, foreign) another := datum_to_syntax(template, text_value(.symbol, 'global')) or { panic(err.msg()) } assert !eqv(another, items[0]) assert free_syntax_identifier_equal(another, items[0]) or { panic(err.msg()) } } fn test_independent_partial_external_nul_symbol_cannot_impersonate_exact_private_alias() { mut code := &Code{ doc: reader.read_all('template') or { panic(err.msg()) } } private := code.identifier_alias(IdentifierAlias{ binding: SyntaxBinding{ kind: .global, name: 'secret', global_slot: 99 } }) or { panic(err.msg()) } mut builder := Builder{ code: code, function: &Function{ code: code } } context := builder.capture_syntax_lookup_context() or { panic(err.msg()) } assert context.binding(private).global_slot == 99 tree, roots := builder.freeze_syntax_input(code.doc.forms) or { panic(err.msg()) } template := wrapped_syntax(&SyntaxObjectData{ tree: tree, context: context, owner: &Owner{} }, int(roots[0])) or { panic(err.msg()) } value := datum_to_syntax(template, text_value(.symbol, private)) or { panic(err.msg()) } info := syntax_identifier_info(value) or { panic(err.msg()) } assert info.raw_name == private && info.name == external_identifier_key(private) assert info.binding.unbound && info.binding.global_slot < 0 assert syntax_to_datum(value) or { panic(err.msg()) }.symbol_name() or { panic(err.msg()) } == private } fn test_independent_partial_mixed_raw_mutual_cycle_preserves_cross_document_backreferences() { a := review_partial_wrapped('#1=#(#2=(7 . #1#) #2#)') b := review_partial_wrapped('#1=#(#2=(7 . #1#) #2#)') a_child := syntax_vector_ref(a, 0) or { panic(err.msg()) } input := cons(a, nil_value()) links := vector([input, a_child, b]) mut pair := input.pair_pointer() pair.cdr = links partial := datum_to_syntax(review_partial_template(), input) or { panic(err.msg()) } assert eqv(partial.pair_pointer().car, a) assert eqv(partial, partial.pair_pointer().cdr.vector_pointer().items[0]) assert eqv(a_child, partial.pair_pointer().cdr.vector_pointer().items[1]) assert eqv(b, partial.pair_pointer().cdr.vector_pointer().items[2]) plain := syntax_to_datum(partial) or { panic(err.msg()) } a_plain := plain.pair_pointer().car plain_links := plain.pair_pointer().cdr.vector_pointer().items b_plain := plain_links[2] assert eqv(plain, plain_links[0]) assert eqv(a_plain.vector_pointer().items[0], plain_links[1]) assert eqv(a_plain, plain_links[1].pair_pointer().cdr) assert eqv(b_plain, b_plain.vector_pointer().items[0].pair_pointer().cdr) assert !eqv(a_plain, b_plain) && !eqv(plain_links[1], b_plain.vector_pointer().items[0]) gc_collect() assert plain_links[1].pair_pointer().car.as_integer() or { panic(err.msg()) } == 7 } fn test_independent_partial_new_ids_and_containers_outlive_symbol_source_scratch() { mut published := arena.new() published.push() template := review_partial_template() mut scratch := arena.new() scratch.push() ordinary := ('owned-' + 'x'.repeat(4096)).clone() nul_name := ('\x00source/' + 'y'.repeat(2048)).clone() input := vector([text_value(.symbol, ordinary), text_value(.symbol, nul_name), text_value(.symbol, ordinary)]) scratch.pop() output := datum_to_syntax(template, input) or { panic(err.msg()) } assert published.is_current() && published.owns(output.pointer()) assert published.owns(output.vector_pointer().items.data) first := output.vector_pointer().items[0] second := output.vector_pointer().items[1] assert eqv(first, output.vector_pointer().items[2]) a := syntax_identifier_info(first) or { panic(err.msg()) } b := syntax_identifier_info(second) or { panic(err.msg()) } assert published.owns(a.name.str) && published.owns(a.raw_name.str) assert published.owns(b.name.str) && published.owns(b.raw_name.str) scratch.free() gc_collect() assert a.raw_name.len == 4102 && a.raw_name.starts_with('owned-') && a.raw_name[4101] == `x` assert b.raw_name.len == 2056 && b.raw_name.starts_with('\x00source/') && b.raw_name[2055] == `y` assert syntax_to_datum(first) or { panic(err.msg()) }.symbol_name() or { panic(err.msg()) } == a.raw_name published.pop() published.free() } fn test_independent_partial_failures_restore_allocator_and_do_not_modify_input_or_template() { mut published := arena.new() published.push() valid := review_partial_template() missing_context := review_partial_wrapped('k') foreign := review_partial_wrapped('(kept)') input := vector([foreign, cons(integer(1), text_value(.symbol, 'bad'))]) for _ in 0 .. 4 { if _ := datum_to_syntax(missing_context, input) { assert false } else { assert err.msg().contains('syntax context') } assert published.is_current() if _ := syntax_to_datum(input) { assert false } else { assert err.msg().contains('unwrapped symbol') } assert published.is_current() assert eqv(input.vector_pointer().items[0], foreign) assert input.vector_pointer().items[1].pair_pointer().cdr.tag == .symbol } output := datum_to_syntax(valid, input) or { panic(err.msg()) } assert eqv(output.vector_pointer().items[0], foreign) assert syntax_identifier(output.vector_pointer().items[1].pair_pointer().cdr) assert syntax_to_datum(output) or { panic(err.msg()) }.tag == .vector assert published.is_current() published.pop() published.free() } fn test_independent_partial_work_storage_is_not_retained_with_pair_or_vector_outputs() { template := review_partial_template() mut small_items := []Value{len: 8, init: integer(1)} mut large_items := []Value{len: 4096, init: integer(1)} for wrapping in [false, true] { small_pairs := list(small_items) large_pairs := list(large_items) assert review_partial_retained(large_pairs, template, wrapping) - review_partial_retained(small_pairs, template, wrapping) == (4096 - 8) * int(sizeof(Pair)) assert review_partial_retained(vector(large_items), template, wrapping) - review_partial_retained(vector(small_items), template, wrapping) == (4096 - 8) * int(sizeof(Value)) } }