| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | fn test_public_make_rejects_a_child_arena_class_before_publishing_an_instance() { |
| 6 | mut vm := new() |
| 7 | make := vm.lookup('make') or { panic(err) } |
| 8 | allocate := vm.lookup('allocate-instance') or { panic(err) } |
| 9 | for family in ['<object>', '<class>', '<slot>', '<method>', '<generic>'] { |
| 10 | mut child := arena.new() |
| 11 | child.push() |
| 12 | class := vm.eval('(make-class (list ${family}) (quote ()))') or { panic(err) } |
| 13 | child.pop() |
| 14 | for constructor in [make, allocate] { |
| 15 | args := if eqv(constructor, make) { [class] } else { [class, nil_value()] } |
| 16 | if _ := vm.call(constructor, args) { |
| 17 | panic('published a parent-owned ${family} instance retaining a child-arena class') |
| 18 | } else { |
| 19 | if !err.msg().contains('arena boundary') { panic(err) } |
| 20 | } |
| 21 | if vm.active.len != 0 { panic('constructor rejection retained an evaluator') } |
| 22 | } |
| 23 | child.free() |
| 24 | } |
| 25 | value := vm.eval('(+ 20 22)') or { panic(err) } |
| 26 | if value.whole() != 42 { panic('constructor rejection left an unusable VM') } |
| 27 | } |
| 28 | |
| 29 | fn test_public_constructors_allow_same_arena_and_vm_root_classes() { |
| 30 | mut root := arena.new() |
| 31 | root.push() |
| 32 | mut vm := new() |
| 33 | vm.eval('(define root-class (make-class (quote ()) (quote ((x #:init-value 7)))))') or { panic(err) } |
| 34 | root.pop() |
| 35 | mut child := arena.new() |
| 36 | child.push() |
| 37 | for family in ['<object>', '<class>', '<slot>', '<method>', '<generic>'] { |
| 38 | value := vm.eval('(let ((local-class (make-class (list ${family}) (quote ((x #:init-value 9)))))) |
| 39 | (list (slot-ref (make local-class) (quote x)) |
| 40 | (slot-ref (make root-class) (quote x))))') or { panic(err) } |
| 41 | actual := write(value) or { panic(err) } |
| 42 | if actual != '(9 7)' { panic('valid constructor ownership failed: ${actual}') } |
| 43 | } |
| 44 | child.pop() |
| 45 | child.free() |
| 46 | root.push() |
| 47 | value := vm.eval('(slot-ref (make root-class) (quote x))') or { panic(err) } |
| 48 | if value.whole() != 7 { panic('temporary construction damaged the root class') } |
| 49 | root.pop() |
| 50 | root.free() |
| 51 | } |
| 52 | |