vile / scheme / goops_class_reference_ownership_test.v
143 lines · 136 sloc · 6.1 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn test_long_lived_goops_metadata_rejects_short_lived_class_and_slot_references() {
6 mut vm := new()
7 vm.eval("(define parent (make-class '() '((x)))) (define slot (class-slot-definition parent 'x))
8 (define getter (compute-getter-method parent slot))
9 (define installed (make <method> #:specializers (cons parent <top>) #:procedure (lambda args 42)))
10 (define g (make-generic 'g)) (add-method! g installed)
11 (define before (list (class-direct-supers parent) (class-precedence-list parent)
12 (class-direct-slots parent) (slot-ref parent 'redefined) (method-specializers installed)))") or { panic(err) }
13 mut short := arena.new()
14 short.push()
15 child := vm.eval("(make-class '() '((y)))") or { panic(err) }
16 short.pop()
17 for body in [
18 "(make-class (list child) '())",
19 "(make-class '() (list (class-slot-definition child 'y)))",
20 "(slot-set! parent 'direct-supers (list <object> child))",
21 "(slot-set! parent 'cpl (list parent child))",
22 "(slot-set! parent 'redefined child)",
23 "(make <method> #:specializers (list child))",
24 "(make <method> #:specializers (cons parent child))",
25 "(method ((x child)) x)",
26 "(slot-set! installed 'specializers (list parent child))",
27 "(slot-set! installed 'specializers (cons parent child))",
28 "(compute-getter-method child slot)",
29 "(compute-setter-method parent (class-slot-definition child 'y))",
30 "(slot-set! getter 'slot-definition (class-slot-definition child 'y))",
31 "(slot-set! parent 'direct-slots (list slot (class-slot-definition child 'y)))",
32 ] {
33 operation := vm.eval('(lambda (child) ' + body + ')') or { panic(err) }
34 if _ := vm.call(operation, [child]) {
35 panic('metadata publication accepted a shorter-lived reference in ${body}')
36 } else {
37 if !err.msg().contains('arena boundary') { panic('${body}: ${err}') }
38 }
39 }
40 short.free()
41 result := vm.eval("(and (equal? before
42 (list (class-direct-supers parent) (class-precedence-list parent)
43 (class-direct-slots parent) (slot-ref parent 'redefined) (method-specializers installed)))
44 (eq? (slot-ref getter 'slot-definition) slot) (= (g (make parent)) 42))") or { panic(err) }
45 if !result.is_true() { panic('failed publication changed existing metadata or dispatch') }
46}
47
48fn test_method_specializers_copy_the_borrowed_list_spine_and_keep_class_identity() {
49 mut vm := new()
50 proc := vm.eval("(lambda (classes) (make <method> #:specializers classes))") or { panic(err) }
51 top := vm.lookup('<top>') or { panic(err) }
52 integer_class := vm.lookup('<integer>') or { panic(err) }
53 mut child := arena.new()
54 child.push()
55 input := cons(integer_class, top)
56 child.pop()
57 method := vm.call(proc, [input]) or { panic(err) }
58 child.free()
59 read := vm.lookup('method-specializers') or { panic(err) }
60 tuple := vm.call(read, [method]) or { panic(err) }
61 if tuple.tag != .pair || !eqv(tuple.pair_pointer().car, integer_class)
62 || !eqv(tuple.pair_pointer().cdr, top) {
63 panic('method lost its valid specializers when the input list expired')
64 }
65}
66
67fn test_goops_class_and_method_references_keep_valid_owners_and_weak_backlinks() {
68 mut root := arena.new()
69 root.push()
70 mut vm := new()
71 parent := vm.eval("(make-class '() '((x)))") or { panic(err) }
72 edit := vm.eval("(lambda (parent)
73 (let* ((local (make-class (list parent) '()))
74 (m (make <method> #:specializers (cons local parent)))
75 (d (class-slot-definition parent 'x))
76 (getter (compute-getter-method local d)))
77 (slot-set! local 'redefined parent)
78 (slot-set! m 'specializers (cons parent local))
79 (and (eq? parent (car (class-direct-supers local)))
80 (memq parent (class-precedence-list local))
81 (eq? (slot-ref local 'redefined) parent)
82 (eq? local (car (method-specializers getter)))
83 (eq? parent (car (method-specializers m)))
84 (eq? local (cdr (method-specializers m))))))") or { panic(err) }
85 root.pop()
86 mut child := arena.new()
87 child.push()
88 value := vm.call(edit, [parent]) or { panic(err) }
89 if !value.is_true() { panic('valid current-arena or VM-root reference lost its identity') }
90 child.pop()
91 root.push()
92 subclasses := vm.lookup('class-direct-subclasses') or { panic(err) }
93 live := vm.call(subclasses, [parent]) or { panic(err) }
94 if (live.list_length() or { panic(err) }) != 1 { panic('live weak subclass was hidden') }
95 root.pop()
96 child.free()
97 root.push()
98 after := vm.call(subclasses, [parent]) or { panic(err) }
99 if (after.list_length() or { panic(err) }) != 0 { panic('expired weak subclass remained visible') }
100 root.pop()
101 root.free()
102}
103
104fn test_inherited_class_slot_cell_respects_the_destination_arena() {
105 mut root := arena.new()
106 root.push()
107 mut vm := new()
108 holder := vm.eval("(define holder (make-class '() '((x #:allocation #:class #:init-value 11))))
109 (class-slot-definition holder 'x)") or { panic(err) }
110 owned_cell := holder.goops_slot_pointer().slot.shared
111 compute := vm.lookup('compute-get-n-set') or { panic(err) }
112 make_child := vm.eval("(lambda () (let ((base (make-class '() '((x #:allocation #:class #:init-value 71)))))
113 (make-class (list base) '())))") or { panic(err) }
114 valid := vm.eval("(lambda () (let* ((derived (make-class (list holder) '()))
115 (local (make-class '() '((x #:allocation #:class #:init-value 32))))
116 (access (compute-get-n-set derived (class-slot-definition local 'x))))
117 (let ((obj (make local)))
118 ((cadr access) obj 19)
119 (class-slot-ref holder 'x))))") or { panic(err) }
120 root.pop()
121
122 mut child := arena.new()
123 child.push()
124 borrowed := vm.call(make_child, []) or { panic(err) }
125 inherited := vm.call(valid, []) or { panic(err) }
126 if inherited.as_integer() or { panic(err) } != 19 { panic('inherited writes did not reach the VM-root cell') }
127 child.pop()
128
129 root.push()
130 if _ := vm.call(compute, [borrowed, holder]) {
131 panic('root slot accepted a shared cell from the child arena')
132 } else {
133 if !err.msg().contains('arena boundary') { panic(err) }
134 }
135 if voidptr(holder.goops_slot_pointer().slot.shared) != voidptr(owned_cell) {
136 panic('failed inheritance replaced the original shared cell')
137 }
138 child.free()
139 result := vm.eval("(slot-ref (make holder) 'x)") or { panic(err) }
140 if result.as_integer() or { panic(err) } != 19 { panic('failed inheritance changed the root slot') }
141 root.pop()
142 root.free()
143}
144