1 ;;; luna.el --- tiny OOP system kernel
3 ;; Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
6 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
9 ;; This file is part of FLIM (Faithful Library about Internet Message).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
30 (eval-when-compile (require 'static))
32 (static-condition-case nil
33 :symbol-for-testing-whether-colon-keyword-is-available-or-not
35 (defconst :before ':before)
36 (defconst :after ':after)
37 (defconst :around ':around)))
39 (defmacro luna-find-class (name)
40 "Return the luna-class of the given NAME."
41 `(get ,name 'luna-class))
43 (defmacro luna-set-class (name class)
44 `(put ,name 'luna-class ,class))
46 (defmacro luna-class-obarray (class)
49 (defmacro luna-class-parents (class)
52 (defmacro luna-class-number-of-slots (class)
55 (defmacro luna-define-class (type &optional parents slots)
56 "Define TYPE as a luna-class.
57 If PARENTS is specified, TYPE inherits PARENTS.
58 Each parent must be name of luna-class (symbol).
59 If SLOTS is specified, TYPE will be defined to have them."
60 `(luna-define-class-function ',type ',(append parents '(standard-object))
63 (defun luna-define-class-function (type &optional parents slots)
64 (static-condition-case nil
65 :symbol-for-testing-whether-colon-keyword-is-available-or-not
69 (setq key (intern (format ":%s" slot)))
71 (let ((oa (make-vector 31 0))
77 (setq parent (pop rest)
79 (mapatoms (lambda (sym)
80 (when (setq j (get sym 'luna-slot-index))
81 (setq name (symbol-name sym))
82 (unless (intern-soft name oa)
83 (put (intern name oa) 'luna-slot-index (+ j b))
86 (luna-class-obarray (luna-find-class parent)))
90 (setq name (symbol-name (pop rest)))
91 (unless (intern-soft name oa)
92 (put (intern name oa) 'luna-slot-index i)
95 (luna-set-class type (vector 'class oa parents i))
98 (defun luna-class-find-member (class member-name)
99 (or (stringp member-name)
100 (setq member-name (symbol-name member-name)))
101 (or (intern-soft member-name (luna-class-obarray class))
102 (let ((parents (luna-class-parents class))
106 (setq ret (luna-class-find-member
107 (luna-find-class (pop parents))
111 (defsubst luna-class-find-or-make-member (class member-name)
112 (or (stringp member-name)
113 (setq member-name (symbol-name member-name)))
114 (intern member-name (luna-class-obarray class)))
116 (defmacro luna-class-slot-index (class slot-name)
117 `(get (luna-class-find-member ,class ,slot-name) 'luna-slot-index))
119 (defmacro luna-slot-index (entity slot-name)
120 `(luna-class-slot-index (luna-find-class (luna-class-name ,entity))
123 (defsubst luna-slot-value (entity slot)
124 "Return the value of SLOT of ENTITY."
125 (aref entity (luna-slot-index entity slot)))
127 (defsubst luna-set-slot-value (entity slot value)
128 "Store VALUE into SLOT of ENTITY."
129 (aset entity (luna-slot-index entity slot) value))
131 (defmacro luna-define-method (name &rest definition)
132 "Define NAME as a method function of a class.
134 Usage of this macro follows:
136 (luna-define-method NAME [METHOD-QUALIFIER] ARGLIST [DOCSTRING] BODY...)
138 NAME is the name of method.
140 Optional argument METHOD-QUALIFIER must be :before, :after or :around.
141 If it is :before / :after, the method is called before / after a
142 method of parent class is finished. ARGLIST is like an argument list
143 of lambda, but (car ARGLIST) must be specialized parameter. (car (car
144 ARGLIST)) is name of variable and \(nth 1 (car ARGLIST)) is name of
147 Optional argument DOCSTRING is the documentation of method.
149 BODY is the body of method."
150 (let ((method-qualifier (pop definition))
151 args specializer class self)
152 (if (memq method-qualifier '(:before :after :around))
153 (setq args (pop definition))
154 (setq args method-qualifier
155 method-qualifier nil)
157 (setq specializer (car args)
158 class (nth 1 specializer)
159 self (car specializer))
160 `(let ((func (lambda ,(if self
161 (cons self (cdr args))
164 (sym (luna-class-find-or-make-member
165 (luna-find-class ',class) ',name)))
167 (put sym 'luna-method-qualifier ,method-qualifier)
170 (put 'luna-define-method 'lisp-indent-function 'defun)
172 (def-edebug-spec luna-define-method
173 (&define name [&optional &or ":before" ":after" ":around"]
176 [&optional ["&optional" arg &rest arg]]
177 &optional ["&rest" arg]
181 (defun luna-class-find-parents-functions (class service)
182 (let ((parents (luna-class-parents class))
186 (setq ret (luna-class-find-functions
187 (luna-find-class (pop parents))
191 (defun luna-class-find-functions (class service)
192 (let ((sym (luna-class-find-member class service)))
194 (cond ((eq (get sym 'luna-method-qualifier) :before)
195 (cons (symbol-function sym)
196 (luna-class-find-parents-functions class service))
198 ((eq (get sym 'luna-method-qualifier) :after)
199 (nconc (luna-class-find-parents-functions class service)
200 (list (symbol-function sym)))
202 ((eq (get sym 'luna-method-qualifier) :around)
203 (cons sym (luna-class-find-parents-functions class service))
206 (list (symbol-function sym))
208 (luna-class-find-parents-functions class service)
211 (defmacro luna-find-functions (entity service)
212 `(luna-class-find-functions (luna-find-class (luna-class-name ,entity))
215 (defsubst luna-send (entity message &rest luna-current-method-arguments)
216 "Send MESSAGE to ENTITY, and return the result.
217 LUNA-CURRENT-METHOD-ARGUMENTS is arguments of the MESSAGE."
218 (let ((luna-next-methods (luna-find-functions entity message))
220 luna-previous-return-value)
221 (while (and luna-next-methods
223 (setq luna-current-method (pop luna-next-methods)
224 luna-previous-return-value
225 (apply luna-current-method
226 luna-current-method-arguments))
227 (if (symbolp luna-current-method)
228 (not (eq (get luna-current-method
229 'luna-method-qualifier) :around))
231 luna-previous-return-value))
234 (defvar luna-next-methods nil)
235 (defvar luna-current-method-arguments nil)
238 (defun luna-call-next-method ()
239 "Call the next method in a method with :around qualifier."
240 (let (luna-current-method
241 luna-previous-return-value)
242 (while (and luna-next-methods
244 (setq luna-current-method (pop luna-next-methods)
245 luna-previous-return-value
246 (apply luna-current-method
247 luna-current-method-arguments))
248 (if (symbolp luna-current-method)
249 (not (eq (get luna-current-method
250 'luna-method-qualifier) :around))
252 luna-previous-return-value))
254 (defmacro luna-class-name (entity)
255 "Return class-name of the ENTITY."
258 (defmacro luna-set-class-name (entity name)
259 `(aset ,entity 0 ,name))
261 (defmacro luna-get-obarray (entity)
264 (defmacro luna-set-obarray (entity obarray)
265 `(aset ,entity 1 ,obarray))
267 (defun luna-make-entity (type &rest init-args)
268 "Make instance of luna-class TYPE and return it.
269 If INIT-ARGS is specified, it is used as initial values of the slots.
270 It must be plist and each slot name must have prefix `:'."
271 (let* ((c (get type 'luna-class))
272 (v (make-vector (luna-class-number-of-slots c) nil)))
273 (luna-set-class-name v type)
274 (luna-set-obarray v (make-vector 7 0))
275 (apply #'luna-send v 'initialize-instance v init-args)
278 (defsubst luna-arglist-to-arguments (arglist)
281 (let ((arg (car arglist)))
282 (or (memq arg '(&optional &rest))
283 (setq dest (cons arg dest)))
285 (setq arglist (cdr arglist)))
288 (defmacro luna-define-generic (name args &optional doc)
289 "Define generic-function NAME.
290 ARGS is argument of and DOC is DOC-string."
292 `(defun ,(intern (symbol-name name)) ,args
294 (luna-send ,(car args) ',name
295 ,@(luna-arglist-to-arguments args))
297 `(defun ,(intern (symbol-name name)) ,args
298 (luna-send ,(car args) ',name
299 ,@(luna-arglist-to-arguments args))
302 (put 'luna-define-generic 'lisp-indent-function 'defun)
304 (defun luna-define-internal-accessors (class-name)
305 "Define internal accessors for an entity of CLASS-NAME."
306 (let ((entity-class (luna-find-class class-name))
307 parents parent-class)
310 (if (luna-class-slot-index entity-class slot)
312 (setq parents (luna-class-parents entity-class))
314 (setq parent-class (luna-find-class (car parents)))
315 (if (luna-class-slot-index parent-class slot)
316 (throw 'derived nil))
317 (setq parents (cdr parents))
321 (defmacro ,(intern (format "%s-%s-internal"
325 ,(luna-class-slot-index entity-class
326 (intern (symbol-name slot)))
328 (defmacro ,(intern (format "%s-set-%s-internal"
332 ,(luna-class-slot-index
333 entity-class (intern (symbol-name slot)))
337 (luna-class-obarray entity-class))))
339 (luna-define-class-function 'standard-object)
341 (luna-define-method initialize-instance ((entity standard-object)
343 (let* ((c (luna-find-class (luna-class-name entity)))
344 (oa (luna-class-obarray c))
347 (setq s (intern-soft (substring (symbol-name (pop init-args)) 1) oa)
350 (aset entity (get s 'luna-slot-index) i)