vile / scheme / goops_slot_hooks_test.v
152 lines · 141 sloc · 6.23 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn slot_hook_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_slot_missing_read_write_bound_and_exists() {
11 mut vm := new()
12 actual := slot_hook_text(mut vm, '(import (vile goops))
13 (define-class <box> () (x #:init-value 1))
14 (define obj (make <box>))
15 (define calls 0)
16 (define-method (slot-missing (c <class>) (o <object>) name)
17 (set! calls (+ calls 1))
18 (list (eq? c <box>) (eq? o obj) name))
19 (define before (list (slot-ref obj (quote absent))
20 (slot-set! obj (quote absent) 9)
21 (slot-bound? obj (quote absent)) (slot-exists? obj (quote absent)) calls))
22 (define-method (slot-missing (c <class>) (o <object>) name value)
23 (list name value))
24 (list before (slot-set! obj (quote absent) 7) (slot-ref obj (quote x)))')
25 assert actual == '(((#t #t absent) (#t #t absent) #t #f 3) (absent 7) 1)'
26}
27
28fn test_goops_slot_unbound_instance_shared_accessor_and_class_signatures() {
29 mut vm := new()
30 actual := slot_hook_text(mut vm, '(import (vile goops))
31 (define-class <box> () (x #:accessor box-x) (s #:allocation #:class #:accessor box-s))
32 (define obj (make <box>))
33 (define-method (slot-unbound (o <object>)) (quote instance))
34 (define-method (slot-unbound (c <class>) (o <object>) name) (list (quote shared) name))
35 (define-method (slot-unbound (c <class>) name) (list (quote class) name))
36 (list (slot-ref obj (quote x)) (box-x obj) (box-s obj)
37 (class-slot-ref <box> (quote s)) (slot-bound? obj (quote x)) (slot-bound? obj (quote s)))')
38 assert actual == '(instance instance (shared s) (class s) #f #f)'
39}
40
41fn test_goops_class_slot_missing_hook_and_non_object_slot_requests() {
42 mut vm := new()
43 actual := slot_hook_text(mut vm, '(import (vile goops))
44 (define-class <box> () (x #:init-value 1))
45 (define-method (slot-missing (c <class>) name) (list (class-name c) name))
46 (define-method (slot-missing (c <class>) (o <integer>) name) (+ o 1))
47 (list (class-slot-ref <box> (quote absent))
48 (class-slot-set! <box> (quote absent) 9)
49 (class-slot-ref <box> (quote x))
50 (slot-ref 41 (quote absent)) (slot-bound? 41 (quote absent))
51 (slot-exists? 41 (quote absent)))')
52 assert actual == '((<box> absent) (<box> absent) (<box> x) 42 #t #f)'
53}
54
55fn test_goops_missing_bound_converts_false_hook_result_to_true() {
56 mut vm := new()
57 actual := slot_hook_text(mut vm, '(define-class <box> ())
58 (define obj (make <box>))
59 (define-method (slot-missing (c <class>) (o <object>) name) #f)
60 (list (slot-ref obj (quote missing)) (slot-bound? obj (quote missing)))')
61 assert actual == '(#f #t)'
62}
63
64fn test_goops_slot_hooks_continuation_exit_and_replay() {
65 mut vm := new()
66 actual := slot_hook_text(mut vm, '(define-class <box> ())
67 (define obj (make <box>)) (define saved #f)
68 (define-method (slot-missing (c <class>) (o <object>) name)
69 (call/cc (lambda (k) (set! saved k) 1)))
70 (slot-bound? obj (quote missing))')
71 assert actual == '#t'
72 saved := vm.lookup('saved') or { panic(err) }
73 for _ in 0 .. 3 {
74 result := vm.call(saved, [boolean(false)]) or { panic(err) }
75 assert result.tag == .boolean && result.is_true()
76 }
77 actual_exit := slot_hook_text(mut vm, '(define escape #f)
78 (define-method (slot-missing (c <class>) (o <object>) name) (escape 42))
79 (call/cc (lambda (k) (set! escape k) (slot-ref obj (quote missing)) 0))')
80 assert actual_exit == '42'
81}
82
83fn test_goops_slot_hooks_defaults_errors_and_recovery() {
84 mut vm := new()
85 slot_hook_text(mut vm, '(define-class <box> () (x)) (define obj (make <box>))')
86 for source in [
87 '(slot-ref obj (quote missing))',
88 '(slot-ref obj (quote x))',
89 '(slot-set! obj (quote missing) 1)',
90 '(slot-bound? obj (quote missing))',
91 '(slot-ref obj 1)',
92 '(slot-bound? obj 1)',
93 '(class-slot-ref <box> 1)',
94 '(slot-ref)',
95 '(class-slot-set! <box> (quote x))',
96 ] {
97 if _ := vm.eval(source) { panic('accepted invalid slot access: ${source}') }
98 }
99 assert slot_hook_text(mut vm, '(slot-set! obj (quote x) 42) (slot-ref obj (quote x))') == '42'
100}
101
102fn test_goops_slot_hooks_r7_library_and_vile_exports() {
103 mut vm := new()
104 actual := slot_hook_text(mut vm, '(define-library (slots demo)
105 (export slot-missing slot-unbound) (import (scheme base) (vile goops)))
106 (import (slots demo) (vile goops))
107 (define-class <box> () (x)) (define obj (make <box>))
108 (define-method (slot-unbound (o <object>)) 42)
109 (slot-ref obj (quote x))')
110 assert actual == '42'
111 value := vm.eval_alt('import((vile,goops))
112 slot-ref(obj, quote(x))') or { panic(err) }
113 assert value.whole() == 42
114}
115
116fn test_goops_slot_hook_unbound_marker_from_host_and_virtual_getter() {
117 mut vm := new()
118 vm.register('host-unbound', fn (mut _vm VM, _args []Value) !Value {
119 return Value{ tag: .uninitialized }
120 }) or { panic(err) }
121 actual := slot_hook_text(mut vm, '(define-class <box> ()
122 (v #:allocation #:virtual #:accessor virtual-value
123 #:slot-ref (lambda (o) (host-unbound)) #:slot-set! (lambda (o v) v)))
124 (define obj (make <box>))
125 (define-method (slot-unbound (c <class>) (o <object>) name) (list (quote unbound) name))
126 (define-method (slot-missing (c <class>) (o <object>) name) (host-unbound))
127 (list (slot-ref obj (quote v)) (virtual-value obj) (slot-bound? obj (quote v))
128 (slot-ref obj (quote missing)) (slot-bound? obj (quote missing)))')
129 assert actual == '((unbound v) (unbound v) #f (unbound missing) #f)'
130}
131
132fn test_goops_handled_missing_slots_keep_bounded_arena_usage() {
133 mut region := arena.new()
134 defer { region.free() }
135 region.push()
136 mut vm := new_with_options(Options{ max_steps: 0, max_frames: 0 }) or { panic(err) }
137 slot_hook_text(mut vm, '(define-class <box> () (x) (s #:allocation #:class)) (define obj (make <box>))
138 (define-method (slot-missing (c <class>) (o <object>) name) 1)
139 (define-method (slot-unbound (o <object>)) 1)
140 (define-method (slot-unbound (c <class>) name) 1)
141 (define (loop n acc) (if (= n 0) acc
142 (loop (- n 1) (+ acc (slot-ref obj (quote absent)) (slot-ref obj (quote x)) (class-slot-ref <box> (quote s))))))')
143 vm.options = Options{ ...vm.options, max_frames: 8 }
144 proc := vm.lookup('loop') or { panic(err) }
145 _ = vm.call(proc, [integer(100), integer(0)]) or { panic(err) }
146 before := region.used()
147 result := vm.call(proc, [integer(50000), integer(0)]) or { panic(err) }
148 delta := region.used() - before
149 region.pop()
150 assert result.whole() == 150000
151 assert delta <= 65536
152}
153