module scheme import arena fn test_public_make_rejects_a_child_arena_class_before_publishing_an_instance() { mut vm := new() make := vm.lookup('make') or { panic(err) } allocate := vm.lookup('allocate-instance') or { panic(err) } for family in ['', '', '', '', ''] { mut child := arena.new() child.push() class := vm.eval('(make-class (list ${family}) (quote ()))') or { panic(err) } child.pop() for constructor in [make, allocate] { args := if eqv(constructor, make) { [class] } else { [class, nil_value()] } if _ := vm.call(constructor, args) { panic('published a parent-owned ${family} instance retaining a child-arena class') } else { if !err.msg().contains('arena boundary') { panic(err) } } if vm.active.len != 0 { panic('constructor rejection retained an evaluator') } } child.free() } value := vm.eval('(+ 20 22)') or { panic(err) } if value.whole() != 42 { panic('constructor rejection left an unusable VM') } } fn test_public_constructors_allow_same_arena_and_vm_root_classes() { mut root := arena.new() root.push() mut vm := new() vm.eval('(define root-class (make-class (quote ()) (quote ((x #:init-value 7)))))') or { panic(err) } root.pop() mut child := arena.new() child.push() for family in ['', '', '', '', ''] { value := vm.eval('(let ((local-class (make-class (list ${family}) (quote ((x #:init-value 9)))))) (list (slot-ref (make local-class) (quote x)) (slot-ref (make root-class) (quote x))))') or { panic(err) } actual := write(value) or { panic(err) } if actual != '(9 7)' { panic('valid constructor ownership failed: ${actual}') } } child.pop() child.free() root.push() value := vm.eval('(slot-ref (make root-class) (quote x))') or { panic(err) } if value.whole() != 7 { panic('temporary construction damaged the root class') } root.pop() root.free() }