;; GOOPS protocols. Native operations provide storage and dispatch primitives; ;; Scheme methods keep callbacks and their continuations on the evaluator. (define-generic allocate-instance) (define-generic initialize) (define-generic make-instance) (define-generic compute-cpl) (define-generic compute-slots) (define-generic compute-get-n-set) (define-generic compute-getter-method) (define-generic compute-setter-method) (define-generic direct-slot-definition-class) (define-generic compute-direct-slot-definition) (define-generic effective-slot-definition-class) (define-generic compute-effective-slot-definition) (define-generic apply-generic) (define-generic compute-applicable-methods) (define-generic compute-effective-method) (define-generic compute-specialized-effective-method) (define-generic sort-applicable-methods) (define-generic method-more-specific?) (define-generic apply-method) (define-generic apply-methods) (define-generic shallow-clone) (define-generic deep-clone) (define-generic change-class) (define-generic update-instance-for-different-class) (define-generic class-redefinition) (define-method (allocate-instance (class ) args) (%goops-allocate class args)) (define-method (initialize (object ) args) (%goops-validate-initargs args) (let slots ((remaining (class-slots (class-of object)))) (if (pair? remaining) (let* ((slot (car remaining)) (key (slot-definition-init-keyword slot)) (value (if key (get-keyword key args %goops-unbound) %goops-unbound))) (if (%goops-unbound? value) (let ((thunk (slot-definition-init-thunk slot))) (if (and thunk (not (memq (slot-definition-allocation slot) '(#:class #:each-subclass)))) ((slot-ref slot 'slot-set!) object (thunk)))) ((slot-ref slot 'slot-set!) object value)) (slots (cdr remaining))))) object) (define-method (initialize (slot ) args) (next-method) (%goops-configure-slot! slot args)) (define-method (make-instance (class ) . args) (let ((object (allocate-instance class args))) (initialize object args) object)) (define-method (compute-cpl (class )) (compute-std-cpl class)) (define-method (compute-slots (class )) (%goops-std-slots class)) (define-method (direct-slot-definition-class (class ) args) (get-keyword #:class args )) (define-method (compute-direct-slot-definition (class ) args) (apply make (direct-slot-definition-class class args) args)) (define-method (effective-slot-definition-class (class ) slot) (class-of slot)) (define-method (compute-effective-slot-definition (class ) slot) (apply make (effective-slot-definition-class class slot) (slot-definition-options slot))) (define (%goops-shared-initial-value slot) (let ((thunk (slot-definition-init-thunk slot))) (if thunk (thunk) (slot-definition-init-value slot)))) (define-method (compute-get-n-set (class ) slot) (if (%goops-static-slot? slot) (%goops-slot-accessors slot) (let ((allocation (slot-definition-allocation slot))) (cond ((eq? allocation #:instance) (let ((index (slot-ref class 'nfields))) (slot-set! class 'nfields (+ index 1)) index)) ((eq? allocation #:each-subclass) (%goops-share-slot! slot (%goops-shared-initial-value slot))) ((eq? allocation #:class) (if (memq (slot-definition-name slot) (map slot-definition-name (class-direct-slots class))) (%goops-share-slot! slot (%goops-shared-initial-value slot)) (let find ((parents (cdr (class-precedence-list class)))) (if (null? parents) (goops-error "inherited shared slot not found" (slot-definition-name slot)) (let ((inherited (class-slot-definition (car parents) (slot-definition-name slot)))) (if inherited (%goops-inherit-slot! slot inherited) (find (cdr parents)))))))) ((eq? allocation #:virtual) (let ((options (slot-definition-options slot))) (list (get-keyword #:slot-ref options #f) (get-keyword #:slot-set! options #f)))) (else (goops-error "unknown slot allocation" allocation)))))) (define-method (compute-getter-method (class ) slot) (%goops-getter-method class slot)) (define-method (compute-setter-method (class ) slot) (%goops-setter-method class slot)) (define (%goops-install-slot-methods class slot) (let ((getter (slot-definition-getter slot)) (set (slot-definition-setter slot)) (accessor (slot-definition-accessor slot))) (if getter (add-method! getter (compute-getter-method class slot))) (if set (add-method! set (compute-setter-method class slot))) (if accessor (begin (add-method! (%goops-slot-accessor-target accessor) (compute-getter-method class slot)) (add-method! (setter accessor) (compute-setter-method class slot)))))) (define-method (initialize (class ) args) (next-method) (slot-set! class 'name (get-keyword #:name args '???)) (slot-set! class 'direct-supers (get-keyword #:dsupers args '())) (slot-set! class 'nfields 0) (slot-set! class 'cpl (compute-cpl class)) (slot-set! class 'direct-slots (map (lambda (spec) (if (is-a? spec ) spec (compute-direct-slot-definition class (%goops-direct-initargs class spec)))) (get-keyword #:slots args '()))) (let ((slots (map (lambda (slot) (let ((effective (compute-effective-slot-definition class slot))) (%goops-install-slot! class effective (compute-get-n-set class effective)) effective)) (compute-slots class)))) (slot-set! class 'slots slots) (for-each (lambda (slot) (%goops-install-slot-methods class slot)) slots)) (%goops-finish-class! class) class) (define-method (initialize (object ) args) (next-method) (let ((procedure (get-keyword #:procedure args #f))) (if procedure (slot-set! object 'procedure procedure))) object) (define-method (initialize (generic ) args) (next-method) (let ((name (get-keyword #:name args #f))) (slot-set! generic 'name (if name name 'cache-miss))) (slot-set! generic 'methods '()) (let ((previous (get-keyword #:default args #f))) (if previous (%goops-add-default! generic previous))) generic) (define-method (initialize (generic ) args) (next-method) (slot-set! generic 'setter (get-keyword #:setter args #f)) generic) (define-method (initialize (generic ) args) (next-method) (slot-set! generic 'extends (get-keyword #:extends args '())) generic) (define-method (initialize (method ) args) (next-method) method) (define (make-class supers slots . args) (%goops-validate-initargs args) (let* ((normalized (if (let scan ((parents supers)) (and (pair? parents) (or (memq (class-precedence-list (car parents))) (scan (cdr parents))))) supers (append supers (list )))) (metaclass (get-keyword #:metaclass args #f)) (chosen (if metaclass metaclass (ensure-metaclass normalized)))) (apply make chosen #:dsupers normalized #:slots slots #:name (get-keyword #:name args %goops-unbound) args))) (define (ensure-metaclass supers) (ensure-metaclass-with-supers (map class-of supers))) (define (ensure-metaclass-with-supers metas) (let* ((normalized (%goops-normalize-metas metas)) (existing (%goops-common-meta normalized))) (if existing existing (%goops-cache-meta! normalized (make #:dsupers normalized #:slots '() #:name %goops-unbound))))) (define class-name (%goops-field-accessor 'class-name 'name)) (define class-direct-supers (%goops-field-accessor 'class-direct-supers 'direct-supers)) (define class-precedence-list (%goops-field-accessor 'class-precedence-list 'cpl)) (define class-direct-slots (%goops-field-accessor 'class-direct-slots 'direct-slots)) (define class-slots (%goops-field-accessor 'class-slots 'slots)) (define class-direct-subclasses (%goops-field-accessor 'class-direct-subclasses 'direct-subclasses)) (define class-direct-methods (%goops-field-accessor 'class-direct-methods 'direct-methods)) (define slot-definition-name (%goops-field-accessor 'slot-definition-name 'name)) (define slot-definition-allocation (%goops-field-accessor 'slot-definition-allocation 'allocation)) (define slot-definition-init-keyword (%goops-field-accessor 'slot-definition-init-keyword 'init-keyword)) (define slot-definition-init-form (%goops-field-accessor 'slot-definition-init-form 'init-form)) (define slot-definition-init-value (%goops-field-accessor 'slot-definition-init-value 'init-value)) (define slot-definition-init-thunk (%goops-field-accessor 'slot-definition-init-thunk 'init-thunk)) (define slot-definition-options (%goops-field-accessor 'slot-definition-options 'options)) (define slot-definition-getter (%goops-field-accessor 'slot-definition-getter 'getter)) (define slot-definition-setter (%goops-field-accessor 'slot-definition-setter 'setter)) (define slot-definition-accessor (%goops-field-accessor 'slot-definition-accessor 'accessor)) (define method-specializers (%goops-field-accessor 'method-specializers 'specializers)) (define method-procedure (%goops-field-accessor 'method-procedure 'procedure)) (define method-generic-function (%goops-field-accessor 'method-generic-function 'generic-function)) (define method-formals (%goops-field-accessor 'method-formals 'formals)) (define method-source (%goops-field-accessor 'method-source 'source)) (define accessor-method-slot-definition (%goops-field-accessor 'accessor-method-slot-definition 'slot-definition)) ;; Reusable procedures keep these hot paths from creating let closures that ;; capture the enclosing activation on every access. (define (%goops-class-slot-value class name value) (if (%goops-unbound? value) (slot-unbound class name) value)) (define (%goops-class-slot-read class name slot) (if (and slot (memq (slot-definition-allocation slot) '(#:class #:each-subclass))) (%goops-class-slot-value class name ((slot-ref slot 'slot-ref/raw) #f)) (slot-missing class name))) (define (class-slot-ref class name) (%goops-class-slot-read class name (class-slot-definition class name))) (define (%goops-class-slot-write class name value slot) (if (and slot (memq (slot-definition-allocation slot) '(#:class #:each-subclass))) (begin ((slot-ref slot 'slot-set!) #f value) (if #f #f)) (slot-missing class name))) (define (class-slot-set! class name value) (%goops-class-slot-write class name value (class-slot-definition class name))) (define-method (compute-applicable-methods (generic ) args) (%compute-applicable-methods generic args)) (define-method (sort-applicable-methods (generic ) methods args) (%goops-sort-methods generic methods args)) (define-method (method-more-specific? (a ) (b ) types) (%goops-more-specific? a b types)) ;; Bind the next procedure while constructing the chain. A lexical next-method ;; still captures the argument locations of each invocation of the bound body. (define (%goops-bind-effective-method method next) (let ((bind (slot-ref method 'make-procedure))) (if bind (bind next) (method-procedure method)))) (define-method (compute-specialized-effective-method (generic ) (method ) types next) (%goops-bind-effective-method method next)) (define-method (compute-specialized-effective-method (generic ) (method ) types next) (let ((arity (vector-length types))) (if (or (= arity 1) (= arity 2)) (let* ((name (slot-definition-name (accessor-method-slot-definition method))) (slot (class-slot-definition (vector-ref types 0) name))) (slot-ref slot (if (= arity 1) 'slot-ref 'slot-set!))) (next-method)))) (define (%goops-build-effective-method generic methods types) (let* ((method (car methods)) (remaining (cdr methods)) (ordinary? (eq? (class-of generic) )) (next (cond ((null? remaining) (lambda args (no-next-method generic args))) (ordinary? (%goops-build-effective-method generic remaining types)) (else (compute-effective-method generic remaining types))))) (if (and ordinary? (eq? (class-of method) )) (%goops-bind-effective-method method next) (compute-specialized-effective-method generic method types next)))) (define-method (compute-effective-method (generic ) methods types) (%goops-build-effective-method generic methods types)) (define-method (apply-method (generic ) methods build-next args) (let* ((method (car methods)) (next (build-next (cdr methods) args)) (factory (slot-ref method 'make-procedure))) (if factory (apply (factory next) args) (apply (method-procedure method) args)))) (define-method (apply-methods (generic ) methods args) (letrec ((build-next (lambda (remaining previous) (lambda arguments (if (null? remaining) (no-next-method generic arguments) (apply-method generic remaining build-next arguments)))))) (if (null? methods) (no-next-method generic args) (apply-method generic methods build-next args)))) (define-method (apply-generic (generic ) args) (let ((methods (compute-applicable-methods generic args))) (if methods (apply-methods generic (sort-applicable-methods generic methods args) args) (begin (no-applicable-method generic args) (apply-generic generic args))))) (define (make-extended-generic parents . name) (let* ((parents (if (list? parents) parents (list parents))) (name (if (null? name) #f (car name))) (setter-name (and name (string->symbol (string-append "setter:" (symbol->string name))))) (setters (let collect ((rest parents) (out '())) (if (null? rest) (reverse out) (collect (cdr rest) (if (is-a? (car rest) ) (cons (ensure-generic (setter (car rest)) setter-name) out) out)))))) (if (null? setters) (make #:extends parents #:name name) (make #:extends parents #:name name #:setter (make #:extends setters #:name setter-name))))) (define (%goops-unique values) (let scan ((remaining values) (seen '()) (result '())) (if (null? remaining) (reverse result) (if (memq (car remaining) seen) (scan (cdr remaining) seen result) (scan (cdr remaining) (cons (car remaining) seen) (cons (car remaining) result)))))) (define (class-subclasses class) (let scan ((pending (class-direct-subclasses class)) (seen (list class)) (result '())) (if (null? pending) (reverse result) (if (memq (car pending) seen) (scan (cdr pending) seen result) (scan (append (class-direct-subclasses (car pending)) (cdr pending)) (cons (car pending) seen) (cons (car pending) result)))))) (define (class-methods class) (%goops-unique (apply append (map class-direct-methods (cons class (class-subclasses class)))))) (define (%goops-allocate-clone object class) (if (is-a? object ) (make-class (class-direct-supers object) (class-direct-slots object) #:name (class-name object) #:metaclass class) (allocate-instance class '()))) (define (%goops-clone-fields object copy class deep?) (for-each (lambda (slot) (let ((name (slot-definition-name slot))) (if (and (%goops-clone-slot? object slot) (slot-bound? object name)) (let* ((value (slot-ref object name)) (new (if (and deep? (instance? value)) (deep-clone value) value))) (if (not (%goops-copy-native-slot! copy slot new object)) (slot-set! copy name new)))))) (class-slots class)) copy) (define-method (shallow-clone (object )) (let ((class (class-of object))) (%goops-clone-fields object (%goops-allocate-clone object class) class #f))) (define %goops-clone-context (make-parameter #f)) (define-method (deep-clone (object )) (let ((context (%goops-clone-context))) (if context (let ((existing (hashtable-ref context object #f))) (if existing existing (let* ((class (class-of object)) (copy (%goops-allocate-clone object class))) (hashtable-set! context object copy) (%goops-clone-fields object copy class #t)))) (parameterize ((%goops-clone-context (make-eq-hashtable))) (deep-clone object))))) (define-method (update-instance-for-different-class (old ) (new )) new) (define-method (change-class (object ) (target )) ;; A getter or initializer can resume after another callback has committed ;; the copy. Resolve the destination after the value is available. (define (store-copied-slot! value plan slot source name) (let ((destination (%goops-copy-target plan))) (if (and destination (not (%goops-copy-native-slot! destination slot value source))) (slot-set! destination name value)))) (let* ((old-class (class-of object)) (plan (%goops-prepare-copy object old-class target)) (copy (%goops-bind-copy plan (allocate-instance target '())))) (dynamic-wind (%goops-copy-winder plan #t) (lambda () (for-each (lambda (slot) (let* ((name (slot-definition-name slot)) (old-slot (class-slot-definition old-class name))) (if (and old-slot (memq (slot-definition-allocation old-slot) '(#:instance #:virtual)) (slot-bound? object name)) (store-copied-slot! (slot-ref object name) plan slot object name) (if (not (memq (slot-definition-allocation slot) '(#:class #:each-subclass))) (let ((thunk (slot-definition-init-thunk slot))) (if thunk (store-copied-slot! (thunk) plan slot object name))))))) (class-slots target)) (let ((previous (%goops-commit-instance! object copy plan))) (if previous (update-instance-for-different-class previous object))) object) (%goops-copy-winder plan #f)))) (define (%goops-migrate object target enter leave) (dynamic-wind enter (lambda () (change-class object target)) leave)) (define-method (class-redefinition (old ) (new )) new) (define-method (class-redefinition (old ) (new )) (%goops-redefinition-preflight old new) (let ((children (class-direct-subclasses old))) (%goops-redefine-class! old new) (for-each (lambda (child) (class-redefinition child (make-class (class-direct-supers child) (class-direct-slots child) #:name (class-name child) #:metaclass (class-of child)))) children) old)) (define (%goops-reduce-chain primitive left right . rest) (let loop ((value (primitive left right)) (tail rest)) (if (null? tail) value (loop (primitive value (car tail)) (cdr tail))))) (define (%goops-compare-chain primitive left right . rest) (let loop ((a left) (b right) (tail rest)) (let ((result (primitive a b))) (if (or (not result) (null? tail)) result (loop b (car tail) (cdr tail)))))) (define %goops-print-parts (let ((at vector-ref) (size vector-length) (text? string?) (emit write-string)) (lambda (parts port) (let loop ((i 0)) (if (< i (size parts)) (let ((part (at parts i))) (if (text? part) (begin (emit part port) (loop (+ i 1))) (begin (part (at parts (+ i 1)) port) (loop (+ i 2)))))))))) ;; Publish only after every default method has been installed. (define make (%goops-enable-protocols! make-instance initialize allocate-instance make-class)) (set! make-instance make)