| 1 | module scheme |
| 2 | |
| 3 | fn each_subclass_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_each_subclass_cells_are_independent_across_classes() { |
| 9 | mut vm := new() |
| 10 | actual := each_subclass_text(mut vm, '(import (vile goops)) |
| 11 | (define calls 0) |
| 12 | (define-class <base> () |
| 13 | (x #:allocation #:each-subclass |
| 14 | #:init-form (begin (set! calls (+ calls 1)) calls))) |
| 15 | (define-class <child> (<base>)) |
| 16 | (define-class <sibling> (<base>)) |
| 17 | (define-class <grandchild> (<child>)) |
| 18 | (define base (make <base>)) |
| 19 | (define child (make <child>)) |
| 20 | (define sibling (make <sibling>)) |
| 21 | (define grandchild (make <grandchild>)) |
| 22 | (list calls (slot-ref base (quote x)) (slot-ref child (quote x)) |
| 23 | (slot-ref sibling (quote x)) (slot-ref grandchild (quote x)))') |
| 24 | if actual != '(4 1 2 3 4)' { panic(actual) } |
| 25 | changed := each_subclass_text(mut vm, '(slot-set! child (quote x) 99) |
| 26 | (list (slot-ref base (quote x)) (slot-ref (make <child>) (quote x)) |
| 27 | (slot-ref sibling (quote x)) (slot-ref grandchild (quote x)) calls)') |
| 28 | if changed != '(1 99 3 4 4)' { panic(changed) } |
| 29 | base := vm.lookup('<base>') or { panic(err) } |
| 30 | child_class := vm.lookup('<child>') or { panic(err) } |
| 31 | if base.goops_class_pointer().instance_fields != 0 |
| 32 | || child_class.goops_class_pointer().instance_fields != 0 { |
| 33 | panic('each-subclass slot reserved instance fields') |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | fn test_goops_each_subclass_init_keywords_and_both_frontends() { |
| 38 | mut vm := new() |
| 39 | actual := each_subclass_text(mut vm, '(import (vile goops)) |
| 40 | (define-class <root> () (x #:allocation #:each-subclass |
| 41 | #:init-keyword #:x #:init-value 3)) |
| 42 | (define-class <branch> (<root>)) |
| 43 | (define root (make <root>)) |
| 44 | (define branch (make <branch> #:x 42)) |
| 45 | (list (slot-ref root (quote x)) (slot-ref branch (quote x)) |
| 46 | (slot-ref (make <branch>) (quote x)))') |
| 47 | if actual != '(3 42 42)' { panic(actual) } |
| 48 | vm.eval('(define branch_class <branch>)') or { panic(err) } |
| 49 | alt := vm.eval_alt('slot-ref(make(branch_class), quote(x))') or { panic(err) } |
| 50 | if alt.whole() != 42 { panic('Vile frontend lost subclass cell') } |
| 51 | shared := each_subclass_text(mut vm, '(define-class <value-base> () |
| 52 | (x #:allocation #:each-subclass #:init-value (list 1))) |
| 53 | (define-class <value-child> (<value-base>)) |
| 54 | (define value-base (make <value-base>)) |
| 55 | (define value-child (make <value-child>)) |
| 56 | (define same-initial-value |
| 57 | (eq? (slot-ref value-base (quote x)) (slot-ref value-child (quote x)))) |
| 58 | (slot-set! value-child (quote x) (list 2)) |
| 59 | (list same-initial-value (slot-ref value-base (quote x)) |
| 60 | (slot-ref value-child (quote x)))') |
| 61 | if shared != '(#t (1) (2))' { panic(shared) } |
| 62 | } |
| 63 | |
| 64 | fn test_goops_each_subclass_direct_make_class_and_initializer_failures() { |
| 65 | mut vm := new() |
| 66 | actual := each_subclass_text(mut vm, '(import (vile goops)) |
| 67 | (define calls 0) |
| 68 | (define <direct> (make-class (list <object>) |
| 69 | (list (list (quote x) #:allocation #:each-subclass #:init-thunk |
| 70 | (lambda () (set! calls (+ calls 1)) calls))) #:name (quote <direct>))) |
| 71 | (define <derived> (make-class (list <direct>) (quote ()) #:name (quote <derived>))) |
| 72 | (list calls (slot-ref (make <direct>) (quote x)) |
| 73 | (slot-ref (make <derived>) (quote x)))') |
| 74 | if actual != '(2 1 2)' { panic(actual) } |
| 75 | // Guile 3.0.11 keeps the first occurrence of a slot option. Duplicate |
| 76 | // allocation keywords are valid; their order determines the allocation. |
| 77 | for source, expected in { |
| 78 | '(slot-definition-allocation (class-slot-definition |
| 79 | (make-class (list <object>) |
| 80 | (quote ((x #:allocation #:each-subclass #:allocation #:class)))) (quote x)))': '#:each-subclass' |
| 81 | '(slot-definition-allocation (class-slot-definition |
| 82 | (make-class (list <object>) |
| 83 | (quote ((x #:allocation #:class #:allocation #:each-subclass)))) (quote x)))': '#:class' |
| 84 | '(slot-ref (make (make-class (list <object>) |
| 85 | (quote ((x #:init-value 1 #:init-value 2))))) (quote x))': '1' |
| 86 | } { |
| 87 | value := each_subclass_text(mut vm, source) |
| 88 | if value != expected { panic('expected ${expected}, got ${value}') } |
| 89 | } |
| 90 | // Keep Vile's single-value initializer contract as a separate rejection; |
| 91 | // Guile accepts that case, unlike an unknown allocation keyword. |
| 92 | for source in [ |
| 93 | '(make-class (list <object>) |
| 94 | (quote ((x #:allocation #:unknown))))', |
| 95 | '(make-class (list <object>) |
| 96 | (list (list (quote x) #:allocation #:each-subclass #:init-thunk |
| 97 | (lambda () (values 1 2)))))', |
| 98 | ] { |
| 99 | if _ := vm.eval(source) { panic('accepted invalid class: ${source}') } |
| 100 | } |
| 101 | } |
| 102 | |