| 1 | module scheme |
| 2 | |
| 3 | fn redefinition_text(mut vm VM, source string) string { |
| 4 | return write(vm.eval(source) or { panic('${source}: ${err.msg()}') }) or { panic(err) } |
| 5 | } |
| 6 | |
| 7 | fn test_goops_ordinary_class_definition_replaces_binding_without_changing_instances() { |
| 8 | mut vm := new() |
| 9 | assert redefinition_text(mut vm, '(define-class <a> () (x #:init-value 1)) |
| 10 | (define old <a>) (define object (make <a>)) |
| 11 | (define-class <a> () (y #:init-value 2)) |
| 12 | (list (eq? old <a>) (eq? (class-of object) old) (slot-ref object (quote x)))') == '(#f #t 1)' |
| 13 | } |
| 14 | |
| 15 | fn test_goops_redefinable_classes_keep_identity_and_migrate_descendants_lazily() { |
| 16 | mut vm := new() |
| 17 | assert redefinition_text(mut vm, '(define a (make-class (quote ()) |
| 18 | (quote ((x #:init-value 1) (gone #:init-value 2))) |
| 19 | #:name (quote a) #:metaclass <redefinable-class>)) |
| 20 | (define b (make-class (list a) (quote ((own #:init-value 3))) #:name (quote b))) |
| 21 | (define object (make b)) (slot-set! object (quote x) 10) |
| 22 | (define fresh (make-class (quote ()) (quote ((added #:init-value 4) (x #:init-value 5))) |
| 23 | #:name (quote a) #:metaclass <redefinable-class>)) |
| 24 | (define returned (class-redefinition a fresh)) |
| 25 | (list (eq? returned a) (eq? (class-of object) b) |
| 26 | (eq? (car (class-direct-supers b)) a) |
| 27 | (slot-ref object (quote x)) (slot-ref object (quote added)) |
| 28 | (slot-ref object (quote own)) (slot-exists? object (quote gone)))') == '(#t #t #t 10 4 3 #f)' |
| 29 | object := vm.lookup('object') or { panic(err) } |
| 30 | assert !object.goops_instance_pointer().migrating |
| 31 | assert object.goops_instance_pointer().layout_version == object.goops_instance_pointer().class.version |
| 32 | } |
| 33 | |
| 34 | fn test_goops_redefinition_initializes_shared_cells_once_and_keeps_subclass_sharing() { |
| 35 | mut vm := new() |
| 36 | assert redefinition_text(mut vm, '(define a (make-class (quote ()) |
| 37 | (quote ((shared #:allocation #:class #:init-value 1) |
| 38 | (per #:allocation #:each-subclass #:init-value 2))) #:metaclass <redefinable-class>)) |
| 39 | (define b (make-class (list a) (quote ()))) |
| 40 | (define first (make b)) (define second (make b)) |
| 41 | (slot-set! first (quote shared) 10) (slot-set! first (quote per) 20) |
| 42 | (class-redefinition a (make-class (quote ()) |
| 43 | (quote ((shared #:allocation #:class #:init-value 3) |
| 44 | (per #:allocation #:each-subclass #:init-value 4))) #:metaclass <redefinable-class>)) |
| 45 | (define before (list (slot-ref first (quote shared)) (slot-ref first (quote per)))) |
| 46 | (slot-set! first (quote shared) 30) (slot-set! first (quote per) 40) |
| 47 | (list before (slot-ref second (quote shared)) (slot-ref second (quote per)) |
| 48 | (slot-ref (make a) (quote shared)) (slot-ref (make a) (quote per)))') == '((3 4) 30 40 30 4)' |
| 49 | } |
| 50 | |
| 51 | fn test_goops_redefinition_repairs_accessors_new_specializers_and_retained_raw_getters() { |
| 52 | mut vm := new() |
| 53 | assert redefinition_text(mut vm, '(define get-x (make-accessor (quote get-x))) |
| 54 | (define gone-get (make-generic (quote gone-get))) |
| 55 | (define a (make-class (quote ()) |
| 56 | (list (list (quote x) #:init-value 1 #:accessor get-x) |
| 57 | (list (quote gone) #:init-value 2 #:getter gone-get)) |
| 58 | #:name (quote a) #:metaclass <redefinable-class>)) |
| 59 | (define b (make-class (list a) (quote ((own #:init-value 3))) #:name (quote b))) |
| 60 | (define object (make b)) (set! (get-x object) 10) |
| 61 | (define raw (slot-ref (class-slot-definition a (quote x)) (quote slot-ref))) |
| 62 | (define-method (describe (x <object>)) (quote (base))) |
| 63 | (define-method (describe (x a)) (cons (get-x x) (next-method))) |
| 64 | (define-method (describe (x b)) (cons (quote child) (next-method))) |
| 65 | (define fresh (make-class (quote ()) |
| 66 | (list (quote (added #:init-value 4)) (list (quote x) #:init-value 5 #:accessor get-x)) |
| 67 | #:name (quote new-a) #:metaclass <redefinable-class>)) |
| 68 | (define-method (fresh-method (x fresh)) (+ 1 (get-x x))) |
| 69 | (class-redefinition a fresh) |
| 70 | (list (get-x object) (raw object) (fresh-method object) (describe object) |
| 71 | (eq? (car (method-specializers (car (generic-function-methods fresh-method)))) a) |
| 72 | (length (generic-function-methods get-x)) (length (generic-function-methods gone-get)) |
| 73 | (class-name a) (class-name fresh))') == '(10 10 11 (child 10 base) #t 2 0 new-a a)' |
| 74 | if _ := vm.eval('(gone-get object)') { |
| 75 | panic('removed generated accessor remained applicable') |
| 76 | } else { |
| 77 | assert err is EvalError |
| 78 | } |
| 79 | assert redefinition_text(mut vm, '(set! (get-x object) 42) (raw object)') == '42' |
| 80 | } |
| 81 | |
| 82 | fn test_goops_define_class_redefinition_uses_the_protocol_for_inherited_redefinable_metaclasses() { |
| 83 | mut vm := new() |
| 84 | assert redefinition_text(mut vm, '(define base (make-class (quote ()) (quote ()) |
| 85 | #:metaclass <redefinable-class>)) |
| 86 | (define-class <child> (base) (x #:init-value 1)) |
| 87 | (define retained <child>) (define object (make <child>)) |
| 88 | (define-class <grandchild> (<child>) (own #:init-value 9)) |
| 89 | (define grand (make <grandchild>)) (define retained-grand <grandchild>) |
| 90 | (define-class <child> (base) (added #:init-value 2) (x #:init-value 3)) |
| 91 | (list (eq? retained <child>) (eq? retained-grand <grandchild>) |
| 92 | (eq? (class-of object) retained) (eq? (class-of grand) retained-grand) |
| 93 | (slot-ref object (quote x)) (slot-ref object (quote added)) |
| 94 | (slot-ref grand (quote own)) (slot-ref grand (quote added)))') == '(#t #t #t #t 1 2 9 2)' |
| 95 | } |
| 96 | |
| 97 | fn test_goops_redefinition_handles_diamond_descendants_and_rejects_new_inheritance_cycles() { |
| 98 | mut vm := new() |
| 99 | assert redefinition_text(mut vm, '(define a (make-class (quote ()) (quote ((x #:init-value 1))) |
| 100 | #:name (quote a) #:metaclass <redefinable-class>)) |
| 101 | (define left (make-class (list a) (quote ((l #:init-value 2))) #:name (quote left))) |
| 102 | (define right (make-class (list a) (quote ((r #:init-value 3))) #:name (quote right))) |
| 103 | (define bottom (make-class (list left right) (quote ((own #:init-value 4))) #:name (quote bottom))) |
| 104 | (define object (make bottom)) |
| 105 | (class-redefinition a (make-class (quote ()) (quote ((added #:init-value 5))) |
| 106 | #:name (quote a) #:metaclass <redefinable-class>)) |
| 107 | (list (eq? (class-of object) bottom) (slot-ref object (quote added)) |
| 108 | (slot-ref object (quote l)) (slot-ref object (quote r)) |
| 109 | (slot-ref object (quote own)) (slot-exists? object (quote x)))') == '(#t 5 2 3 4 #f)' |
| 110 | if _ := vm.eval('(class-redefinition a (make-class (list bottom) (quote ()) |
| 111 | #:metaclass <redefinable-class>))') { |
| 112 | panic('accepted circular in-place class redefinition') |
| 113 | } else { |
| 114 | assert err is EvalError |
| 115 | assert err.msg().contains('circular') |
| 116 | } |
| 117 | assert redefinition_text(mut vm, '(slot-ref object (quote added))') == '5' |
| 118 | } |
| 119 | |