X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/clg/blobdiff_plain/4e968638bb6018ba57fccf3543985b6c95b783fa..8f49b7a10a9717890ca98dff2b01799b80ce2761:/gffi/virtual-slots.lisp diff --git a/gffi/virtual-slots.lisp b/gffi/virtual-slots.lisp index 2f75fc9..22a6af3 100644 --- a/gffi/virtual-slots.lisp +++ b/gffi/virtual-slots.lisp @@ -20,47 +20,59 @@ ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -;; $Id: virtual-slots.lisp,v 1.1 2006-04-25 20:49:16 espen Exp $ +;; $Id: virtual-slots.lisp,v 1.11 2007-11-08 13:49:26 espen Exp $ (in-package "GFFI") ;;;; Superclass for all metaclasses implementing some sort of virtual slots -(eval-when (:compile-toplevel :load-toplevel :execute) - (defclass virtual-slots-class (standard-class) - ()) - - (defclass direct-virtual-slot-definition (standard-direct-slot-definition) - ((setter :reader slot-definition-setter :initarg :setter) - (getter :reader slot-definition-getter :initarg :getter) - (unbound :reader slot-definition-unbound :initarg :unbound) - (boundp :reader slot-definition-boundp :initarg :boundp) - (makunbound :reader slot-definition-makunbound :initarg :makunbound) - #+clisp(type :initarg :type :reader slot-definition-type))) - - (defclass effective-virtual-slot-definition (standard-effective-slot-definition) - ((setter :reader slot-definition-setter :initarg :setter) - (getter :reader slot-definition-getter :initarg :getter) - (unbound :reader slot-definition-unbound :initarg :unbound) - (boundp :reader slot-definition-boundp :initarg :boundp) - (makunbound :reader slot-definition-makunbound :initarg :makunbound) - #+clisp(reader-function) - #+clisp(writer-function) - #+clisp(boundp-function) - makunbound-function - #+clisp(type :initarg :type :reader slot-definition-type))) - - (defclass direct-special-slot-definition (standard-direct-slot-definition) - ((special :initarg :special :accessor slot-definition-special))) +(defclass virtual-slots-class (standard-class) + ()) + +(defclass direct-virtual-slot-definition (standard-direct-slot-definition) + ((setter :reader slot-definition-setter :initarg :setter) + (getter :reader slot-definition-getter :initarg :getter) + (unbound :reader slot-definition-unbound :initarg :unbound) + (boundp :reader slot-definition-boundp :initarg :boundp) + (makunbound :reader slot-definition-makunbound :initarg :makunbound) + #+clisp(type :initarg :type :reader slot-definition-type))) - (defclass effective-special-slot-definition (standard-effective-slot-definition) - ((special :initarg :special :accessor slot-definition-special)))) +(defclass effective-virtual-slot-definition (standard-effective-slot-definition) + ((setter :reader slot-definition-setter :initarg :setter) + (getter :reader slot-definition-getter :initarg :getter) + (unbound :reader slot-definition-unbound :initarg :unbound) + (boundp :reader slot-definition-boundp :initarg :boundp) + (makunbound :reader slot-definition-makunbound :initarg :makunbound) + #+clisp(reader-function) + #+clisp(writer-function) + #+clisp(boundp-function) + makunbound-function + #+clisp(type :initarg :type :reader slot-definition-type))) + +(defclass direct-special-slot-definition (standard-direct-slot-definition) + ((special :initarg :special :accessor slot-definition-special))) + +(defclass effective-special-slot-definition (standard-effective-slot-definition) + ((special :initarg :special :accessor slot-definition-special))) + +(defclass virtual-slots-object (standard-object) + ()) -(defgeneric compute-slot-reader-function (slotd)) +(defgeneric slot-readable-p (slotd)) +(defgeneric slot-writable-p (slotd)) +(defgeneric compute-slot-reader-function (slotd &optional signal-unbound-p)) (defgeneric compute-slot-boundp-function (slotd)) (defgeneric compute-slot-writer-function (slotd)) (defgeneric compute-slot-makunbound-function (slotd)) +(defmethod slot-readable-p ((slotd standard-effective-slot-definition)) + (declare (ignore slotd)) + t) + +(defmethod slot-writable-p ((slotd standard-effective-slot-definition)) + (declare (ignore slotd)) + t) + #+clisp (defmethod slot-definition-type ((slotd t)) @@ -84,6 +96,9 @@ (defmethod effective-slot-definition-class ((class virtual-slots-class) &rest in (t (call-next-method)))) +(defmethod slot-readable-p ((slotd effective-virtual-slot-definition)) + (slot-boundp slotd 'getter)) + (define-condition unreadable-slot (cell-error) ((instance :reader unreadable-slot-instance :initarg :instance)) (:report (lambda (condition stream) @@ -91,20 +106,67 @@ (define-condition unreadable-slot (cell-error) (cell-error-name condition) (unreadable-slot-instance condition))))) -(defmethod compute-slot-reader-function ((slotd effective-virtual-slot-definition)) - (if (slot-boundp slotd 'getter) - (slot-value slotd 'getter) - #'(lambda (object) - (error 'unreadable-slot :name (slot-definition-name slotd) :instance object)))) +(defmethod compute-slot-reader-function :around ((slotd effective-virtual-slot-definition) &optional (signal-unbound-p t)) + (if (not (slot-readable-p slotd)) + #'(lambda (object) + (error 'unreadable-slot :name (slot-definition-name slotd) :instance object)) + (let ((reader-function (call-next-method))) + (cond + ;; Don't create wrapper to signal unbound value + ((not signal-unbound-p) reader-function) + + ;; An explicit boundp function has been supplied + ((slot-boundp slotd 'boundp) + (let ((boundp (slot-value slotd 'boundp))) + #'(lambda (object) + (if (not (funcall boundp object)) + (slot-unbound (class-of object) object (slot-definition-name slotd)) + (funcall reader-function object))))) + + ;; A type unbound value exists + ((let ((unbound-method (find-applicable-type-method 'unbound-value + (slot-definition-type slotd) nil))) + (when unbound-method + (let ((unbound-value (funcall unbound-method (slot-definition-type slotd)))) + #'(lambda (object) + (let ((value (funcall reader-function object))) + (if (eq value unbound-value) + (slot-unbound (class-of object) object (slot-definition-name slotd)) + value))))))) + + ((let ((boundp-function (compute-slot-boundp-function slotd))) + #'(lambda (object) + (if (funcall boundp-function object) + (funcall reader-function object) + (slot-unbound (class-of object) object (slot-definition-name slotd)))))))))) + +(defmethod compute-slot-reader-function ((slotd effective-virtual-slot-definition) &optional signal-unbound-p) + (declare (ignore signal-unbound-p)) + (let ((getter (slot-value slotd 'getter))) + #-sbcl getter + #+sbcl + (etypecase getter + (symbol #'(lambda (object) (funcall getter object))) + (function getter)))) (defmethod compute-slot-boundp-function ((slotd effective-virtual-slot-definition)) (cond + ;; Non readable slots are not bound per definition + ((not (slot-readable-p slotd)) + #'(lambda (object) (declare (ignore object)) nil)) + ;; An explicit boundp function has been supplied - ((slot-boundp slotd 'boundp) (slot-value slotd 'boundp)) - + ((slot-boundp slotd 'boundp) + (let ((boundp (slot-value slotd 'boundp))) + #-sbcl boundp + #+sbcl + (etypecase boundp + (symbol #'(lambda (object) (funcall boundp object))) + (function boundp)))) + ;; An unbound value has been supplied ((slot-boundp slotd 'unbound) - (let ((reader-function (slot-value slotd 'reader-function)) + (let ((reader-function (compute-slot-reader-function slotd nil)) (unbound-value (slot-value slotd 'unbound))) #'(lambda (object) (not (eql (funcall reader-function object) unbound-value))))) @@ -113,7 +175,7 @@ (defmethod compute-slot-boundp-function ((slotd effective-virtual-slot-definitio ((let ((unbound-method (find-applicable-type-method 'unbound-value (slot-definition-type slotd) nil))) (when unbound-method - (let ((reader-function (slot-value slotd 'reader-function)) + (let ((reader-function (compute-slot-reader-function slotd nil)) (unbound-value (funcall unbound-method (slot-definition-type slotd)))) #'(lambda (object) (not (eql (funcall reader-function object) unbound-value))))))) @@ -121,6 +183,9 @@ (defmethod compute-slot-boundp-function ((slotd effective-virtual-slot-definitio ;; Slot has no unbound state (#'(lambda (object) (declare (ignore object)) t)))) +(defmethod slot-writable-p ((slotd effective-virtual-slot-definition)) + (slot-boundp slotd 'setter)) + (define-condition unwritable-slot (cell-error) ((instance :reader unwritable-slot-instance :initarg :instance)) (:report (lambda (condition stream) @@ -128,39 +193,61 @@ (define-condition unwritable-slot (cell-error) (cell-error-name condition) (unwritable-slot-instance condition))))) +(defmethod compute-slot-writer-function :around ((slotd effective-virtual-slot-definition)) + (if (not (slot-writable-p slotd)) + #'(lambda (value object) + (declare (ignore value)) + (error 'unwritable-slot :name (slot-definition-name slotd) :instance object)) + (call-next-method))) + (defmethod compute-slot-writer-function ((slotd effective-virtual-slot-definition)) - (if (slot-boundp slotd 'setter) - (slot-value slotd 'setter) - #'(lambda (value object) - (declare (ignore value)) - (error 'unwritable-slot :name (slot-definition-name slotd) :instance object)))) + (let ((setter (slot-value slotd 'setter))) + #-sbcl setter + #+sbcl + (etypecase setter + (symbol #'(lambda (value object) (funcall setter value object))) + (list #'(lambda (value object) + (funcall setter value object))) + (function setter)))) + +(define-condition slot-can-not-be-unbound (cell-error) + ((instance :reader slot-can-not-be-unbound-instance :initarg :instance)) + (:report (lambda (condition stream) + (format stream "~@" + (cell-error-name condition) + (slot-can-not-be-unbound-instance condition))))) (defmethod compute-slot-makunbound-function ((slotd effective-virtual-slot-definition)) (cond - ((slot-boundp slotd 'makunbound) (slot-value slotd 'makunbound)) + ((not (slot-writable-p slotd)) + #'(lambda (object) + (error 'unwritable-slot :name (slot-definition-name slotd) :instance object))) + ((slot-boundp slotd 'makunbound) + (let ((makunbound (slot-value slotd 'makunbound))) + #-sbcl makunbound + #+sbcl + (etypecase makunbound + (symbol #'(lambda (object) (funcall makunbound object))) + (function makunbound)))) ((slot-boundp slotd 'unbound) #'(lambda (object) (funcall (slot-value slotd 'writer-function) (slot-value slotd 'unbound) object))) (t #'(lambda (object) - (error 'unwritable-slot :name (slot-definition-name slotd) :instance object))))) + (error 'slot-can-not-be-unbound :name (slot-definition-name slotd) :instance object))))) #-clisp (defmethod initialize-internal-slot-functions ((slotd effective-virtual-slot-definition)) - (setf - (slot-value slotd 'reader-function) (compute-slot-reader-function slotd) - (slot-value slotd 'boundp-function) (compute-slot-boundp-function slotd) - (slot-value slotd 'writer-function) (compute-slot-writer-function slotd) - (slot-value slotd 'makunbound-function) (compute-slot-makunbound-function slotd)) - - #?-(sbcl>= 0 9 8)(initialize-internal-slot-gfs (slot-definition-name slotd))) + #?-(sbcl>= 0 9 8) + (initialize-internal-slot-gfs (slot-definition-name slotd))) #-clisp (defmethod compute-slot-accessor-info ((slotd effective-virtual-slot-definition) type gf) nil) + (defun slot-bound-in-some-p (instances slot) (find-if #'(lambda (ob) @@ -195,73 +282,58 @@ (defmethod compute-effective-slot-definition-initargs ((class virtual-slots-clas (append '(:special t) (call-next-method))) (t (call-next-method)))) +(defmacro vsc-slot-x-using-class (x x-slot-name computer &key allow-string-fun-p) + (let ((generic-name (intern (concatenate 'string + "SLOT-" (string x) "-USING-CLASS")))) + `(defmethod ,generic-name + ((class virtual-slots-class) (object virtual-slots-object) + (slotd effective-virtual-slot-definition)) + (unless (and (slot-boundp slotd ',x-slot-name) + ,@(when allow-string-fun-p + `((not + (stringp (slot-value slotd ',x-slot-name)))))) + (setf (slot-value slotd ',x-slot-name) (,computer slotd))) + (funcall (slot-value slotd ',x-slot-name) object)))) + +(vsc-slot-x-using-class value getter compute-slot-reader-function + :allow-string-fun-p t) +(vsc-slot-x-using-class boundp boundp-function compute-slot-boundp-function) +(vsc-slot-x-using-class makunbound makunbound-function + compute-slot-makunbound-function) + +(defmethod (setf slot-value-using-class) (value (class virtual-slots-class) + (object virtual-slots-object) + (slotd effective-virtual-slot-definition)) + (unless (slot-boundp slotd 'setter) + (setf (slot-value slotd 'setter) (compute-slot-writer-function slotd))) + (funcall (slot-value slotd 'setter) value object)) + +;; In CLISP and SBCL (0.9.15 or newler) a class may not have been +;; finalized when update-slots are called. So to avoid the possibility +;; of finalize-instance being called recursivly we have to delay the +;; initialization of slot functions until after an instance has been +;; created. +;; 2007-11-08: done this for all implementations +;; #?(or (sbcl>= 0 9 15) (featurep :clisp)) +(defmethod slot-unbound (class (slotd effective-virtual-slot-definition) (name (eql 'reader-function))) + (declare (ignore class)) + (setf (slot-value slotd name) (compute-slot-reader-function slotd))) + +;; #?(or (sbcl>= 0 9 15) (featurep :clisp)) +(defmethod slot-unbound (class (slotd effective-virtual-slot-definition) (name (eql 'boundp-function))) + (declare (ignore class)) + (setf (slot-value slotd name) (compute-slot-boundp-function slotd))) + +;; #?(or (sbcl>= 0 9 15) (featurep :clisp)) +(defmethod slot-unbound (class (slotd effective-virtual-slot-definition) (name (eql 'writer-function))) + (declare (ignore class)) + (setf (slot-value slotd name) (compute-slot-writer-function slotd))) + +;; #?(or (sbcl>= 0 9 15) (featurep :clisp)) +(defmethod slot-unbound (class (slotd effective-virtual-slot-definition) (name (eql 'makunbound-function))) + (declare (ignore class)) + (setf (slot-value slotd name) (compute-slot-makunbound-function slotd))) -(defmethod slot-value-using-class - ((class virtual-slots-class) (object standard-object) - (slotd effective-virtual-slot-definition)) - ;; This isn't optimal when we have an unbound value, as the reader - ;; function gets invoke twice - (if (funcall (slot-value slotd 'boundp-function) object) - (funcall (slot-value slotd 'reader-function) object) - (slot-unbound class object (slot-definition-name slotd)))) - -(defmethod slot-boundp-using-class - ((class virtual-slots-class) (object standard-object) - (slotd effective-virtual-slot-definition)) - (handler-case - (funcall (slot-value slotd 'boundp-function) object) - (unreadable-slot (condition) - (declare (ignore condition)) - nil))) - -(defmethod (setf slot-value-using-class) - (value (class virtual-slots-class) (object standard-object) - (slotd effective-virtual-slot-definition)) - (funcall (slot-value slotd 'writer-function) value object)) - -(defmethod slot-makunbound-using-class - ((class virtual-slots-class) (object standard-object) - (slotd effective-virtual-slot-definition)) - (funcall (slot-value slotd 'makunbound-function) object)) - - -;; In CLISP a class may not have been finalized when update-slots are -;; called. So to avoid the possibility of finalize-instance beeing -;; called recursivly we have to delay the initialization of slot -;; functions until after an instance has been created. We therefor do -;; it in around methods for the generic functions used to access -;; slots. -#+clisp -(defmethod slot-value-using-class :around ((class virtual-slots-class) (object standard-object) (slotd effective-virtual-slot-definition)) - (unless (slot-boundp slotd 'reader-function) - (setf - (slot-value slotd 'reader-function) (compute-slot-reader-function slotd) - (slot-value slotd 'boundp-function) (compute-slot-boundp-function slotd))) - (call-next-method)) - -#+clisp -(defmethod slot-boundp-using-class :around ((class virtual-slots-class) (object standard-object) (slotd effective-virtual-slot-definition)) - (unless (slot-boundp slotd 'boundp-function) - (setf - (slot-value slotd 'reader-function) (compute-slot-reader-function slotd) - (slot-value slotd 'boundp-function) (compute-slot-boundp-function slotd))) - (call-next-method)) - -#+clisp -(defmethod (setf slot-value-using-class) :around (value (class virtual-slots-class) (object standard-object) (slotd effective-virtual-slot-definition)) - (declare (ignore value)) - (unless (slot-boundp slotd 'writer-function) - (setf - (slot-value slotd 'writer-function) (compute-slot-writer-function slotd))) - (call-next-method)) - -#+clisp -(defmethod slot-makunbound-using-class :around ((class virtual-slots-class) (object standard-object) (slotd effective-virtual-slot-definition)) - (unless (slot-boundp slotd 'makunbound-function) - (setf - (slot-value slotd 'makunbound-function) - (compute-slot-makunbound-function slotd))) - (call-next-method)) (defmethod validate-superclass ((class virtual-slots-class) (super standard-class)) @@ -276,13 +348,9 @@ (defmethod slot-definition-special ((slotd standard-effective-slot-definition)) nil) -(defclass virtual-slots-object (standard-object) - ()) - - ;;; To determine if a slot should be initialized with the initform, ;;; CLISP checks whether it is unbound or not. This doesn't work with -;;; virtual slots which does not have an unbound state, so we have to +;;; virtual slots that does not have an unbound state, so we have to ;;; implement initform initialization in a way similar to how it is ;;; done in PCL. #+clisp