| 1 | module scheme |
| 2 | |
| 3 | fn class_slot_text(mut vm VM, source string) string { |
| 4 | value := vm.eval(source) or { panic('${source}: ${err.msg()}') } |
| 5 | return write(value) or { panic(err) } |
| 6 | } |
| 7 | |
| 8 | fn test_goops_class_slot_access_uses_shared_and_each_subclass_cells() { |
| 9 | mut vm := new() |
| 10 | actual := class_slot_text(mut vm, '(import (vile goops)) |
| 11 | (define-class <base> () |
| 12 | (shared #:allocation #:class #:init-value 2) |
| 13 | (own #:allocation #:each-subclass #:init-value 3)) |
| 14 | (define-class <child> (<base>)) |
| 15 | (define base (make <base>)) |
| 16 | (define child (make <child>)) |
| 17 | (list (class-slot-ref <base> (quote shared)) |
| 18 | (class-slot-ref <child> (quote shared)) |
| 19 | (class-slot-ref <base> (quote own)) |
| 20 | (class-slot-ref <child> (quote own)))') |
| 21 | if actual != '(2 2 3 3)' { panic(actual) } |
| 22 | changed := class_slot_text(mut vm, '(list |
| 23 | (class-slot-set! <child> (quote shared) 11) |
| 24 | (class-slot-set! <child> (quote own) 8) |
| 25 | (class-slot-ref <base> (quote shared)) |
| 26 | (class-slot-ref <base> (quote own)) |
| 27 | (class-slot-ref <child> (quote own)) |
| 28 | (slot-ref base (quote shared)) |
| 29 | (slot-ref child (quote own)))') |
| 30 | if changed != '(#<unspecified> #<unspecified> 11 3 8 11 8)' { panic(changed) } |
| 31 | } |
| 32 | |
| 33 | fn test_goops_class_slot_access_direct_make_class_and_vile_frontend() { |
| 34 | mut vm := new() |
| 35 | actual := class_slot_text(mut vm, '(import (vile goops)) |
| 36 | (define <direct> (make-class (list <object>) |
| 37 | (list (list (quote value) #:allocation #:class #:init-value 6)) |
| 38 | #:name (quote <direct>))) |
| 39 | (class-slot-ref <direct> (quote value))') |
| 40 | if actual != '6' { panic(actual) } |
| 41 | alt := vm.eval_alt('import((vile,goops)) |
| 42 | define-class(local_class, (), (value, #:allocation, #:class, #:init-value, 4)) |
| 43 | class-slot-set!(local_class, quote(value), 9) |
| 44 | class-slot-ref(local_class, quote(value))') or { panic(err) } |
| 45 | if alt.whole() != 9 { panic('Vile class slot access returned wrong value') } |
| 46 | } |
| 47 | |
| 48 | fn test_goops_class_slot_access_rejects_invalid_and_unbound_slots() { |
| 49 | mut vm := new() |
| 50 | vm.eval('(import (vile goops)) |
| 51 | (define-class <slots> () |
| 52 | (shared #:allocation #:class) |
| 53 | (instance #:init-value 1))') or { panic(err) } |
| 54 | for source in [ |
| 55 | '(class-slot-ref <slots> (quote shared))', |
| 56 | '(class-slot-ref <slots> (quote instance))', |
| 57 | '(class-slot-set! <slots> (quote instance) 2)', |
| 58 | '(class-slot-ref <slots> (quote missing))', |
| 59 | '(class-slot-ref <slots> 1)', |
| 60 | '(class-slot-ref 1 (quote shared))', |
| 61 | '(class-slot-set! <slots> (quote shared))', |
| 62 | ] { |
| 63 | if _ := vm.eval(source) { panic('accepted invalid class slot access: ${source}') } |
| 64 | } |
| 65 | actual := class_slot_text(mut vm, '(class-slot-set! <slots> (quote shared) 7) |
| 66 | (class-slot-ref <slots> (quote shared))') |
| 67 | if actual != '7' { panic(actual) } |
| 68 | } |
| 69 | |