vile / scheme / goops_virtual_test.v
237 lines · 223 sloc · 9.17 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn virtual_text(mut vm VM, source string) string {
6 value := vm.eval(source) or { panic('${source}: ${err.msg()}') }
7 return write(value) or { panic(err) }
8}
9
10fn test_goops_virtual_slots_compute_values_without_instance_fields() {
11 mut vm := new()
12 actual := virtual_text(mut vm, '(import (vile goops))
13 (define-class <box> ()
14 (real #:init-value 2)
15 (double #:allocation #:virtual #:init-keyword #:double
16 #:slot-ref (lambda (o) (* 2 (slot-ref o (quote real))))
17 #:slot-set! (lambda (o v) (slot-set! o (quote real) (/ v 2)))))
18 (define b (make <box> #:double 20))
19 (define before (list (slot-ref b (quote real))
20 (slot-ref b (quote double)) (slot-bound? b (quote double))))
21 (slot-set! b (quote double) 8)
22 (list before (slot-ref b (quote real)) (slot-ref b (quote double)))')
23 if actual != '((10 20 #t) 4 8)' { panic(actual) }
24 class := vm.lookup('<box>') or { panic(err) }
25 if class.goops_class_pointer().instance_fields != 1 {
26 panic('virtual slot reserved instance storage')
27 }
28}
29
30fn test_goops_virtual_initializers_use_setter_once_per_instance() {
31 mut vm := new()
32 actual := virtual_text(mut vm, '(import (vile goops))
33 (define calls 0)
34 (define-class <base> ()
35 (actual #:init-value 1)
36 (virtual #:allocation #:virtual #:init-form 7
37 #:slot-ref (lambda (o) (slot-ref o (quote actual)))
38 #:slot-set! (lambda (o v)
39 (set! calls (+ calls 1)) (slot-set! o (quote actual) v))))
40 (define-class <child> (<base>))
41 (define a (make <base>))
42 (define b (make <child>))
43 (list calls (slot-ref a (quote virtual)) (slot-ref b (quote virtual)))')
44 if actual != '(2 7 7)' { panic(actual) }
45}
46
47fn test_goops_virtual_init_value_and_nonvirtual_callback_options() {
48 mut vm := new()
49 actual := virtual_text(mut vm, '(import (vile goops))
50 (define writes 0)
51 (define-class <mixed> ()
52 (real #:init-value 1 #:slot-ref 3 #:slot-set! 4)
53 (computed #:allocation #:virtual #:init-value 8
54 #:slot-ref (lambda (o) (* 2 (slot-ref o (quote real))))
55 #:slot-set! (lambda (o v)
56 (set! writes (+ writes 1))
57 (slot-set! o (quote real) (/ v 2)))))
58 (define a (make <mixed>))
59 (define b (make <mixed>))
60 (list writes (slot-ref a (quote computed))
61 (slot-ref b (quote real)))')
62 if actual != '(2 8 4)' { panic(actual) }
63}
64
65fn test_goops_virtual_setter_result_matches_callback_result() {
66 mut vm := new()
67 actual := virtual_text(mut vm, '(import (vile goops))
68 (define-class <result> ()
69 (x #:allocation #:virtual
70 #:slot-ref (lambda (o) 1)
71 #:slot-set! (lambda (o v) (quote callback-result))))
72 (slot-set! (make <result>) (quote x) 42)')
73 if actual != 'callback-result' { panic(actual) }
74}
75
76fn test_goops_virtual_hot_calls_keep_bounded_arena_usage() {
77 mut region := arena.new()
78 defer { region.free() }
79 region.push()
80 mut vm := new_with_options(Options{ max_steps: 0, max_frames: 0 }) or { panic(err) }
81 virtual_text(mut vm, '(import (vile goops))
82 (define-class <hot> () (x #:init-value 1)
83 (y #:allocation #:virtual
84 #:slot-ref (lambda (o) (slot-ref o (quote x)))
85 #:slot-set! (lambda (o v) (slot-set! o (quote x) v))))
86 (define obj (make <hot>))
87 (define (hot-read n acc)
88 (if (= n 0) acc (hot-read (- n 1) (+ acc (slot-ref obj (quote y))))))
89 (define (hot-write n)
90 (if (= n 0) (slot-ref obj (quote x))
91 (begin (slot-set! obj (quote y) n) (hot-write (- n 1)))))')
92 vm.options = Options{ ...vm.options, max_frames: 4 }
93 read_proc := vm.lookup('hot-read') or { panic(err) }
94 write_proc := vm.lookup('hot-write') or { panic(err) }
95 _ = vm.call(read_proc, [integer(100), integer(0)]) or { panic(err) }
96 _ = vm.call(write_proc, [integer(100)]) or { panic(err) }
97 before := region.used()
98 read_result := vm.call(read_proc, [integer(50000), integer(0)]) or { panic(err) }
99 write_result := vm.call(write_proc, [integer(50000)]) or { panic(err) }
100 delta := region.used() - before
101 region.pop()
102 if read_result.whole() != 50000 || write_result.whole() != 1 || delta > 65536 {
103 panic('virtual slot calls grew the arena by ${delta} bytes')
104 }
105}
106
107fn test_goops_virtual_slot_bound_uses_current_machine_and_bounded_arena() {
108 mut region := arena.new()
109 defer { region.free() }
110 region.push()
111 mut vm := new_with_options(Options{ max_steps: 0, max_frames: 0 }) or { panic(err) }
112 virtual_text(mut vm, '(import (vile goops))
113 (define-class <bound-hot> () (x #:init-value 1)
114 (y #:allocation #:virtual
115 #:slot-ref (lambda (o) (slot-ref o (quote x)))
116 #:slot-set! (lambda (o v) (slot-set! o (quote x) v))))
117 (define bound-object (make <bound-hot>))
118 (define (hot-bound n acc)
119 (if (= n 0) acc
120 (hot-bound (- n 1) (+ acc (if (slot-bound? bound-object (quote y)) 1 0)))))
121 (define (tail-bound n)
122 (if (= n 0) (slot-bound? bound-object (quote y)) (tail-bound (- n 1))))')
123 vm.options = Options{ ...vm.options, max_frames: 6 }
124 hot := vm.lookup('hot-bound') or { panic(err) }
125 tail := vm.lookup('tail-bound') or { panic(err) }
126 _ = vm.call(hot, [integer(100), integer(0)]) or { panic(err) }
127 before := region.used()
128 result := vm.call(hot, [integer(50000), integer(0)]) or { panic(err) }
129 tail_result := vm.call(tail, [integer(50000)]) or { panic(err) }
130 delta := region.used() - before
131 region.pop()
132 if result.whole() != 50000 || tail_result.tag != .boolean || !tail_result.is_true()
133 || delta > 65536 {
134 panic('virtual slot-bound? grew the arena by ${delta} bytes')
135 }
136}
137
138fn test_goops_virtual_slot_bound_continuation_reentry_finishes_bridge() {
139 mut vm := new()
140 first := virtual_text(mut vm, '(import (vile goops))
141 (define saved #f)
142 (define-class <bound-reentry> ()
143 (x #:allocation #:virtual
144 #:slot-ref (lambda (o) (call/cc (lambda (k) (set! saved k) 1)))
145 #:slot-set! (lambda (o v) v)))
146 (define bound-object (make <bound-reentry>))
147 (slot-bound? bound-object (quote x))')
148 if first != '#t' { panic(first) }
149 saved := vm.lookup('saved') or { panic(err) }
150 reentered := vm.call(saved, [integer(2)]) or { panic(err) }
151 if reentered.tag != .boolean || !reentered.is_true() {
152 panic('virtual slot-bound? continuation skipped its bridge')
153 }
154}
155
156fn test_goops_virtual_slot_bound_callback_failure_recovers_machine() {
157 mut vm := new()
158 virtual_text(mut vm, '(import (vile goops))
159 (define-class <bound-failure> ()
160 (x #:allocation #:virtual
161 #:slot-ref (lambda (o) (/ 1 0))
162 #:slot-set! (lambda (o v) v)))
163 (define bound-object (make <bound-failure>))')
164 if _ := vm.eval('(slot-bound? bound-object (quote x))') {
165 panic('accepted virtual slot-bound? getter failure')
166 }
167 if virtual_text(mut vm, '(+ 1 2)') != '3' { panic('VM did not recover after getter failure') }
168}
169
170fn test_goops_virtual_slot_direct_make_class_and_vile_frontend() {
171 mut vm := new()
172 actual := virtual_text(mut vm, '(import (vile goops))
173 (define <direct> (make-class (list <object>)
174 (list (list (quote x) #:init-value 4)
175 (list (quote y) #:allocation #:virtual
176 #:slot-ref (lambda (o) (+ 1 (slot-ref o (quote x))))
177 #:slot-set! (lambda (o v) (slot-set! o (quote x) (- v 1)))))
178 #:name (quote <direct>)))
179 (define obj (make <direct>))
180 (slot-ref obj (quote y))')
181 if actual != '5' { panic(actual) }
182 value := vm.eval_alt('slot-set!(obj, quote(y), 12)
183 slot-ref(obj, quote(y))') or { panic(err) }
184 if value.whole() != 12 { panic('Vile virtual slot failed') }
185 bound := vm.eval_alt('slot-bound?(obj, quote(y))') or { panic(err) }
186 if bound.tag != .boolean || !bound.is_true() {
187 panic('Vile virtual slot-bound? failed')
188 }
189}
190
191fn test_goops_virtual_procedure_slot_remains_callable() {
192 mut vm := new()
193 actual := virtual_text(mut vm, '(import (vile goops))
194 (define-class <callable> (<applicable-struct>)
195 (target #:init-value (lambda (x) (+ x 3)))
196 (procedure #:allocation #:virtual
197 #:slot-ref (lambda (o) (slot-ref o (quote target)))
198 #:slot-set! (lambda (o v) (slot-set! o (quote target) v))))
199 (define f (make <callable>))
200 (list (f 4) (begin (slot-set! f (quote procedure) (lambda (x) (+ x 5))) (f 4)))')
201 if actual != '(7 9)' { panic(actual) }
202}
203
204fn test_goops_virtual_callbacks_keep_continuation_and_reentry_semantics() {
205 mut vm := new()
206 actual := virtual_text(mut vm, '(import (vile goops))
207 (define jump #f)
208 (define-class <jumping> ()
209 (x #:allocation #:virtual
210 #:slot-ref (lambda (o) (jump 41))
211 #:slot-set! (lambda (o v) (jump v))))
212 (define obj (make <jumping>))
213 (list (call/cc (lambda (k) (set! jump k)
214 (slot-ref obj (quote x)) 0))
215 (call/cc (lambda (k) (set! jump k)
216 (slot-set! obj (quote x) 42) 0)))')
217 if actual != '(41 42)' { panic(actual) }
218}
219
220fn test_goops_virtual_slot_rejects_missing_callbacks_and_recovers() {
221 mut vm := new()
222 virtual_text(mut vm, '(import (vile goops))')
223 for source in [
224 '(define-class <bad> () (x #:allocation #:virtual))',
225 '(define-class <bad> () (x #:allocation #:virtual #:slot-ref (lambda (o) 1)))',
226 '(define-class <bad> () (x #:allocation #:virtual #:slot-ref 1 #:slot-set! (lambda (o v) v)))',
227 '(define-class <bad> () (x #:allocation #:virtual #:slot-ref (lambda (o) 1) #:slot-set! #f))',
228 '(define-class <bad> () (x #:allocation #:virtual #:slot-ref (lambda (o) 1) #:slot-set! 42))',
229 '(define-class <bad> () (x #:allocation #:virtual #:slot-ref (begin) #:slot-ref (lambda (o) 2) #:slot-set! (lambda (o v) v)))',
230 ] {
231 if _ := vm.eval(source) { panic('accepted invalid virtual slot: ${source}') }
232 }
233 if virtual_text(mut vm, '(define-class <good> () (x #:init-value 3))
234 (slot-ref (make <good>) (quote x))') != '3' {
235 panic('VM did not recover')
236 }
237}
238