1 ;;;; miscellaneous side-effectful tests of the MOP
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 ;;;; Note that the MOP is not in an entirely supported state.
15 ;;;; However, this seems a good a way as any of ensuring that we have
18 ;;; This is basically the DYNAMIC-SLOT-CLASS example from AMOP, with
19 ;;; fixups for running in the full MOP rather than closette: SLOTDs
20 ;;; instead of slot-names, and so on.
22 (defpackage "TEST" (:use "CL" "SB-MOP"))
25 (defclass dynamic-slot-class (standard-class) ())
27 (defmethod validate-superclass
28 ((class dynamic-slot-class) (super standard-class))
31 (defmethod compute-effective-slot-definition
32 ((class dynamic-slot-class) name direct-slots)
33 (let ((slot (call-next-method)))
34 (setf (slot-definition-allocation slot) :dynamic)
37 (defun dynamic-slot-p (slot)
38 (eq (slot-definition-allocation slot) :dynamic))
40 (let ((table (make-hash-table)))
42 (defun allocate-table-entry (instance)
43 (setf (gethash instance table) ()))
45 (defun read-dynamic-slot-value (instance slot-name)
46 (let* ((alist (gethash instance table))
47 (entry (assoc slot-name alist)))
49 (error "slot ~S unbound in ~S" slot-name instance)
52 (defun write-dynamic-slot-value (new-value instance slot-name)
53 (let* ((alist (gethash instance table))
54 (entry (assoc slot-name alist)))
56 (push `(,slot-name . ,new-value)
57 (gethash instance table))
58 (setf (cdr entry) new-value))
61 (defun dynamic-slot-boundp (instance slot-name)
62 (let* ((alist (gethash instance table))
63 (entry (assoc slot-name alist)))
66 (defun dynamic-slot-makunbound (instance slot-name)
67 (let* ((alist (gethash instance table))
68 (entry (assoc slot-name alist)))
70 (setf (gethash instance table) (delete entry alist))))
75 (defmethod allocate-instance ((class dynamic-slot-class) &key)
76 (let ((instance (call-next-method)))
77 (allocate-table-entry instance)
80 (defmethod slot-value-using-class ((class dynamic-slot-class)
82 (let ((slot (find slotd (class-slots class))))
84 (read-dynamic-slot-value instance (slot-definition-name slotd))
87 (defmethod (setf slot-value-using-class) (new-value (class dynamic-slot-class)
89 (let ((slot (find slotd (class-slots class))))
91 (write-dynamic-slot-value new-value instance (slot-definition-name slotd))
94 (defmethod slot-boundp-using-class ((class dynamic-slot-class)
96 (let ((slot (find slotd (class-slots class))))
98 (dynamic-slot-boundp instance (slot-definition-name slotd))
101 (defmethod slot-makunbound-using-class ((class dynamic-slot-class)
103 (let ((slot (find slotd (class-slots class))))
105 (dynamic-slot-makunbound instance (slot-definition-name slotd))
106 (call-next-method))))
108 (defclass test-class-1 ()
109 ((slot1 :initarg :slot1)
110 (slot2 :initarg :slot2 :initform nil))
111 (:metaclass dynamic-slot-class))
113 (defclass test-class-2 (test-class-1)
114 ((slot2 :initarg :slot2 :initform t)
115 (slot3 :initarg :slot3))
116 (:metaclass dynamic-slot-class))
118 (defvar *one* (make-instance 'test-class-1))
119 (defvar *two* (make-instance 'test-class-2 :slot3 1))
121 (assert (not (slot-boundp *one* 'slot1)))
122 (assert (null (slot-value *one* 'slot2)))
123 (assert (eq t (slot-value *two* 'slot2)))
124 (assert (= 1 (slot-value *two* 'slot3)))