| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | fn goops_slots_eval(mut vm VM, source string) string { |
| 6 | value := vm.eval(source) or { panic('${source}: ${err}') } |
| 7 | return write(value) or { panic(err) } |
| 8 | } |
| 9 | |
| 10 | fn test_goops_make_class_instances_and_slots_match_guile_reference() { |
| 11 | mut vm := new() |
| 12 | actual := goops_slots_eval(mut vm, '(import (vile goops)) |
| 13 | (define <point> (make-class (list <object>) |
| 14 | (quote ((x #:init-keyword #:x) (y #:init-value 7))) #:name (quote <point>))) |
| 15 | (define p (make <point> #:x 4)) |
| 16 | (list (class-name <point>) |
| 17 | (map class-name (class-precedence-list <point>)) |
| 18 | (class-name (class-of p)) (instance? p) |
| 19 | (slot-ref p (quote x)) (slot-ref p (quote y)) |
| 20 | (slot-bound? p (quote x)) (slot-exists? p (quote y)) |
| 21 | (slot-exists? p (quote z)))') |
| 22 | assert actual == '(<point> (<point> <object> <top>) <point> #t 4 7 #t #t #f)' |
| 23 | assert goops_slots_eval(mut vm, '(slot-set! p (quote x) 9)') == '9' |
| 24 | assert goops_slots_eval(mut vm, '(slot-ref p (quote x))') == '9' |
| 25 | assert goops_slots_eval(mut vm, '(instance? (make <object>))') == '#t' |
| 26 | assert goops_slots_eval(mut vm, '(class-name (make-class (quote ()) (quote ())))') == '(unbound)' |
| 27 | assert goops_slots_eval(mut vm, '(let ((a (make-class (list <object>) (quote ()) #:name (quote <same>))) |
| 28 | (b (make-class (list <object>) (quote ()) #:name (quote <same>)))) |
| 29 | (eq? a b))') == '#f' |
| 30 | assert goops_slots_eval(mut vm, '(map class-name (class-precedence-list |
| 31 | (make-class (quote ()) (quote ()) #:name (quote <empty>))))') == '(<empty> <object> <top>)' |
| 32 | assert goops_slots_eval(mut vm, '(slot-bound? (make <point>) (quote x))') == '#f' |
| 33 | assert goops_slots_eval(mut vm, '(slot-ref (make <point> #:z 9) (quote y))') == '7' |
| 34 | } |
| 35 | |
| 36 | fn test_goops_class_and_slot_errors_do_not_publish_partial_classes() { |
| 37 | mut vm := new() |
| 38 | vm.ensure_goops() |
| 39 | for source in [ |
| 40 | '(make-class (list <object>) (quote ((x) (x))) #:name (quote <bad>))', |
| 41 | '(make-class (list <object> <object>) (quote ()) #:name (quote <bad>))', |
| 42 | '(make-class (list <object>) (quote ((x #:allocation #:unknown-allocation))) #:name (quote <bad>))', |
| 43 | '(make-class (list <object>) (quote ((x #:accessor 42))) #:name (quote <bad>))', |
| 44 | ] { |
| 45 | before := vm.goops.classes.len |
| 46 | if _ := vm.eval(source) { panic('accepted invalid class: ${source}') } |
| 47 | assert vm.goops.classes.len == before |
| 48 | } |
| 49 | goops_slots_eval(mut vm, '(define <point> (make-class (list <object>) |
| 50 | (quote ((x #:init-keyword #:x))) #:name (quote <point>))) |
| 51 | (define p (make <point>))') |
| 52 | for source in ['(slot-ref p (quote x))', '(slot-ref p (quote z))', '(slot-set! p (quote z) 9)', |
| 53 | '(make <point> #:x)', '(make <integer>)', '(make <class> #:slots)'] { |
| 54 | if _ := vm.eval(source) { panic('accepted invalid slot operation: ${source}') } |
| 55 | } |
| 56 | assert goops_slots_eval(mut vm, '(slot-set! p (quote x) 10)') == '10' |
| 57 | assert goops_slots_eval(mut vm, '(slot-ref p (quote x))') == '10' |
| 58 | gc_collect() |
| 59 | assert goops_slots_eval(mut vm, '(slot-ref p (quote x))') == '10' |
| 60 | mut other := new() |
| 61 | foreign := vm.lookup('p') or { panic(err) } |
| 62 | other.define('foreign-instance', foreign) or { panic(err) } |
| 63 | for source in ['(class-of foreign-instance)', '(slot-ref foreign-instance (quote x))', |
| 64 | '(slot-set! foreign-instance (quote x) 3)'] { |
| 65 | if _ := other.eval(source) { panic('accepted foreign instance: ${source}') } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | fn test_goops_multiple_inheritance_slot_order_and_override() { |
| 70 | mut vm := new() |
| 71 | actual := goops_slots_eval(mut vm, '(define <a> (make-class (list <object>) |
| 72 | (quote ((x #:init-value 1))) #:name (quote <a>))) |
| 73 | (define <b> (make-class (list <object>) |
| 74 | (quote ((y #:init-value 2))) #:name (quote <b>))) |
| 75 | (define <c> (make-class (list <a> <b>) |
| 76 | (quote ((z #:init-value 3))) #:name (quote <c>))) |
| 77 | (define obj (make <c>)) |
| 78 | (list (map class-name (class-precedence-list <c>)) |
| 79 | (slot-ref obj (quote x)) (slot-ref obj (quote y)) |
| 80 | (slot-ref obj (quote z)))') |
| 81 | assert actual == '((<c> <a> <b> <object> <top>) 1 2 3)' |
| 82 | class := vm.lookup('<c>') or { panic(err) } |
| 83 | slots := class.goops_class_pointer().slots |
| 84 | assert slots.len == 3 |
| 85 | assert slots[0].name == 'y' && slots[1].name == 'x' && slots[2].name == 'z' |
| 86 | assert goops_slots_eval(mut vm, '(define <d> (make-class (list <a>) |
| 87 | (quote ((x #:init-value 5) (w #:init-value 6))) #:name (quote <d>))) |
| 88 | (define d (make <d>)) |
| 89 | (list (slot-ref d (quote x)) (slot-ref d (quote w)))') == '(5 6)' |
| 90 | } |
| 91 | |
| 92 | fn test_goops_slot_operations_share_scheme_and_vile_frontends() { |
| 93 | mut vm := new() |
| 94 | vm.eval('(define point_class (make-class (list <object>) |
| 95 | (quote ((x #:init-keyword #:x))) #:name (quote <point>)))') or { panic(err) } |
| 96 | first := vm.eval_alt('import((vile,goops)) |
| 97 | p := make(point_class, #:x, 4) |
| 98 | slot-ref(p, quote(x))') or { panic(err) } |
| 99 | assert first.whole() == 4 |
| 100 | assert goops_slots_eval(mut vm, '(slot-set! p (quote x) 12)') == '12' |
| 101 | second := vm.eval_alt('slot-ref(p, quote(x))') or { panic(err) } |
| 102 | assert second.whole() == 12 |
| 103 | } |
| 104 | |
| 105 | fn test_goops_instances_use_one_scanned_arena_allocation() { |
| 106 | mut region := arena.new() |
| 107 | defer { region.free() } |
| 108 | region.push() |
| 109 | mut vm := new() |
| 110 | class := vm.eval('(make-class (list <object>) (quote ((x) (y))) #:name (quote <point>))') or { |
| 111 | panic(err) |
| 112 | } |
| 113 | args := [class] |
| 114 | before := region.used() |
| 115 | mut instance := Value{} |
| 116 | for _ in 0 .. 1000 { |
| 117 | instance = goops_make_native(mut vm, args) or { panic(err) } |
| 118 | } |
| 119 | delta := region.used() - before |
| 120 | region.pop() |
| 121 | assert instance.tag == .goops_instance |
| 122 | // One initial alignment gap is possible. Any extra allocation per call |
| 123 | // would add at least another aligned block on all 1000 iterations. |
| 124 | expected := 1000 * (goops_field_offset() + 2 * int(sizeof(Value))) |
| 125 | assert delta >= expected && delta < expected + 16 |
| 126 | } |
| 127 | |
| 128 | fn test_goops_temporary_classes_do_not_publish_into_vm_arena() { |
| 129 | mut owner_region := arena.new() |
| 130 | defer { owner_region.free() } |
| 131 | owner_region.push() |
| 132 | mut vm := new() |
| 133 | assert vm.goops.deferred |
| 134 | owner_region.pop() |
| 135 | mut temporary := arena.new() |
| 136 | temporary.push() |
| 137 | root_object := vm.lookup('<object>') or { panic(err) } |
| 138 | assert owner_region.owns(root_object.pointer()) && !temporary.owns(root_object.pointer()) |
| 139 | baseline := vm.goops.classes.len |
| 140 | class := vm.eval('(make-class (list <object>) (quote ((x #:init-value 3))) #:name (quote <temporary>))') or { |
| 141 | panic(err) |
| 142 | } |
| 143 | assert temporary.owns(class.goops_class_pointer()) |
| 144 | assert vm.goops.classes.len == baseline |
| 145 | temporary.pop() |
| 146 | temporary.free() |
| 147 | owner_region.push() |
| 148 | assert goops_slots_eval(mut vm, '(class-name <object>)') == '<object>' |
| 149 | assert vm.goops.classes.len == baseline |
| 150 | owner_region.pop() |
| 151 | } |
| 152 | |
| 153 | fn test_goops_class_allocated_slots_share_storage_with_subclasses() { |
| 154 | mut vm := new() |
| 155 | actual := goops_slots_eval(mut vm, '(import (vile goops)) |
| 156 | (define count 0) |
| 157 | (define-class <shared> () |
| 158 | (x #:allocation #:class #:init-keyword #:x |
| 159 | #:init-form (begin (set! count (+ count 1)) count)) |
| 160 | (y #:init-value 7)) |
| 161 | (define-class <child> (<shared>)) |
| 162 | (define a (make <shared>)) |
| 163 | (define b (make <child>)) |
| 164 | (list count (slot-ref a (quote x)) (slot-ref b (quote x)) |
| 165 | (slot-ref a (quote y)) (slot-ref b (quote y)))') |
| 166 | assert actual == '(1 1 1 7 7)' |
| 167 | gc_collect() |
| 168 | assert goops_slots_eval(mut vm, '(slot-set! a (quote x) 5) |
| 169 | (list (slot-ref a (quote x)) (slot-ref b (quote x)))') == '(5 5)' |
| 170 | assert goops_slots_eval(mut vm, '(make <child> #:x 9) |
| 171 | (list (slot-ref a (quote x)) (slot-ref b (quote x)))') == '(9 9)' |
| 172 | assert goops_slots_eval(mut vm, '(slot-set! a (quote y) 10) |
| 173 | (list (slot-ref a (quote y)) (slot-ref b (quote y)))') == '(10 7)' |
| 174 | shared_class := vm.lookup('<shared>') or { panic(err) } |
| 175 | child := vm.lookup('<child>') or { panic(err) } |
| 176 | assert shared_class.goops_class_pointer().instance_fields == 1 |
| 177 | assert child.goops_class_pointer().instance_fields == 1 |
| 178 | assert voidptr(shared_class.goops_class_pointer().slots[0].shared) == voidptr(child.goops_class_pointer().slots[0].shared) |
| 179 | assert goops_slots_eval(mut vm, '(define-class <empty-shared> () |
| 180 | (z #:allocation #:class)) |
| 181 | (define c (make <empty-shared>)) |
| 182 | (list (slot-bound? c (quote z)) |
| 183 | (begin (slot-set! c (quote z) 6) (slot-ref c (quote z))))') == '(#f 6)' |
| 184 | } |
| 185 | |
| 186 | fn test_goops_class_slot_override_and_invalid_allocation_options() { |
| 187 | mut vm := new() |
| 188 | assert goops_slots_eval(mut vm, '(import (vile goops)) |
| 189 | (define-class <base> () (x #:allocation #:class #:init-value 1)) |
| 190 | (define-class <derived> (<base>) (x #:allocation #:instance #:init-value 2)) |
| 191 | (define a (make <base>)) (define b (make <derived>)) |
| 192 | (slot-set! a (quote x) 5) |
| 193 | (list (slot-ref a (quote x)) (slot-ref b (quote x)))') == '(5 2)' |
| 194 | for source in [ |
| 195 | '(define-class <bad> () (x #:allocation #:virtual))', |
| 196 | '(define-class <bad> () (x #:allocation 3))', |
| 197 | '(define-class <bad> () (x #:allocation #:unknown-allocation #:allocation #:instance))', |
| 198 | '(make-class (list <object>) (quote ((x #:allocation #:virtual))))', |
| 199 | ] { |
| 200 | if _ := vm.eval(source) { panic('accepted invalid slot allocation: ${source}') } |
| 201 | } |
| 202 | assert goops_slots_eval(mut vm, '(slot-ref a (quote x))') == '5' |
| 203 | } |
| 204 | |
| 205 | fn test_goops_class_allocated_slots_work_in_vile_frontend() { |
| 206 | mut vm := new() |
| 207 | value := vm.eval_alt('import((vile,goops)) |
| 208 | define-class(shared_cls, (), (x, #:allocation, #:class, #:init-value, 3)) |
| 209 | a := make(shared_cls) |
| 210 | b := make(shared_cls) |
| 211 | slot-set!(a, quote(x), 8) |
| 212 | slot-ref(b, quote(x))') or { panic(err) } |
| 213 | assert value.whole() == 8 |
| 214 | } |
| 215 | |
| 216 | fn test_goops_class_slots_do_not_expand_each_instance() { |
| 217 | mut region := arena.new() |
| 218 | defer { region.free() } |
| 219 | region.push() |
| 220 | mut vm := new() |
| 221 | class := vm.eval('(make-class (list <object>) |
| 222 | (quote ((shared #:allocation #:class #:init-value 2))) |
| 223 | #:name (quote <compact>))') or { panic(err) } |
| 224 | assert class.goops_class_pointer().instance_fields == 0 |
| 225 | args := [class] |
| 226 | before := region.used() |
| 227 | mut last := Value{} |
| 228 | for _ in 0 .. 1000 { |
| 229 | last = goops_make_native(mut vm, args) or { panic(err) } |
| 230 | } |
| 231 | delta := region.used() - before |
| 232 | region.pop() |
| 233 | assert last.tag == .goops_instance |
| 234 | expected := 1000 * goops_field_offset() |
| 235 | assert delta >= expected && delta < expected + 16 |
| 236 | } |
| 237 | |