vile / scheme / goops_construction_test.v
256 lines · 236 sloc · 11.26 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn construction_text(mut vm VM, source string) string {
6 result := vm.eval(source) or { panic('${source}: ${err.msg()}') }
7 return write(result) or { panic(err) }
8}
9
10fn construction_expect(actual string, expected string) {
11 if actual != expected { panic('expected ${expected}, got ${actual}') }
12}
13
14fn test_goops_raw_slot_constructor_defaults_and_options() {
15 mut vm := new()
16 actual := construction_text(mut vm, '(import (vile goops))
17 (define s (make <slot>))
18 (define named (make <slot> #:name (quote x) #:init-value 42))
19 (list (class-name (class-of s)) (instance? s)
20 (slot-definition-name s) (slot-definition-allocation s)
21 (slot-definition-init-value s) (slot-definition-init-form s)
22 (slot-definition-init-thunk s) (slot-definition-init-keyword s)
23 (slot-definition-getter s) (slot-definition-setter s) (slot-definition-accessor s)
24 (slot-definition-options s) (slot-definition-init-thunk named)
25 (slot-definition-options named))')
26 construction_expect(actual, '(<slot> #t (unbound) #:instance (unbound) (unbound) #f #f #f #f #f () #f (#:name x #:init-value 42))')
27}
28
29fn test_goops_raw_slot_options_first_keyword_and_extension_metadata() {
30 mut vm := new()
31 actual := construction_text(mut vm, '(define data (list 1))
32 (define s (make <slot> #:name 123 #:custom data #:init-value 1 #:init-value 2
33 #:getter 7 #:setter #f #:accessor (if #f 1)))
34 (set-car! data 42)
35 (list (slot-definition-name s) (slot-definition-init-value s)
36 (slot-definition-getter s) (slot-definition-setter s) (slot-definition-accessor s)
37 (slot-definition-options s))')
38 construction_expect(actual, '(123 1 7 #f #<unspecified> (#:name 123 #:custom (42) #:init-value 1 #:init-value 2 #:getter 7 #:setter #f #:accessor #<unspecified>))')
39}
40
41fn test_goops_raw_class_constructor_defaults_and_explicit_layout() {
42 mut vm := new()
43 actual := construction_text(mut vm, '(define c (make <class>))
44 (define explicit (make <class> #:name (quote <box>) #:dsupers (list <object>)
45 #:slots (quote (x (y #:init-value 42)))))
46 (define o (make explicit))
47 (list (class-name c) (map class-name (class-direct-supers c))
48 (map class-name (class-precedence-list c)) (class-slots c)
49 (class-name (class-of c)) (instance? c)
50 (class-name explicit) (map slot-definition-name (class-slots explicit))
51 (slot-bound? o (quote x)) (slot-ref o (quote y)))')
52 construction_expect(actual, '(??? () (???) () <class> #t <box> (x y) #f 42)')
53}
54
55fn test_goops_prebuilt_direct_slot_identity_effective_copy_and_inheritance() {
56 mut vm := new()
57 actual := construction_text(mut vm, '(define s (make <slot> #:name (quote x) #:init-thunk (lambda () 42)))
58 (define c (make-class (list <object>) (list s)))
59 (define child (make-class (list c) (quote ())))
60 (define direct (car (class-direct-slots c)))
61 (define effective (car (class-slots c)))
62 (list (eq? s direct) (eq? direct effective)
63 (eq? effective (car (class-slots child)))
64 (eq? (slot-definition-options direct) (slot-definition-options effective))
65 (slot-ref (make c) (quote x)) (slot-ref (make child) (quote x)))')
66 construction_expect(actual, '(#t #f #f #f 42 42)')
67}
68
69fn test_goops_prebuilt_init_value_without_thunk_leaves_instance_slot_unbound() {
70 mut vm := new()
71 actual := construction_text(mut vm, '(define s (make <slot> #:name (quote x) #:init-value 42 #:init-keyword #:x))
72 (define c (make <class> #:dsupers (list <object>) #:slots (list s)))
73 (define inherited (make-class (list c) (quote ())))
74 (list (slot-definition-init-value s)
75 (slot-definition-init-value (car (class-slots c)))
76 (slot-definition-init-thunk (car (class-slots c)))
77 (slot-bound? (make c) (quote x)) (slot-bound? (make inherited) (quote x))
78 (slot-ref (make c #:x 9) (quote x)))')
79 construction_expect(actual, '(42 42 #f #f #f 9)')
80}
81
82fn test_goops_prebuilt_shared_slots_allocate_cells_for_each_defining_class() {
83 mut vm := new()
84 actual := construction_text(mut vm, '(define s (make <slot> #:name (quote x) #:allocation #:class #:init-value 42))
85 (define a (make-class (list <object>) (list s)))
86 (define b (make-class (list <object>) (list s)))
87 (define child (make-class (list a) (quote ())))
88 (class-slot-set! a (quote x) 7)
89 (list (eq? s (car (class-direct-slots a))) (eq? s (car (class-direct-slots b)))
90 (class-slot-ref a (quote x)) (class-slot-ref b (quote x))
91 (class-slot-ref child (quote x)))')
92 construction_expect(actual, '(#t #t 7 42 7)')
93}
94
95fn test_goops_prebuilt_each_subclass_thunks_and_direct_reuse() {
96 mut vm := new()
97 actual := construction_text(mut vm, '(define count 0)
98 (define s (make <slot> #:name (quote x) #:allocation #:each-subclass
99 #:init-thunk (lambda () (set! count (+ count 1)) count)))
100 (define a (make-class (list <object>) (list s)))
101 (define b (make-class (list a) (quote ())))
102 (define c (make-class (list <object>) (list s)))
103 (list count (class-slot-ref a (quote x)) (class-slot-ref b (quote x)) (class-slot-ref c (quote x)))')
104 construction_expect(actual, '(3 1 2 3)')
105}
106
107fn test_goops_prebuilt_virtual_slots_use_recorded_options_and_initializer() {
108 mut vm := new()
109 actual := construction_text(mut vm, '(define data 1) (define writes 0)
110 (define s (make <slot> #:name (quote v) #:allocation #:virtual #:init-value 42
111 #:slot-ref (lambda (o) data)
112 #:slot-set! (lambda (o v) (set! writes (+ writes 1)) (set! data v))))
113 (define c (make-class (list <object>) (list s)))
114 (define o (make c))
115 (define before (list writes (slot-ref o (quote v))))
116 (slot-set! o (quote v) 9)
117 (define t (make <slot> #:name (quote v) #:allocation #:virtual #:init-thunk (lambda () 44)
118 #:slot-ref (lambda (o) data) #:slot-set! (lambda (o v) (set! data v))))
119 (define d (make-class (list <object>) (list t)))
120 (list before writes (slot-ref (make d) (quote v)))')
121 construction_expect(actual, '((0 1) 1 44)')
122}
123
124fn test_goops_prebuilt_slot_accessors_extend_existing_generics() {
125 mut vm := new()
126 actual := construction_text(mut vm, '(define accessor (make-accessor (quote value)))
127 (define s (make <slot> #:name (quote x) #:accessor accessor #:init-thunk (lambda () 42)))
128 (define c (make-class (list <object>) (list s)))
129 (define o (make c))
130 (list (accessor o) ((setter accessor) o 9) (accessor o)
131 (eq? (slot-definition-accessor (car (class-slots c))) accessor))')
132 construction_expect(actual, '(42 9 9 #t)')
133}
134
135fn test_goops_slot_construction_retains_lexical_initializers_under_gc() {
136 mut vm := new()
137 construction_text(mut vm, '(define s (let ((value (list 41)))
138 (make <slot> #:name (quote x) #:init-form (quote (+ 1 (car value)))
139 #:init-thunk (lambda () (+ 1 (car value))))))
140 (define c (make-class (list <object>) (list s)))')
141 for _ in 0 .. 50 {
142 gc_collect()
143 construction_expect(construction_text(mut vm, '(slot-ref (make c) (quote x))'), '42')
144 }
145}
146
147fn test_goops_construction_errors_and_recovery() {
148 mut vm := new()
149 for source in [
150 '(make <slot> #:name)',
151 '(make <slot> 1 2)',
152 '(make <slot> #:init-thunk 1)',
153 '(make <slot> #:init-thunk (lambda (x) x))',
154 '(make <slot> #:init-thunk (case-lambda ((x) x)))',
155 '(make <class> #:slots)',
156 '(make <class> #:dsupers 1)',
157 '(make-class (list <object>) (list (make <slot>)))',
158 '(make-class (list <object>) (list (make <slot> #:name (quote x) #:allocation #:unknown)))',
159 '(make-class (list <object>) (list (make <slot> #:name (quote x) #:getter 1)))',
160 '(let ((s (make <slot> #:name (quote x)))) (make-class (list <object>) (list s s)))',
161 '(let ((slots (list (make <slot> #:name (quote x))))) (set-cdr! slots slots) (make <class> #:slots slots))',
162 ] {
163 if _ := vm.eval(source) { panic('accepted invalid constructor: ${source}') }
164 }
165 construction_expect(construction_text(mut vm, '(slot-definition-name (make <slot> #:name (quote ok) #:init-thunk (case-lambda (() 1) ((x) x))))'), 'ok')
166}
167
168fn test_goops_construction_rejects_foreign_slot_and_class_references() {
169 mut first := new()
170 mut second := new()
171 slot := first.eval('(make <slot> #:name (quote x))') or { panic(err) }
172 make_class := second.lookup('make-class') or { panic(err) }
173 object := second.lookup('<object>') or { panic(err) }
174 if _ := second.call(make_class, [list([object]), list([slot])]) {
175 panic('accepted foreign slot')
176 }
177 slot_class := second.lookup('<slot>') or { panic(err) }
178 make := second.lookup('make') or { panic(err) }
179 thunk := first.eval('(lambda () 42)') or { panic(err) }
180 if _ := second.call(make, [slot_class, keyword('init-thunk') or { panic(err) }, thunk]) {
181 panic('accepted foreign initializer')
182 }
183}
184
185fn test_goops_construction_in_vile_and_r7_library_exports() {
186 mut vm := new()
187 construction_expect(construction_text(mut vm, '(define-library (meta constructors)
188 (export slot-class class-class object-class)
189 (import (scheme base) (vile goops))
190 (begin (define slot-class <slot>) (define class-class <class>) (define object-class <object>)))
191 (import (meta constructors) (vile goops))
192 (class-name slot-class)'), '<slot>')
193 value := vm.eval_alt('s := make(slot-class, #:name, quote(x), #:init-thunk, fn() { 42 })
194 c := make(class-class, #:name, quote(box), #:dsupers, list(object-class), #:slots, list(s))
195 slot-ref(make(c), quote(x))') or { panic(err) }
196 if value.whole() != 42 { panic('Vile constructor result') }
197}
198
199fn test_goops_prebuilt_slot_can_outlive_class_created_in_temporary_arena() {
200 mut owner := arena.new()
201 defer { owner.free() }
202 owner.push()
203 mut vm := new()
204 slot := vm.eval('(make <slot> #:name (quote x) #:init-thunk (lambda () 42))') or { panic(err) }
205 make_class := vm.lookup('make-class') or { panic(err) }
206 object := vm.lookup('<object>') or { panic(err) }
207 property := vm.lookup('slot-definition-name') or { panic(err) }
208 owner.pop()
209 mut temporary := arena.new()
210 temporary.push()
211 class := vm.call(make_class, [list([object]), list([slot])]) or { panic(err) }
212 if !temporary.owns(class.pointer()) { panic('class escaped its allocation region') }
213 temporary.pop()
214 temporary.free()
215 owner.push()
216 result := vm.call(property, [slot]) or { panic(err) }
217 owner.pop()
218 if result.tag != .symbol || result.text() != 'x' {
219 panic('construction mutated original slot owner')
220 }
221}
222
223fn test_goops_prebuilt_shared_initializer_can_exit_and_reenter_host() {
224 mut vm := new()
225 vm.register('host-initialize', fn (mut nested VM, _args []Value) !Value {
226 return nested.eval('(+ 40 2)')
227 }) or { panic(err) }
228 actual := construction_text(mut vm, '(define escape #f)
229 (define s (make <slot> #:name (quote x) #:allocation #:class
230 #:init-thunk (lambda () (escape 9))))
231 (define escaped (call/cc (lambda (k) (set! escape k)
232 (make-class (list <object>) (list s)) 0)))
233 (define t (make <slot> #:name (quote x) #:allocation #:class #:init-thunk host-initialize))
234 (define c (make <class> #:dsupers (list <object>) #:slots (list t)))
235 (list escaped (class-slot-ref c (quote x)))')
236 construction_expect(actual, '(9 42)')
237}
238
239fn test_goops_raw_class_keeps_non_symbol_and_unspecified_names() {
240 mut vm := new()
241 actual := construction_text(mut vm, '(list
242 (class-name (make <class> #:name 123 #:name 456))
243 (class-name (make <class> #:name (if #f 1)))
244 (class-name (make <class> #:custom 9)))')
245 construction_expect(actual, '(123 #<unspecified> ???)')
246}
247
248fn test_goops_class_construction_keeps_non_lexical_symbol_names() {
249 mut vm := new()
250 actual := construction_text(mut vm, '(define name (string->symbol "has spaces"))
251 (define a (make-class (quote ()) (quote ()) #:name name))
252 (define b (make <class> #:name name))
253 (list (eq? name (class-name a)) (eq? name (class-name b))
254 (symbol->string (class-name a)))')
255 construction_expect(actual, '(#t #t "has spaces")')
256}
257