1 ;;; luna.el --- tiny OOP system kernel
3 ;; Copyright (C) 1999,2000 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 (eval-when-compile (require 'cl))
29 (eval-when-compile (require 'static))
31 (static-condition-case nil
32 :symbol-for-testing-whether-colon-keyword-is-available-or-not
34 (defconst :before ':before)
35 (defconst :after ':after)
36 (defconst :around ':around)))
42 (defmacro luna-find-class (name)
43 "Return the luna-class of the given NAME."
44 `(get ,name 'luna-class))
46 (defmacro luna-set-class (name class)
47 `(put ,name 'luna-class ,class))
49 (defmacro luna-class-obarray (class)
52 (defmacro luna-class-parents (class)
55 (defmacro luna-class-number-of-slots (class)
58 (defmacro luna-define-class (type &optional parents slots)
59 "Define TYPE as a luna-class.
60 If PARENTS is specified, TYPE inherits PARENTS.
61 Each parent must be name of luna-class (symbol).
62 If SLOTS is specified, TYPE will be defined to have them."
63 `(luna-define-class-function ',type ',(append parents '(standard-object))
66 (defun luna-define-class-function (type &optional parents slots)
67 (static-condition-case nil
68 :symbol-for-testing-whether-colon-keyword-is-available-or-not
72 (setq key (intern (format ":%s" slot)))
74 (let ((oa (make-vector 31 0))
80 (setq parent (pop rest)
82 (mapatoms (lambda (sym)
83 (when (setq j (get sym 'luna-slot-index))
84 (setq name (symbol-name sym))
85 (unless (intern-soft name oa)
86 (put (intern name oa) 'luna-slot-index (+ j b))
89 (luna-class-obarray (luna-find-class parent)))
93 (setq name (symbol-name (pop rest)))
94 (unless (intern-soft name oa)
95 (put (intern name oa) 'luna-slot-index i)
98 (luna-set-class type (vector 'class oa parents i))
101 (defun luna-class-find-member (class member-name)
102 (or (stringp member-name)
103 (setq member-name (symbol-name member-name)))
104 (or (intern-soft member-name (luna-class-obarray class))
105 (let ((parents (luna-class-parents class))
109 (setq ret (luna-class-find-member
110 (luna-find-class (pop parents))
114 (defsubst luna-class-find-or-make-member (class member-name)
115 (or (stringp member-name)
116 (setq member-name (symbol-name member-name)))
117 (intern member-name (luna-class-obarray class)))
119 (defmacro luna-class-slot-index (class slot-name)
120 `(get (luna-class-find-member ,class ,slot-name) 'luna-slot-index))
122 (defmacro luna-define-method (name &rest definition)
123 "Define NAME as a method function of a class.
125 Usage of this macro follows:
127 (luna-define-method NAME [METHOD-QUALIFIER] ARGLIST [DOCSTRING] BODY...)
129 NAME is the name of method.
131 Optional argument METHOD-QUALIFIER must be :before, :after or :around.
132 If it is :before / :after, the method is called before / after a
133 method of parent class is finished. ARGLIST is like an argument list
134 of lambda, but (car ARGLIST) must be specialized parameter. (car (car
135 ARGLIST)) is name of variable and \(nth 1 (car ARGLIST)) is name of
138 Optional argument DOCSTRING is the documentation of method.
140 BODY is the body of method."
141 (let ((method-qualifier (pop definition))
142 args specializer class self)
143 (if (memq method-qualifier '(:before :after :around))
144 (setq args (pop definition))
145 (setq args method-qualifier
146 method-qualifier nil)
148 (setq specializer (car args)
149 class (nth 1 specializer)
150 self (car specializer))
151 `(let ((func (lambda ,(if self
152 (cons self (cdr args))
155 (sym (luna-class-find-or-make-member
156 (luna-find-class ',class) ',name)))
158 (put sym 'luna-method-qualifier ,method-qualifier)
161 (put 'luna-define-method 'lisp-indent-function 'defun)
163 (def-edebug-spec luna-define-method
164 (&define name [&optional &or ":before" ":after" ":around"]
167 [&optional ["&optional" arg &rest arg]]
168 &optional ["&rest" arg]
172 (defun luna-class-find-parents-functions (class service)
173 (let ((parents (luna-class-parents class))
177 (setq ret (luna-class-find-functions
178 (luna-find-class (pop parents))
182 (defun luna-class-find-functions (class service)
183 (let ((sym (luna-class-find-member class service)))
185 (cond ((eq (get sym 'luna-method-qualifier) :before)
186 (cons (symbol-function sym)
187 (luna-class-find-parents-functions class service))
189 ((eq (get sym 'luna-method-qualifier) :after)
190 (nconc (luna-class-find-parents-functions class service)
191 (list (symbol-function sym)))
193 ((eq (get sym 'luna-method-qualifier) :around)
194 (cons sym (luna-class-find-parents-functions class service))
197 (list (symbol-function sym))
199 (luna-class-find-parents-functions class service)
203 ;;; @ instance (entity)
206 (defmacro luna-class-name (entity)
207 "Return class-name of the ENTITY."
210 (defmacro luna-set-class-name (entity name)
211 `(aset ,entity 0 ,name))
213 (defmacro luna-get-obarray (entity)
216 (defmacro luna-set-obarray (entity obarray)
217 `(aset ,entity 1 ,obarray))
219 (defmacro luna-slot-index (entity slot-name)
220 `(luna-class-slot-index (luna-find-class (luna-class-name ,entity))
223 (defsubst luna-slot-value (entity slot)
224 "Return the value of SLOT of ENTITY."
225 (aref entity (luna-slot-index entity slot)))
227 (defsubst luna-set-slot-value (entity slot value)
228 "Store VALUE into SLOT of ENTITY."
229 (aset entity (luna-slot-index entity slot) value))
231 (defmacro luna-find-functions (entity service)
232 `(luna-class-find-functions (luna-find-class (luna-class-name ,entity))
235 (defsubst luna-send (entity message &rest luna-current-method-arguments)
236 "Send MESSAGE to ENTITY, and return the result.
237 LUNA-CURRENT-METHOD-ARGUMENTS is arguments of the MESSAGE."
238 (let ((luna-next-methods (luna-find-functions entity message))
240 luna-previous-return-value)
241 (while (and luna-next-methods
243 (setq luna-current-method (pop luna-next-methods)
244 luna-previous-return-value
245 (apply luna-current-method
246 luna-current-method-arguments))
247 (if (symbolp luna-current-method)
248 (not (eq (get luna-current-method
249 'luna-method-qualifier) :around))
251 luna-previous-return-value))
254 (defvar luna-next-methods nil)
255 (defvar luna-current-method-arguments nil)
258 (defun luna-call-next-method ()
259 "Call the next method in a method with :around qualifier."
260 (let (luna-current-method
261 luna-previous-return-value)
262 (while (and luna-next-methods
264 (setq luna-current-method (pop luna-next-methods)
265 luna-previous-return-value
266 (apply luna-current-method
267 luna-current-method-arguments))
268 (if (symbolp luna-current-method)
269 (not (eq (get luna-current-method
270 'luna-method-qualifier) :around))
272 luna-previous-return-value))
274 (defun luna-make-entity (type &rest init-args)
275 "Make instance of luna-class TYPE and return it.
276 If INIT-ARGS is specified, it is used as initial values of the slots.
277 It must be plist and each slot name must have prefix `:'."
278 (let* ((c (get type 'luna-class))
279 (v (make-vector (luna-class-number-of-slots c) nil)))
280 (luna-set-class-name v type)
281 (luna-set-obarray v (make-vector 7 0))
282 (apply #'luna-send v 'initialize-instance v init-args)
286 ;;; @ interface (generic function)
289 (defsubst luna-arglist-to-arguments (arglist)
292 (let ((arg (car arglist)))
293 (or (memq arg '(&optional &rest))
294 (setq dest (cons arg dest)))
296 (setq arglist (cdr arglist)))
299 (defmacro luna-define-generic (name args &optional doc)
300 "Define generic-function NAME.
301 ARGS is argument of and DOC is DOC-string."
303 `(defun ,(intern (symbol-name name)) ,args
305 (luna-send ,(car args) ',name
306 ,@(luna-arglist-to-arguments args))
308 `(defun ,(intern (symbol-name name)) ,args
309 (luna-send ,(car args) ',name
310 ,@(luna-arglist-to-arguments args))
313 (put 'luna-define-generic 'lisp-indent-function 'defun)
319 (defun luna-define-internal-accessors (class-name)
320 "Define internal accessors for an entity of CLASS-NAME."
321 (let ((entity-class (luna-find-class class-name))
322 parents parent-class)
325 (if (luna-class-slot-index entity-class slot)
327 (setq parents (luna-class-parents entity-class))
329 (setq parent-class (luna-find-class (car parents)))
330 (if (luna-class-slot-index parent-class slot)
331 (throw 'derived nil))
332 (setq parents (cdr parents))
336 (defmacro ,(intern (format "%s-%s-internal"
340 ,(luna-class-slot-index entity-class
341 (intern (symbol-name slot)))
343 (defmacro ,(intern (format "%s-set-%s-internal"
347 ,(luna-class-slot-index
348 entity-class (intern (symbol-name slot)))
352 (luna-class-obarray entity-class))))
355 ;;; @ standard object
358 (luna-define-class-function 'standard-object)
360 (luna-define-method initialize-instance ((entity standard-object)
362 (let* ((c (luna-find-class (luna-class-name entity)))
363 (oa (luna-class-obarray c))
366 (setq s (intern-soft (substring (symbol-name (pop init-args)) 1) oa)
369 (aset entity (get s 'luna-slot-index) i)