module scheme fn redefinition_text(mut vm VM, source string) string { return write(vm.eval(source) or { panic('${source}: ${err.msg()}') }) or { panic(err) } } fn test_goops_ordinary_class_definition_replaces_binding_without_changing_instances() { mut vm := new() assert redefinition_text(mut vm, '(define-class () (x #:init-value 1)) (define old ) (define object (make )) (define-class () (y #:init-value 2)) (list (eq? old ) (eq? (class-of object) old) (slot-ref object (quote x)))') == '(#f #t 1)' } fn test_goops_redefinable_classes_keep_identity_and_migrate_descendants_lazily() { mut vm := new() assert redefinition_text(mut vm, '(define a (make-class (quote ()) (quote ((x #:init-value 1) (gone #:init-value 2))) #:name (quote a) #:metaclass )) (define b (make-class (list a) (quote ((own #:init-value 3))) #:name (quote b))) (define object (make b)) (slot-set! object (quote x) 10) (define fresh (make-class (quote ()) (quote ((added #:init-value 4) (x #:init-value 5))) #:name (quote a) #:metaclass )) (define returned (class-redefinition a fresh)) (list (eq? returned a) (eq? (class-of object) b) (eq? (car (class-direct-supers b)) a) (slot-ref object (quote x)) (slot-ref object (quote added)) (slot-ref object (quote own)) (slot-exists? object (quote gone)))') == '(#t #t #t 10 4 3 #f)' object := vm.lookup('object') or { panic(err) } assert !object.goops_instance_pointer().migrating assert object.goops_instance_pointer().layout_version == object.goops_instance_pointer().class.version } fn test_goops_redefinition_initializes_shared_cells_once_and_keeps_subclass_sharing() { mut vm := new() assert redefinition_text(mut vm, '(define a (make-class (quote ()) (quote ((shared #:allocation #:class #:init-value 1) (per #:allocation #:each-subclass #:init-value 2))) #:metaclass )) (define b (make-class (list a) (quote ()))) (define first (make b)) (define second (make b)) (slot-set! first (quote shared) 10) (slot-set! first (quote per) 20) (class-redefinition a (make-class (quote ()) (quote ((shared #:allocation #:class #:init-value 3) (per #:allocation #:each-subclass #:init-value 4))) #:metaclass )) (define before (list (slot-ref first (quote shared)) (slot-ref first (quote per)))) (slot-set! first (quote shared) 30) (slot-set! first (quote per) 40) (list before (slot-ref second (quote shared)) (slot-ref second (quote per)) (slot-ref (make a) (quote shared)) (slot-ref (make a) (quote per)))') == '((3 4) 30 40 30 4)' } fn test_goops_redefinition_repairs_accessors_new_specializers_and_retained_raw_getters() { mut vm := new() assert redefinition_text(mut vm, '(define get-x (make-accessor (quote get-x))) (define gone-get (make-generic (quote gone-get))) (define a (make-class (quote ()) (list (list (quote x) #:init-value 1 #:accessor get-x) (list (quote gone) #:init-value 2 #:getter gone-get)) #:name (quote a) #:metaclass )) (define b (make-class (list a) (quote ((own #:init-value 3))) #:name (quote b))) (define object (make b)) (set! (get-x object) 10) (define raw (slot-ref (class-slot-definition a (quote x)) (quote slot-ref))) (define-method (describe (x )) (quote (base))) (define-method (describe (x a)) (cons (get-x x) (next-method))) (define-method (describe (x b)) (cons (quote child) (next-method))) (define fresh (make-class (quote ()) (list (quote (added #:init-value 4)) (list (quote x) #:init-value 5 #:accessor get-x)) #:name (quote new-a) #:metaclass )) (define-method (fresh-method (x fresh)) (+ 1 (get-x x))) (class-redefinition a fresh) (list (get-x object) (raw object) (fresh-method object) (describe object) (eq? (car (method-specializers (car (generic-function-methods fresh-method)))) a) (length (generic-function-methods get-x)) (length (generic-function-methods gone-get)) (class-name a) (class-name fresh))') == '(10 10 11 (child 10 base) #t 2 0 new-a a)' if _ := vm.eval('(gone-get object)') { panic('removed generated accessor remained applicable') } else { assert err is EvalError } assert redefinition_text(mut vm, '(set! (get-x object) 42) (raw object)') == '42' } fn test_goops_define_class_redefinition_uses_the_protocol_for_inherited_redefinable_metaclasses() { mut vm := new() assert redefinition_text(mut vm, '(define base (make-class (quote ()) (quote ()) #:metaclass )) (define-class (base) (x #:init-value 1)) (define retained ) (define object (make )) (define-class () (own #:init-value 9)) (define grand (make )) (define retained-grand ) (define-class (base) (added #:init-value 2) (x #:init-value 3)) (list (eq? retained ) (eq? retained-grand ) (eq? (class-of object) retained) (eq? (class-of grand) retained-grand) (slot-ref object (quote x)) (slot-ref object (quote added)) (slot-ref grand (quote own)) (slot-ref grand (quote added)))') == '(#t #t #t #t 1 2 9 2)' } fn test_goops_redefinition_handles_diamond_descendants_and_rejects_new_inheritance_cycles() { mut vm := new() assert redefinition_text(mut vm, '(define a (make-class (quote ()) (quote ((x #:init-value 1))) #:name (quote a) #:metaclass )) (define left (make-class (list a) (quote ((l #:init-value 2))) #:name (quote left))) (define right (make-class (list a) (quote ((r #:init-value 3))) #:name (quote right))) (define bottom (make-class (list left right) (quote ((own #:init-value 4))) #:name (quote bottom))) (define object (make bottom)) (class-redefinition a (make-class (quote ()) (quote ((added #:init-value 5))) #:name (quote a) #:metaclass )) (list (eq? (class-of object) bottom) (slot-ref object (quote added)) (slot-ref object (quote l)) (slot-ref object (quote r)) (slot-ref object (quote own)) (slot-exists? object (quote x)))') == '(#t 5 2 3 4 #f)' if _ := vm.eval('(class-redefinition a (make-class (list bottom) (quote ()) #:metaclass ))') { panic('accepted circular in-place class redefinition') } else { assert err is EvalError assert err.msg().contains('circular') } assert redefinition_text(mut vm, '(slot-ref object (quote added))') == '5' }