vile / scheme / goops_slot_metadata_test.v
251 lines · 235 sloc · 11.53 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4import reader
5
6fn slot_metadata_text(mut vm VM, source string) string {
7 value := vm.eval(source) or { panic('${source}: ${err.msg()}') }
8 return write(value) or { panic(err) }
9}
10
11fn slot_metadata_expect(actual string, expected string) {
12 if actual != expected { panic('expected ${expected}, got ${actual}') }
13}
14
15fn test_goops_slot_definitions_identity_inheritance_and_order() {
16 mut vm := new()
17 actual := slot_metadata_text(mut vm, '(import (vile goops))
18 (define-class <a> () (x #:init-value 1) (s #:allocation #:class #:init-value 2))
19 (define-class <b> (<a>) (y #:init-form (+ 1 2)))
20 (define dx (car (class-direct-slots <a>)))
21 (define ax (class-slot-definition <a> (quote x)))
22 (define bx (class-slot-definition <b> (quote x)))
23 (list (map slot-definition-name (class-direct-slots <b>))
24 (map slot-definition-name (class-slots <b>))
25 (eq? dx ax) (eq? ax bx) (eq? ax (class-slot-definition <a> (quote x)))
26 (eq? dx (car (class-direct-slots <a>)))
27 (class-name (class-of ax)) (instance? ax) (is-a? ax <slot>)
28 (class-slot-definition <a> (quote missing))
29 (slot-definition-init-value bx) (slot-ref (make <b>) (quote y)))')
30 slot_metadata_expect(actual, '((y) (x s y) #f #f #t #t <slot> #t #t #f 1 3)')
31 inherited := slot_metadata_text(mut vm, '(define-class <c> (<a>) (x #:init-value 9))
32 (list (map slot-definition-name (class-slots <c>))
33 (slot-definition-init-value (class-slot-definition <c> (quote x)))
34 (slot-definition-init-value ax))')
35 slot_metadata_expect(inherited, '((x s) 9 1)')
36 slot_metadata_expect(slot_metadata_text(mut vm, 'ax'), '#<slot x>')
37}
38
39fn test_goops_slot_initializer_forms_thunks_and_lexical_scope() {
40 mut vm := new()
41 actual := slot_metadata_text(mut vm, '(define hits 0)
42 (define <box> (let ((offset 40))
43 (define-class <local> ()
44 (x #:init-form (begin (set! hits (+ hits 1)) (+ offset hits)))
45 (y #:init-value (begin (set! hits (+ hits 10)) (list offset)))) <local>))
46 (define x (class-slot-definition <box> (quote x)))
47 (define y (class-slot-definition <box> (quote y)))
48 (define before hits)
49 (define xt (slot-definition-init-thunk x))
50 (define yt (slot-init-function <box> (quote y)))
51 (list before (slot-definition-init-form x) (xt) hits
52 (slot-ref (make <box>) (quote x)) hits
53 (eq? xt (slot-init-function <box> (quote x)))
54 (eq? (yt) (slot-definition-init-value y))
55 (eq? (slot-definition-init-value x) (slot-definition-init-form y)))')
56 slot_metadata_expect(actual, '(10 (begin (set! hits (+ hits 1)) (+ offset hits)) 51 11 52 12 #t #t #t)')
57}
58
59fn test_goops_slot_options_accessors_allocations_and_defaults() {
60 mut vm := new()
61 actual := slot_metadata_text(mut vm, '(define-generic read-x) (define-generic write-x)
62 (define-class <box> ()
63 (x #:init-keyword #:x #:init-value 7 #:getter read-x #:setter write-x #:accessor box-x)
64 (s #:allocation #:class) (e #:allocation #:each-subclass)
65 (v #:allocation #:virtual #:slot-ref (lambda (o) 1) #:slot-set! (lambda (o v) v)) (z))
66 (define x (class-slot-definition <box> (quote x)))
67 (define z (class-slot-definition <box> (quote z)))
68 (list (eq? (slot-definition-getter x) read-x)
69 (eq? (slot-definition-setter x) write-x) (eq? (slot-definition-accessor x) box-x)
70 (slot-definition-init-keyword x)
71 (map slot-definition-allocation (class-slots <box>))
72 (slot-definition-getter z) (slot-definition-setter z) (slot-definition-accessor z)
73 (slot-definition-init-keyword z) (slot-definition-init-thunk z)
74 (slot-init-function <box> (quote z))
75 (car (slot-definition-options x)) (cadr (slot-definition-options x))
76 (eq? (slot-definition-options x) (slot-definition-options x)))')
77 slot_metadata_expect(actual, '(#t #t #t #:x (#:instance #:class #:each-subclass #:virtual #:instance) #f #f #f #f #f #f #:name x #t)')
78}
79
80fn test_goops_slot_options_snapshot_spine_and_keep_initializer_alias() {
81 mut vm := new()
82 actual := slot_metadata_text(mut vm, '(define data (list 1))
83 (define spec (list (quote x) #:init-value data))
84 (define c (make-class (list <object>) (list spec)))
85 (define d (class-slot-definition c (quote x)))
86 (set-car! (cddr spec) 99)
87 (set-car! data 42)
88 (list (slot-definition-name d) (slot-definition-init-value d)
89 ((slot-definition-init-thunk d))
90 (eq? (slot-definition-init-value d) data)
91 (cadr (memq #:init-value (slot-definition-options d))))')
92 slot_metadata_expect(actual, '(x (42) (42) #t (42))')
93}
94
95fn test_goops_public_unbound_marker_is_data_and_unbinds_slots() {
96 mut vm := new()
97 actual := slot_metadata_text(mut vm, '(define-class <box> () (x) (s #:allocation #:class)
98 (v #:allocation #:virtual #:slot-ref (lambda (o) marker) #:slot-set! (lambda (o v) v)))
99 (define d (class-slot-definition <box> (quote x)))
100 (define marker (slot-definition-init-value d))
101 (define obj (make <box>))
102 (define-method (slot-unbound (o <object>)) (quote instance))
103 (define-method (slot-unbound (c <class>) (o <object>) n) n)
104 (define-method (slot-unbound (c <class>) n) n)
105 (define-method (slot-missing (c <class>) (o <object>) n) marker)
106 (slot-set! obj (quote x) marker) (slot-set! obj (quote s) marker)
107 (list marker (eq? marker (slot-definition-init-form d))
108 (eq? marker (class-name (make-class (quote ()) (quote ()))))
109 (slot-bound? obj (quote x)) (slot-ref obj (quote x))
110 (slot-bound? obj (quote s)) (slot-ref obj (quote s))
111 (class-slot-ref <box> (quote s))
112 (slot-bound? obj (quote v)) (slot-ref obj (quote v))
113 (slot-bound? obj (quote missing)) (slot-ref obj (quote missing))
114 (begin (slot-set! obj (quote x) (list (quote unbound))) (slot-bound? obj (quote x))))')
115 slot_metadata_expect(actual, '((unbound) #t #t #f instance #f s s #f v #f missing #t)')
116 if _ := vm.eval('(set-car! marker 1)') { panic('mutated public unbound marker') }
117}
118
119fn test_goops_slot_definition_errors_and_cyclic_spec_recovery() {
120 mut vm := new()
121 slot_metadata_text(mut vm, '(define-class <box> () (x))')
122 for source in [
123 '(class-slots 1)',
124 '(class-direct-slots <box> 1)',
125 '(class-slot-definition <box> 1)',
126 '(slot-init-function <box> (quote absent))',
127 '(slot-definition-name <box>)',
128 '(slot-definition-options 1)',
129 '(slot-definition-init-value)',
130 '(slot-definition-init-thunk 1 2)',
131 '(make-class (list <object>) (quote ((x #:init-thunk 42))))',
132 '(let ((s (list (quote x) #:init-value 1))) (set-cdr! (cddr s) s) (make-class (list <object>) (list s)))',
133 ] {
134 if _ := vm.eval(source) { panic('accepted invalid slot metadata: ${source}') }
135 }
136 slot_metadata_expect(slot_metadata_text(mut vm, '(slot-definition-name (car (class-slots <box>)))'), 'x')
137}
138
139fn test_goops_slot_definition_foreign_vm_rejection() {
140 mut first := new()
141 mut second := new()
142 definition := first.eval('(define-class <box> () (x)) (car (class-slots <box>))') or { panic(err) }
143 for name in ['class-of', 'instance?', 'slot-definition-name', 'slot-definition-options',
144 'slot-definition-allocation', 'slot-definition-getter', 'slot-definition-setter',
145 'slot-definition-accessor', 'slot-definition-init-value', 'slot-definition-init-form',
146 'slot-definition-init-thunk', 'slot-definition-init-keyword'] {
147 operation := second.lookup(name) or { panic(err) }
148 if _ := second.call(operation, [definition]) { panic('accepted foreign slot in ${name}') }
149 }
150}
151
152fn test_goops_slot_metadata_exports_and_both_frontends() {
153 mut vm := new()
154 actual := slot_metadata_text(mut vm, '(define-library (meta slots)
155 (export c) (import (scheme base) (vile goops))
156 (begin (define-class c () (x #:init-value 42))))
157 (import (meta slots) (vile goops))
158 (slot-definition-init-value (class-slot-definition c (quote x)))')
159 slot_metadata_expect(actual, '42')
160 result := vm.eval_alt('import((vile,goops))
161 slot-definition-init-value(class-slot-definition(c, quote(x)))') or { panic(err) }
162 if result.whole() != 42 { panic('unexpected slot result') }
163}
164
165fn test_goops_slot_handle_retains_anonymous_class_and_initializer() {
166 mut vm := new()
167 slot_metadata_text(mut vm, '(define d (let ((v (list 41)))
168 (define-class <ephemeral> () (x #:init-form (+ 1 (car v))))
169 (class-slot-definition <ephemeral> (quote x))))')
170 for _ in 0 .. 100 {
171 gc_collect()
172 slot_metadata_expect(slot_metadata_text(mut vm, '((slot-definition-init-thunk d))'), '42')
173 slot_metadata_expect(slot_metadata_text(mut vm, '(slot-definition-name d)'), 'x')
174 }
175}
176
177fn test_goops_slot_property_queries_keep_bounded_arena_usage() {
178 mut region := arena.new()
179 defer { region.free() }
180 region.push()
181 mut vm := new_with_options(Options{ max_steps: 0, max_frames: 0 }) or { panic(err) }
182 slot_metadata_text(mut vm, '(define-class <box> () (x #:init-value 42 #:init-keyword #:x))
183 (define d (class-slot-definition <box> (quote x)))
184 (define (loop n) (if (= n 0) (slot-definition-init-value d)
185 (begin (slot-definition-name d) (slot-definition-options d)
186 (slot-definition-allocation d) (slot-definition-init-keyword d)
187 (slot-definition-init-thunk d) (class-slot-definition <box> (quote x))
188 (loop (- n 1)))))')
189 vm.options = Options{ ...vm.options, max_frames: 8 }
190 proc := vm.lookup('loop') or { panic(err) }
191 _ = vm.call(proc, [integer(100)]) or { panic(err) }
192 before := region.used()
193 result := vm.call(proc, [integer(50000)]) or { panic(err) }
194 delta := region.used() - before
195 region.pop()
196 if result.whole() != 42 { panic('unexpected slot result') }
197 if delta > 65536 { panic('slot queries allocated ${delta} arena bytes') }
198}
199
200fn test_goops_slot_options_have_separate_direct_effective_and_inherited_spines() {
201 mut vm := new()
202 actual := slot_metadata_text(mut vm, '(define-class <a> () (x #:init-value (list 1)))
203 (define-class <b> (<a>))
204 (define d (car (class-direct-slots <a>)))
205 (define a (class-slot-definition <a> (quote x)))
206 (define b (class-slot-definition <b> (quote x)))
207 (define before (list (eq? (slot-definition-options d) (slot-definition-options a))
208 (eq? (slot-definition-options a) (slot-definition-options b))
209 (eq? (slot-definition-init-thunk a) (slot-definition-init-thunk b))))
210 (set-car! (cdr (slot-definition-options a)) (quote changed))
211 (list before (cadr (slot-definition-options d)) (cadr (slot-definition-options a))
212 (cadr (slot-definition-options b)) (slot-definition-name a)
213 (eq? (slot-definition-init-value d) (slot-definition-init-value b)))')
214 slot_metadata_expect(actual, '((#f #f #t) x changed x x #t)')
215}
216
217fn test_goops_slot_queries_in_borrowed_arena_do_not_publish_temporary_metadata() {
218 mut owner := arena.new()
219 defer { owner.free() }
220 owner.push()
221 mut vm := new()
222 slot_metadata_text(mut vm, '(define-class <box> () (x #:init-value 42))')
223 class := vm.lookup('<box>') or { panic(err) }
224 lookup := vm.lookup('class-slot-definition') or { panic(err) }
225 name := symbol('x', reader.ReadOptions{}) or { panic(err) }
226 property := vm.lookup('slot-definition-init-value') or { panic(err) }
227 owner.pop()
228 mut temporary := arena.new()
229 temporary.push()
230 definition := vm.call(lookup, [class, name]) or { panic(err) }
231 if !owner.owns(definition.pointer()) || temporary.owns(definition.pointer()) {
232 panic('metadata lookup created a temporary slot handle')
233 }
234 temporary.pop()
235 temporary.free()
236 owner.push()
237 result := vm.call(property, [definition]) or { panic(err) }
238 owner.pop()
239 if result.whole() != 42 { panic('slot metadata did not survive borrowed arena') }
240}
241
242fn test_goops_bootstrap_slot_metadata_and_empty_class_lists() {
243 mut vm := new()
244 actual := slot_metadata_text(mut vm, '(define d (car (class-direct-slots <applicable-struct>)))
245 (define e (car (class-slots <applicable-struct>)))
246 (list (class-direct-slots <object>) (class-slots <top>)
247 (slot-definition-name d) (slot-definition-name e) (eq? d e)
248 (slot-definition-allocation d) (slot-definition-init-keyword d)
249 (slot-definition-init-thunk d) (slot-definition-init-value d))')
250 slot_metadata_expect(actual, '(() () procedure procedure #f #:instance #:procedure #f (unbound))')
251}
252