| 1 |
;;; |
| 2 |
;;; object.scm - object system |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(declare) ;; a dummy form to suppress generation of "sci" file |
| 35 |
|
| 36 |
;; preparing inline stub code |
| 37 |
(inline-stub |
| 38 |
(.include "gauche/priv/configP.h" |
| 39 |
"gauche/priv/classP.h" |
| 40 |
"gauche/vminsn.h") |
| 41 |
(declare-stub-type <slot-accessor> "ScmSlotAccessor*") |
| 42 |
(declare-stub-type <generic> "ScmGeneric*") |
| 43 |
(declare-stub-type <method> "ScmMethod*") |
| 44 |
) |
| 45 |
|
| 46 |
;; This module is not meant to be `use'd. It is just to hide |
| 47 |
;; auxiliary procedures from the rest of the system. The necessary |
| 48 |
;; bindings are injected into 'gauche' module at the initialization time. |
| 49 |
(define-module gauche.object) |
| 50 |
(select-module gauche.object) |
| 51 |
|
| 52 |
;; TRANSIENT: If GAUCHE_KEYWORD_IS_SYMBOL, we need to rename keywords as well |
| 53 |
;; to preserve hygiene. The symbol? check allows us to pass a keyword to |
| 54 |
;; %id, regardless of GAUCHE_KEYWORD_IS_SYMBOL setting. |
| 55 |
(define (%id name) |
| 56 |
(if (symbol? name) |
| 57 |
((with-module gauche.internal make-identifier) |
| 58 |
name (find-module 'gauche.object) '()) |
| 59 |
name)) |
| 60 |
|
| 61 |
;;; I'm trying to make MOP as close to STklos and Goops as possible. |
| 62 |
;;; The perfect compatibility can't be done since the underlying implemenation |
| 63 |
;;; in C differs a bit. |
| 64 |
|
| 65 |
;; Bootstrapping "make" |
| 66 |
;; We already have generic-function for "make" defined in C. Just wanted |
| 67 |
;; to make a method specialized for <class>, i.e. the most common "make". |
| 68 |
;; However, we can't say (make <method> ...) before we have a make method. |
| 69 |
;; So we have to "hard wire" the method creation. |
| 70 |
|
| 71 |
(let ([%make (^[class . initargs] |
| 72 |
(rlet1 obj (allocate-instance class initargs) |
| 73 |
(initialize obj initargs)))] |
| 74 |
[body (^[class initargs next-method] |
| 75 |
(rlet1 obj (allocate-instance class initargs) |
| 76 |
(initialize obj initargs)))]) |
| 77 |
(add-method! make |
| 78 |
(%make <method> |
| 79 |
:generic make |
| 80 |
:specializers (list <class>) |
| 81 |
:lambda-list '(class . initargs) |
| 82 |
:body body))) |
| 83 |
|
| 84 |
;;---------------------------------------------------------------- |
| 85 |
;; Generic function |
| 86 |
;; |
| 87 |
|
| 88 |
;; Called from define-generic macro expander in libmacro.scm |
| 89 |
(define (%expand-define-generic name opts) |
| 90 |
(receive (true-name getter-name) (%check-setter-name name) |
| 91 |
(let ([class (get-keyword :class opts <generic>)] |
| 92 |
[other (delete-keyword :class opts)]) |
| 93 |
(if getter-name |
| 94 |
(quasirename %id |
| 95 |
`(define ,true-name |
| 96 |
(rlet1 ,true-name (make ,class ':name ',true-name ,@other) |
| 97 |
(set! (setter ,getter-name) ,true-name)))) |
| 98 |
(quasirename %id |
| 99 |
`(define ,true-name |
| 100 |
(make ,class ':name ',true-name ,@other))))))) |
| 101 |
|
| 102 |
;; allow (setter name) type declaration |
| 103 |
(define (%check-setter-name name) |
| 104 |
(cond [(identifier? name) (values name #f)] |
| 105 |
[(and (pair? name) (eq? (car name) 'setter) |
| 106 |
(pair? (cdr name)) (identifier? (cadr name)) |
| 107 |
(null? (cddr name))) |
| 108 |
(values (%make-setter-name (cadr name)) (cadr name))] |
| 109 |
[else (error "Bad name for generic function or method" name)])) |
| 110 |
|
| 111 |
(define (%make-setter-name name) |
| 112 |
(string->symbol (format #f "setter of ~a" name))) |
| 113 |
|
| 114 |
;; These are hidden procedure in gauche.object, for now. |
| 115 |
(define-cproc generic-seal! (gf::<generic>) ::<void> |
| 116 |
(set! (SCM_PROCEDURE_CONSTANT gf) TRUE)) |
| 117 |
(define-cproc generic-unseal! (gf::<generic>) ::<void> |
| 118 |
(set! (SCM_PROCEDURE_CONSTANT gf) FALSE)) |
| 119 |
|
| 120 |
;;---------------------------------------------------------------- |
| 121 |
;; Method |
| 122 |
;; |
| 123 |
|
| 124 |
;; Called from define-method macro expander in libmacro.scm |
| 125 |
(define (%expand-define-method name quals specs body form) |
| 126 |
;; check qualifiers. we only support :locked for now |
| 127 |
;; TRANSIENT: Must use hygienic compare. |
| 128 |
(if-let1 bad (find (^q (not (memq q '(:locked)))) quals) |
| 129 |
(error "syntax-error: unsupported method qualifier:" bad)) |
| 130 |
|
| 131 |
;; classify arguments to required, rest, and optionals |
| 132 |
;; ((a <x>) b (c <y>)) => r:((a <x>) b (c <y>)) r:#f o:#f |
| 133 |
;; ((a <x>) b . c) => r:((a <x>) b) r:c o:#f |
| 134 |
;; ((a <x>) b :optional x :key y) |
| 135 |
;; => r:((a <x>) b) r:#:G01 o:(:optional x :key y)) |
| 136 |
(receive (reqs rest opts) |
| 137 |
(let loop ([ss specs] [rs '()]) |
| 138 |
(cond [(null? ss) (values (reverse rs) #f #f)] |
| 139 |
[(not (pair? ss)) (values (reverse rs) ss #f)] |
| 140 |
[(keyword? (car ss)) (values (reverse rs) (gensym) ss)] |
| 141 |
[else (loop (cdr ss) (cons (car ss) rs))])) |
| 142 |
(let* ([specializers (map (^s (if (pair? s) (cadr s) (%id'<top>))) reqs)] |
| 143 |
[reqargs (map (^s (if (pair? s) (car s) s)) reqs)] |
| 144 |
[lambda-list (if rest `(,@reqargs . ,rest) reqargs)] |
| 145 |
[real-args (if rest |
| 146 |
`(,@reqargs ,rest next-method) |
| 147 |
`(,@reqargs next-method))] |
| 148 |
[real-body ((with-module gauche.internal with-source-info) |
| 149 |
(if opts |
| 150 |
(quasirename %id |
| 151 |
`(lambda ,real-args |
| 152 |
(apply (lambda ,opts ,@body) ,rest))) |
| 153 |
(quasirename %id |
| 154 |
`(lambda ,real-args ,@body))) |
| 155 |
form)] |
| 156 |
;; We bind method body to an artificially weird module-local name, |
| 157 |
;; so that test-module can find and scan the method's body. |
| 158 |
[method-unique-name (gensym |
| 159 |
(symbol->string |
| 160 |
(symbol-append (unwrap-syntax name) |
| 161 |
(unwrap-syntax specs) |
| 162 |
'|.|)))] |
| 163 |
) |
| 164 |
(receive (true-name getter-name) (%check-setter-name name) |
| 165 |
(let1 gf (gensym "gf") |
| 166 |
(quasirename %id |
| 167 |
`(define ,method-unique-name |
| 168 |
(rlet1 ,method-unique-name ,real-body |
| 169 |
(let1 ,gf (%ensure-generic-function ',true-name (current-module)) |
| 170 |
(add-method! ,gf (make <method> |
| 171 |
':generic ,gf |
| 172 |
':specializers (list ,@specializers) |
| 173 |
':lambda-list ',lambda-list |
| 174 |
':method-locked (boolean (memq ':locked ',quals)) |
| 175 |
':body ,method-unique-name)) |
| 176 |
,@(cond-list [getter-name |
| 177 |
(quasirename %id |
| 178 |
`(unless (has-setter? ,getter-name) |
| 179 |
(set! (setter ,getter-name) ,gf)))])))) |
| 180 |
)))))) |
| 181 |
|
| 182 |
(inline-stub |
| 183 |
;; internal for %ensure-generic-function |
| 184 |
(define-cfn call-fallback-proc (args::ScmObj* nargs::int gf::ScmGeneric*) |
| 185 |
:static |
| 186 |
(return (Scm_VMApply (SCM_OBJ (-> gf data)) (Scm_ArrayToList args nargs)))) |
| 187 |
|
| 188 |
;; Ensure a named generic function is defined within the given module. |
| 189 |
;; This is called from method definition (either explicit definition by |
| 190 |
;; define-method, or implicit definition by slot's :getter/:setter option). |
| 191 |
;; |
| 192 |
;; If NAME has a visible binding to a subr or a closure, we use it as |
| 193 |
;; a fallback method. NB: this feature is debatable, since it may breach |
| 194 |
;; the isolation of namespaces. We may drop this feature in future. |
| 195 |
;; |
| 196 |
;; After the call to this procedure, a binding of NAME is inserted in |
| 197 |
;; the current module. |
| 198 |
(define-cproc %ensure-generic-function (name::<symbol> module::<module>) |
| 199 |
(let* ([val (Scm_GlobalVariableRef module name 0)]) |
| 200 |
(cond |
| 201 |
[(Scm_TypeP val SCM_CLASS_GENERIC) |
| 202 |
(when (SCM_UNBOUNDP (Scm_GlobalVariableRef module name |
| 203 |
SCM_BINDING_STAY_IN_MODULE)) |
| 204 |
(Scm_Define module name val))] |
| 205 |
[else |
| 206 |
(if (or (SCM_SUBRP val) (SCM_CLOSUREP val)) |
| 207 |
(set! val (Scm_MakeBaseGeneric (SCM_OBJ name) call_fallback_proc val)) |
| 208 |
(set! val (Scm_MakeBaseGeneric (SCM_OBJ name) NULL NULL))) |
| 209 |
(Scm_Define module name val)]) ;redefine |
| 210 |
(return val))) |
| 211 |
|
| 212 |
(define-cproc %make-next-method (gf methods::<list> args::<list>) |
| 213 |
(let* ([argv::ScmObj*] [argc::ScmSize]) |
| 214 |
(unless (Scm_TypeP gf SCM_CLASS_GENERIC) |
| 215 |
(Scm_Error "generic function required, but got %S" gf)) |
| 216 |
(dolist [mp methods] |
| 217 |
(unless (Scm_TypeP mp SCM_CLASS_METHOD) |
| 218 |
(Scm_Error "method required, but got %S" mp))) |
| 219 |
(set! argv (Scm_ListToArray args (& argc) NULL TRUE)) |
| 220 |
(return (Scm_MakeNextMethod (SCM_GENERIC gf) methods argv argc |
| 221 |
FALSE FALSE)))) |
| 222 |
) |
| 223 |
|
| 224 |
;;---------------------------------------------------------------- |
| 225 |
;; Class |
| 226 |
;; |
| 227 |
|
| 228 |
;; TEMPORARY: We need to detect keywords such as :metaclass, :slots |
| 229 |
;; or :supers at macro-expansion time. If the define-class form |
| 230 |
;; is macro-generated, however, these keywords may be renamed, so |
| 231 |
;; we can't simply use get-keyword. This will eventually addressed |
| 232 |
;; when we rewrote define-class expander as macro. Meanwhile, |
| 233 |
;; this ad-hoc solution would work. |
| 234 |
|
| 235 |
(define (%get-keyword key args default) |
| 236 |
(let loop ((as args)) |
| 237 |
(cond [(null? as) default] |
| 238 |
[(null? (cdr as)) (error "Key-value list is not even:" args)] |
| 239 |
[(eqv? key (unwrap-syntax-1 (car as))) (cadr as)] |
| 240 |
[else (loop (cddr as))]))) |
| 241 |
|
| 242 |
;; Called from define-class macro expander in libmacro.scm |
| 243 |
(define (%expand-define-class name supers slots options) |
| 244 |
(let* ([metaclass (or (%get-keyword :metaclass options #f) |
| 245 |
(quasirename %id |
| 246 |
`(%get-default-metaclass (list ,@supers))))] |
| 247 |
[slot-defs (map %process-slot-definition slots)] |
| 248 |
[class (gensym)] |
| 249 |
[slot (gensym)]) |
| 250 |
(quasirename %id |
| 251 |
;; We expand this into define-inline, to make the binding inlinable. |
| 252 |
;; It allows the compiler to use the information of the class |
| 253 |
;; reference. Note that class redefinition does override the |
| 254 |
;; inlinable binding (which should be rare, though). We handle |
| 255 |
;; redefinition of class specially in gloc.c. |
| 256 |
`(define-inline ,name |
| 257 |
(rlet1 ,class (make ,metaclass |
| 258 |
':name ',name ':supers (list ,@supers) |
| 259 |
':slots (list ,@slot-defs) |
| 260 |
':defined-modules (list (current-module)) |
| 261 |
,@options) |
| 262 |
(when (%check-class-binding ',name (current-module)) |
| 263 |
(redefine-class! ,name ,class)) |
| 264 |
(for-each (lambda (,slot) |
| 265 |
(%make-accessor ,class ,slot (current-module))) |
| 266 |
(class-slots ,class))))))) |
| 267 |
|
| 268 |
(define (%process-slot-definition sdef) |
| 269 |
(if (pair? sdef) |
| 270 |
(let loop ([opts (cdr sdef)] [r '()]) |
| 271 |
(cond [(null? opts) (quasirename %id `(list ',(car sdef) ,@(reverse! r)))] |
| 272 |
[(not (and (pair? opts) (pair? (cdr opts)))) |
| 273 |
(error "bad slot specification:" sdef)] |
| 274 |
[else |
| 275 |
;; TRANSIENT: These comparison must be done hygienically, once |
| 276 |
;; we replace expander with er transformer. |
| 277 |
(case (car opts) |
| 278 |
[(:initform :init-form) |
| 279 |
(loop (cddr opts) |
| 280 |
(quasirename %id |
| 281 |
`((lambda () ,(cadr opts)) ':init-thunk ,@r)))] |
| 282 |
[(:getter :setter :accessor) |
| 283 |
(loop (cddr opts) |
| 284 |
(quasirename %id `(',(cadr opts) ',(car opts) ,@r)))] |
| 285 |
[else |
| 286 |
(loop (cddr opts) |
| 287 |
(quasirename %id `(,(cadr opts) ',(car opts) ,@r)))])])) |
| 288 |
(quasirename %id `'(,sdef)))) |
| 289 |
|
| 290 |
;; Determine default metaclass, that is a class inheriting all the metaclasses |
| 291 |
;; of supers. The idea is taken from stklos. The difference is that |
| 292 |
;; metaclass calculation is done at runtime in Gauche, while at compile-time |
| 293 |
;; in STklos. |
| 294 |
(define %get-default-metaclass |
| 295 |
(let1 generated-metas '() |
| 296 |
(define (find-metaclass metasupers) |
| 297 |
(cond [(assoc metasupers generated-metas) => cdr] |
| 298 |
[else (make-metaclass metasupers)])) |
| 299 |
(define (make-metaclass metasupers) |
| 300 |
(rlet1 meta (make <class> |
| 301 |
:supers metasupers :name (gensym "metaclass") :slots '()) |
| 302 |
(set! generated-metas (acons metasupers meta generated-metas)))) |
| 303 |
|
| 304 |
(^[supers] |
| 305 |
(if (null? supers) |
| 306 |
<class> |
| 307 |
(let* ([all-metas (map class-of supers)] |
| 308 |
[all-cpls (apply append |
| 309 |
(map (^m (cdr (class-precedence-list m))) |
| 310 |
all-metas))] |
| 311 |
[needed '()]) |
| 312 |
(dolist [m all-metas] |
| 313 |
(when (and (not (memq m all-cpls)) |
| 314 |
(not (memq m needed))) |
| 315 |
(set! needed (cons m needed)))) |
| 316 |
(if (null? (cdr needed)) |
| 317 |
(car needed) |
| 318 |
(find-metaclass (reverse! needed)))))) |
| 319 |
)) |
| 320 |
|
| 321 |
;;; |
| 322 |
;;; Method INITIALIZE (class <class>) initargs |
| 323 |
;;; |
| 324 |
|
| 325 |
(define-method initialize :locked ((class <class>) initargs) |
| 326 |
(next-method) |
| 327 |
(let* ([slots (%get-keyword :slots initargs '())] |
| 328 |
[sup (%get-keyword :supers initargs '())] |
| 329 |
;; NB: we always add <object> to the direct supers, for C defined |
| 330 |
;; base classes may not be inheriting from it. |
| 331 |
[supers (append sup (list <object>))]) |
| 332 |
;; The order of initialization is somewhat important, since calculation |
| 333 |
;; of values of some slots depends on the other slots. |
| 334 |
(slot-set! class 'direct-supers supers) |
| 335 |
(slot-set! class 'cpl (compute-cpl class)) |
| 336 |
(slot-set! class 'direct-slots |
| 337 |
(map (^s (if (pair? s) s (list s))) slots)) |
| 338 |
;; note: num-instance-slots is set up during compute-get-n-set. |
| 339 |
(let1 slots (compute-slots class) |
| 340 |
(slot-set! class 'slots slots) |
| 341 |
(slot-set! class 'accessors |
| 342 |
(map (^s ;; returns (name . #<slot-accessor>) |
| 343 |
(cons (car s) |
| 344 |
(compute-slot-accessor |
| 345 |
class s |
| 346 |
(compute-get-n-set class s)))) |
| 347 |
slots)) |
| 348 |
) |
| 349 |
;; bookkeeping for class redefinition |
| 350 |
(slot-set! class 'initargs initargs) |
| 351 |
(dolist [super supers] (%add-direct-subclass! super class)) |
| 352 |
(class-post-initialize class initargs) |
| 353 |
;; seal the class |
| 354 |
(%finish-class-initialization! class) |
| 355 |
)) |
| 356 |
|
| 357 |
;; This is a hook to tweak critical slots of class at initialization time. |
| 358 |
;; The core slots becomes immutable once class initialization is done, |
| 359 |
;; so any tweaks that requires to modify them needs to be implemented here. |
| 360 |
(define-method class-post-initialize ((class <class>) initargs) #f) |
| 361 |
|
| 362 |
(define (%make-accessor class slot module) |
| 363 |
(let* ([name (slot-definition-name slot)] |
| 364 |
[sa (class-slot-accessor class name)] |
| 365 |
[%getter (slot-definition-getter slot)] |
| 366 |
[%setter (slot-definition-setter slot)] |
| 367 |
[%accessor (slot-definition-accessor slot)]) |
| 368 |
|
| 369 |
(define (make-getter gf) |
| 370 |
(add-method! gf |
| 371 |
(make <accessor-method> |
| 372 |
:generic gf :specializers (list class) |
| 373 |
:slot-accessor sa :lambda-list '(obj) |
| 374 |
:body (^[obj next-method] #f) ;; dummy |
| 375 |
))) |
| 376 |
|
| 377 |
(define (make-setter gf) |
| 378 |
(add-method! gf |
| 379 |
(make <accessor-method> |
| 380 |
:generic gf :specializers (list class <top>) |
| 381 |
:slot-accessor sa :lambda-list '(obj val) |
| 382 |
:body (^[obj val next-method] #f) ;; dummy |
| 383 |
))) |
| 384 |
|
| 385 |
(when %getter |
| 386 |
(make-getter (%ensure-generic-function %getter module))) |
| 387 |
(when %setter |
| 388 |
(make-setter (%ensure-generic-function %setter module))) |
| 389 |
(when %accessor |
| 390 |
(let* ([gf (%ensure-generic-function %accessor module)] |
| 391 |
[gfs (or (setter gf) |
| 392 |
(%ensure-generic-function (%make-setter-name %accessor) |
| 393 |
module))]) |
| 394 |
(make-getter gf) |
| 395 |
(make-setter gfs) |
| 396 |
(set! (setter gf) gfs) |
| 397 |
)) |
| 398 |
)) |
| 399 |
|
| 400 |
;;; Method COMPUTE-SLOTS (class <class>) |
| 401 |
(define-method compute-slots :locked ((class <class>)) |
| 402 |
(let ([cpl (slot-ref class 'cpl)] |
| 403 |
[slots '()]) |
| 404 |
(dolist [c cpl] |
| 405 |
(dolist [slot (slot-ref c 'direct-slots)] |
| 406 |
(unless (assq (car slot) slots) |
| 407 |
(set! slots (cons slot slots))))) |
| 408 |
(reverse slots))) |
| 409 |
|
| 410 |
;;; Method COMPUTE-GET-N-SET (class <class>) slot |
| 411 |
;;; May return: |
| 412 |
;;; integer for instance slot |
| 413 |
;;; list (getter [setter [bound? [allocate?]]]) |
| 414 |
;;; slot accessor |
| 415 |
(define-method compute-get-n-set :locked ((class <class>) slot) |
| 416 |
|
| 417 |
;; NB: STklos ignores :initform slot option for class slots, but |
| 418 |
;; I think it's sometimes useful. |
| 419 |
(define (make-class-slot) |
| 420 |
(let* ([init-value (slot-definition-option slot :init-value (undefined))] |
| 421 |
[init-thunk (slot-definition-option slot :init-thunk #f)]) |
| 422 |
(if init-thunk |
| 423 |
(%make-class-slot (init-thunk)) |
| 424 |
(%make-class-slot init-value)))) |
| 425 |
|
| 426 |
(let ([slot-name (slot-definition-name slot)] |
| 427 |
[alloc (slot-definition-allocation slot)]) |
| 428 |
(case alloc |
| 429 |
[(:instance) |
| 430 |
(rlet1 num (slot-ref class 'num-instance-slots) |
| 431 |
(slot-set! class 'num-instance-slots (+ num 1)))] |
| 432 |
[(:class) |
| 433 |
(if (assq slot-name (class-direct-slots class)) |
| 434 |
(make-class-slot) |
| 435 |
(let loop ((cpl (class-precedence-list class))) |
| 436 |
(cond [(null? cpl) |
| 437 |
(error "something wrong with slot inheritance of" class)] |
| 438 |
[(assq slot-name (class-direct-slots (car cpl))) |
| 439 |
(class-slot-accessor (car cpl) slot-name)] |
| 440 |
[else (loop (cdr cpl))])))] |
| 441 |
[(:each-subclass) (make-class-slot)] |
| 442 |
[(:virtual) |
| 443 |
(let ([getter (slot-definition-option slot :slot-ref #f)] |
| 444 |
[setter (slot-definition-option slot :slot-set! #f)] |
| 445 |
[bound? (slot-definition-option slot :slot-bound? #f)]) |
| 446 |
(unless (procedure? getter) |
| 447 |
(error "virtual slot requires at least :slot-ref:" slot)) |
| 448 |
(list getter setter bound?))] |
| 449 |
[(:builtin) |
| 450 |
(or (slot-definition-option slot :slot-accessor #f) |
| 451 |
(errorf "builtin slot ~s of class ~s doesn't have associated slot accessor" |
| 452 |
(car slot) class))] |
| 453 |
[else (error "unsupported slot allocation:" alloc)]))) |
| 454 |
|
| 455 |
(define (%make-class-slot cell) |
| 456 |
(list (^[o] cell) |
| 457 |
(^[o v] (set! cell v)) |
| 458 |
(^[o] (not (undefined? cell))))) |
| 459 |
|
| 460 |
;; METHOD COMPUTE-SLOT-ACCESSOR (class <class>) g-n-s |
| 461 |
;; this method doesn't have equivalent one in STklos. |
| 462 |
(define-method compute-slot-accessor :locked ((class <class>) slot gns) |
| 463 |
(if (is-a? gns <slot-accessor>) |
| 464 |
gns |
| 465 |
(apply make <slot-accessor> |
| 466 |
:class class :name (slot-definition-name slot) |
| 467 |
`(,@(cond |
| 468 |
[(integer? gns) (list :slot-number gns :initializable #t)] |
| 469 |
[(list? gns) |
| 470 |
(list :getter (car gns) |
| 471 |
:setter (list-ref gns 1 #f) |
| 472 |
:bound? (list-ref gns 2 #f) |
| 473 |
:initializable (list-ref gns 3 #f))] |
| 474 |
[else |
| 475 |
(errorf "bad getter-and-setter returned by compute-get-n-set for ~s ~s: ~s" |
| 476 |
class slot gns)]) |
| 477 |
,@(cdr slot))))) |
| 478 |
|
| 479 |
;; access class allocated slot. API compatible with Goops. |
| 480 |
(define (%class-slot-gns class slot-name acc-type) |
| 481 |
(cond [(class-slot-definition class slot-name) |
| 482 |
=> (^[slot] |
| 483 |
(if (memv (slot-definition-allocation slot) |
| 484 |
'(:class :each-subclass)) |
| 485 |
(slot-ref (class-slot-accessor class slot-name) acc-type) |
| 486 |
(errorf "attempt to access non-class allocated slot ~s of class ~s as a class slot." slot-name class)))] |
| 487 |
[else |
| 488 |
(errorf "attempt to access non-existent slot ~s of class ~s as a class slot." slot-name class)])) |
| 489 |
|
| 490 |
(define (class-slot-set! class slot-name val) |
| 491 |
(apply (%class-slot-gns class slot-name 'setter) (list #f val))) |
| 492 |
|
| 493 |
(define class-slot-ref |
| 494 |
(getter-with-setter |
| 495 |
(^[class slot-name] |
| 496 |
(let1 val (apply (%class-slot-gns class slot-name 'getter) '(#f)) |
| 497 |
(if (undefined? val) (slot-unbound class slot-name) val))) |
| 498 |
class-slot-set!)) |
| 499 |
|
| 500 |
(define (class-slot-bound? class slot-name) |
| 501 |
(apply (%class-slot-gns class slot-name 'bound?) '(#f))) |
| 502 |
|
| 503 |
;; Default class printer. This is only called if the class is an instance |
| 504 |
;; of a metaclass other than <class>. (If it's <class>, built-in print |
| 505 |
;; method is called; see class.c) |
| 506 |
;; Avoid using directly access the slots of obj, so that it won't trigger |
| 507 |
;; updating obj in case when obj's class has been redefined. |
| 508 |
;; We also need a cproc to check if OBJ is redefined; for, accessing |
| 509 |
;; 'redefined' slot needs to lock OBJ, which blocks when OBJ is being |
| 510 |
;; redefined. |
| 511 |
(define-method write-object ((obj <class>) out) |
| 512 |
(let1 name (slot-ref-using-class (current-class-of obj) obj 'name) |
| 513 |
(if (%class-redefined? obj) |
| 514 |
(format out "#<class ~a (redefined)>" name) |
| 515 |
(format out "#<class ~a>" name)))) |
| 516 |
|
| 517 |
(define-cproc %class-redefined? (obj) ::<boolean> |
| 518 |
(return (and (SCM_ISA obj SCM_CLASS_CLASS) |
| 519 |
(not (SCM_FALSEP (-> (cast ScmClass* obj) redefined)))))) |
| 520 |
|
| 521 |
;; convenient routine to push/pop a value to the slot. |
| 522 |
;; this can be optimized later. |
| 523 |
(define (slot-push! obj slot value) |
| 524 |
(slot-set! obj slot (cons value (slot-ref obj slot)))) |
| 525 |
(define (slot-pop! obj slot . default) |
| 526 |
(if (and (not (null? default)) |
| 527 |
(or (not (slot-bound? obj slot)) |
| 528 |
(not (pair? (slot-ref obj slot))))) |
| 529 |
(car default) |
| 530 |
(let1 r (slot-ref obj slot) |
| 531 |
(slot-set! obj slot (cdr r)) |
| 532 |
(car r)))) |
| 533 |
|
| 534 |
;; default unbound slot and missing slot handlers. |
| 535 |
;; we avoid printing object itself in the error message, for it might |
| 536 |
;; cause an infinite loop (via write-object method). |
| 537 |
(define-method slot-unbound ((class <class>) obj slot) |
| 538 |
(errorf "slot ~s of object of class ~a is unbound" slot class)) |
| 539 |
|
| 540 |
(define-method slot-missing ((class <class>) obj slot . value) |
| 541 |
(errorf "object of class ~s doesn't have such slot: ~s" class slot)) |
| 542 |
|
| 543 |
(define (slot-exists? obj slot) |
| 544 |
(slot-exists-using-class? (class-of obj) obj slot)) |
| 545 |
|
| 546 |
(define-method slot-exists-using-class? (class obj slot) |
| 547 |
(not (not (assq slot (class-slots class))))) |
| 548 |
|
| 549 |
(define-cproc %check-class-binding (name module::<module>) Scm_CheckClassBinding) |
| 550 |
(define-cproc class-of (obj) Scm_VMClassOf) |
| 551 |
|
| 552 |
;; current-class-of doesn't updates OBJ, and returns possibly the old class |
| 553 |
;; which has been redefined. Should only be used in class redefinition |
| 554 |
;; routines. |
| 555 |
(define-cproc current-class-of (obj) (return (SCM_OBJ (Scm_ClassOf obj)))) |
| 556 |
|
| 557 |
(define-cproc is-a? (obj klass::<class>) (inliner IS-A) Scm_VMIsA) |
| 558 |
(define-cproc subclass? (c1::<class> c2::<class>) ::<boolean> Scm_SubclassP) |
| 559 |
|
| 560 |
;; NB: subtype? and of-type? is in libtype.scm. |
| 561 |
|
| 562 |
(define-cproc slot-ref (obj slot) |
| 563 |
(inliner SLOT-REF) (setter slot-set!) |
| 564 |
(return (Scm_VMSlotRef obj slot FALSE))) |
| 565 |
|
| 566 |
(define-cproc slot-set! (obj slot value) (inliner SLOT-SET) Scm_VMSlotSet) |
| 567 |
|
| 568 |
(define-cproc slot-bound? (obj slot) Scm_VMSlotBoundP) |
| 569 |
|
| 570 |
(define-cproc slot-ref-using-accessor (obj accessor::<slot-accessor>) |
| 571 |
(return (Scm_VMSlotRefUsingAccessor obj accessor FALSE))) |
| 572 |
|
| 573 |
(define-cproc slot-bound-using-accessor? (obj accessor::<slot-accessor>) |
| 574 |
(return (Scm_VMSlotRefUsingAccessor obj accessor TRUE))) |
| 575 |
|
| 576 |
(define-cproc slot-set-using-accessor! (obj accessor::<slot-accessor> value) |
| 577 |
Scm_VMSlotSetUsingAccessor) |
| 578 |
|
| 579 |
(define-cproc slot-initialize-using-accessor! (obj accessor::<slot-accessor> |
| 580 |
initargs) |
| 581 |
Scm_VMSlotInitializeUsingAccessor) |
| 582 |
|
| 583 |
;; Internal API - undocumented |
| 584 |
(define-cproc instance-slot-ref (obj num::<fixnum> :optional fallback) |
| 585 |
(.if "GAUCHE_API_VERSION < 98" |
| 586 |
(return (Scm_InstanceSlotRef3 obj num fallback)) |
| 587 |
(return (Scm_InstanceSlotRef obj num fallback)))) |
| 588 |
;; Internal API - undocumented |
| 589 |
(define-cproc instance-slot-set! (obj num::<fixnum> value) ::<void> |
| 590 |
Scm_InstanceSlotSet) |
| 591 |
|
| 592 |
(define-cproc %finish-class-initialization! (klass::<class>) ::<void> |
| 593 |
(Scm_ClassMalleableSet klass FALSE)) |
| 594 |
|
| 595 |
;; |
| 596 |
;; Record related builtins |
| 597 |
;; |
| 598 |
(define-cproc %make-record (klass::<class> |
| 599 |
:optarray (inits numinits 10) |
| 600 |
:rest rinits) |
| 601 |
(let* ([obj (Scm__AllocateAndInitializeInstance klass inits numinits 0)]) |
| 602 |
(when (== numinits 10) |
| 603 |
(let* ([i::int 10]) |
| 604 |
(dolist [init rinits] (Scm_InstanceSlotSet obj (post++ i) init)))) |
| 605 |
(return obj))) |
| 606 |
|
| 607 |
(define-cproc %make-recordv (klass::<class> argv::<vector>) |
| 608 |
(let* ([v::ScmObj* (SCM_VECTOR_ELEMENTS argv)] |
| 609 |
[n::int (SCM_VECTOR_SIZE argv)]) |
| 610 |
(return (Scm__AllocateAndInitializeInstance klass v n 0)))) |
| 611 |
|
| 612 |
(define-cproc %record-ref (klass::<class> obj k::<fixnum>) |
| 613 |
(unless (SCM_ISA obj klass) |
| 614 |
(Scm_Error "record-ref: instance of %S expected, got %S" klass obj)) |
| 615 |
(.if "GAUCHE_API_VERSION < 98" |
| 616 |
(return (Scm_InstanceSlotRef3 obj k SCM_UNBOUND)) |
| 617 |
(return (Scm_InstanceSlotRef obj k SCM_UNBOUND)))) |
| 618 |
|
| 619 |
(define-cproc %record-set! (klass::<class> obj k::<fixnum> val) ::<void> |
| 620 |
(unless (SCM_ISA obj klass) |
| 621 |
(Scm_Error "record-set!: instance of %S expected, got %S" klass obj)) |
| 622 |
(Scm_InstanceSlotSet obj k val)) |
| 623 |
|
| 624 |
(define-cproc touch-instance! (obj) Scm_VMTouchInstance) |
| 625 |
|
| 626 |
;;---------------------------------------------------------------- |
| 627 |
;; Class Redefinition |
| 628 |
;; |
| 629 |
|
| 630 |
;; implemented in gauche/redefutil.scm |
| 631 |
;(autoload "gauche/redefutil" |
| 632 |
; redefine-class! class-redefinition |
| 633 |
; update-direct-subclass! change-object-class) |
| 634 |
|
| 635 |
;; change-class gf is defined in C, so we can't use autoload for it. |
| 636 |
(define-method change-class :locked ((obj <object>) (new-class <class>)) |
| 637 |
(change-object-class obj (current-class-of obj) new-class)) |
| 638 |
|
| 639 |
;; C bindings used by class redefinition routine. |
| 640 |
(define-cproc %start-class-redefinition! (k::<class>) ::<void> |
| 641 |
Scm_StartClassRedefinition) |
| 642 |
(define-cproc %commit-class-redefinition! (k::<class> newk) ::<void> |
| 643 |
Scm_CommitClassRedefinition) |
| 644 |
(define-cproc %replace-class-binding! (k::<class> newk::<class>) ::<void> |
| 645 |
Scm_ReplaceClassBinding) |
| 646 |
(define-cproc %add-direct-subclass! (super::<class> sub::<class>) ::<void> |
| 647 |
Scm_AddDirectSubclass) |
| 648 |
(define-cproc %delete-direct-subclass! (super::<class> sub::<class>) ::<void> |
| 649 |
Scm_DeleteDirectSubclass) |
| 650 |
(define-cproc %add-direct-method! (super::<class> m::<method>) ::<void> |
| 651 |
Scm_AddDirectMethod) |
| 652 |
(define-cproc %delete-direct-method! (super::<class> m::<method>) ::<void> |
| 653 |
Scm_DeleteDirectMethod) |
| 654 |
(define-cproc %transplant-instance! (src dst) ::<void> |
| 655 |
Scm_TransplantInstance) |
| 656 |
|
| 657 |
;;---------------------------------------------------------------- |
| 658 |
;; Method Application |
| 659 |
;; |
| 660 |
|
| 661 |
;; Like stklos or goops, pure generic is handled completely in C |
| 662 |
;; and the following protocol is skipped. |
| 663 |
;; |
| 664 |
;; apply-generic [GF] |
| 665 |
;; compute-applicable-methods [GF, method defined in C] |
| 666 |
;; sort-applicable-methods [GF] |
| 667 |
;; method-more-specific? [GF, method defined in C] |
| 668 |
;; apply-methods [GF] |
| 669 |
;; apply-method [GF] |
| 670 |
;; |
| 671 |
;; The protocol mimics STklos, but the underlying application mechanism |
| 672 |
;; differs a bit. |
| 673 |
|
| 674 |
(define-method apply-generic :locked ((gf <generic>) args) |
| 675 |
(let1 methods (compute-applicable-methods gf args) |
| 676 |
(apply-methods gf (sort-applicable-methods gf methods args) args))) |
| 677 |
|
| 678 |
(define-method sort-applicable-methods :locked ((gf <generic>) methods args) |
| 679 |
(let1 types (map class-of args) |
| 680 |
(sort methods (^[x y] (method-more-specific? x y types))))) |
| 681 |
|
| 682 |
(define-method apply-methods :locked ((gf <generic>) methods args) |
| 683 |
(apply-method gf methods %make-next-method args)) |
| 684 |
|
| 685 |
(define-method apply-method :locked ((gf <generic>) methods build-next args) |
| 686 |
(apply (build-next gf methods args) args)) |
| 687 |
|
| 688 |
;; internal, but useful to expose |
| 689 |
(define-cproc method-applicable-for-classes? (m::<method> :rest classes) |
| 690 |
::<boolean> |
| 691 |
(let* ([argc::int (Scm_Length classes)] |
| 692 |
[cp::ScmClass** (SCM_NEW_ARRAY (ScmClass*) argc)] |
| 693 |
[n::int 0]) |
| 694 |
(for-each (lambda (c) |
| 695 |
(unless (SCM_CLASSP c) |
| 696 |
(Scm_Error "class required, but got %S" c)) |
| 697 |
(set! (aref cp n) (SCM_CLASS c)) |
| 698 |
(post++ n)) |
| 699 |
classes) |
| 700 |
(return (Scm_MethodApplicableForClasses m cp argc)))) |
| 701 |
|
| 702 |
;; Manually trigger dispatch table construction |
| 703 |
;; (This is for development - eventually we set this triggered automatically) |
| 704 |
(define-cproc generic-build-dispatcher! (gf::<generic> axis::<fixnum>) |
| 705 |
Scm__GenericBuildDispatcher) |
| 706 |
|
| 707 |
(define-cproc generic-dispatcher-info (gf::<generic>) |
| 708 |
Scm__GenericDispatcherInfo) |
| 709 |
|
| 710 |
(define-cproc generic-invalidate-dispatcher! (gf::<generic>) ::<void> |
| 711 |
Scm__GenericInvalidateDispatcher) |
| 712 |
|
| 713 |
(define-cproc %generic-dispatcher-dump (gf::<generic> |
| 714 |
:optional (port::<port> |
| 715 |
(current-output-port))) |
| 716 |
::<void> Scm__GenericDispatcherDump) |
| 717 |
|
| 718 |
;;---------------------------------------------------------------- |
| 719 |
;; Introspection routines |
| 720 |
;; |
| 721 |
|
| 722 |
(define (class-name class) (slot-ref class 'name)) |
| 723 |
(define (class-precedence-list class) (slot-ref class 'cpl)) |
| 724 |
(define (class-direct-supers class) (slot-ref class 'direct-supers)) |
| 725 |
(define (class-direct-slots class) (slot-ref class 'direct-slots)) |
| 726 |
(define (class-direct-methods class) (slot-ref class 'direct-methods)) |
| 727 |
(define (class-direct-subclasses class) (slot-ref class 'direct-subclasses)) |
| 728 |
(define (class-slots class) (slot-ref class 'slots)) |
| 729 |
|
| 730 |
(define (slot-definition-name slot) (car slot)) |
| 731 |
(define (slot-definition-options slot) (cdr slot)) |
| 732 |
(define (slot-definition-option slot key . default) |
| 733 |
(apply get-keyword key (cdr slot) default)) |
| 734 |
(define (slot-definition-allocation slot) |
| 735 |
(get-keyword :allocation (cdr slot) :instance)) |
| 736 |
(define (slot-definition-getter slot) |
| 737 |
(get-keyword :getter (cdr slot) #f)) |
| 738 |
(define (slot-definition-setter slot) |
| 739 |
(get-keyword :setter (cdr slot) #f)) |
| 740 |
(define (slot-definition-accessor slot) |
| 741 |
(get-keyword :accessor (cdr slot) #f)) |
| 742 |
|
| 743 |
(define (class-slot-definition class slot-name) |
| 744 |
(assq slot-name (slot-ref class 'slots))) |
| 745 |
(define (class-slot-accessor class slot-name) |
| 746 |
(cond [(assq slot-name (slot-ref class 'accessors)) => cdr] |
| 747 |
[else #f])) |
| 748 |
|
| 749 |
;;---------------------------------------------------------------- |
| 750 |
;; Generic coercion |
| 751 |
;; (should this be in separate file, e.g. coerce.scm? |
| 752 |
;; autoload may have problem with autoloading generic fn.) |
| 753 |
|
| 754 |
(define-method x->string :locked ((obj <string>)) obj) |
| 755 |
(define-method x->string :locked ((obj <number>)) (number->string obj)) |
| 756 |
(define-method x->string :locked ((obj <symbol>)) (symbol->string obj)) |
| 757 |
(define-method x->string :locked ((obj <char>)) (string obj)) |
| 758 |
(define-method x->string :locked ((obj <top>)) (write-to-string obj display)) |
| 759 |
|
| 760 |
(define-method x->integer :locked ((obj <integer>)) obj) |
| 761 |
(define-method x->integer :locked ((obj <real>)) (round->exact obj)) |
| 762 |
(define-method x->integer :locked ((obj <number>)) (round->exact (real-part obj))) |
| 763 |
(define-method x->integer :locked ((obj <char>)) (char->integer obj)) |
| 764 |
(define-method x->integer :locked ((obj <top>)) (x->integer (x->number obj))) |
| 765 |
|
| 766 |
(define-method x->number :locked ((obj <number>)) obj) |
| 767 |
(define-method x->number :locked ((obj <string>)) (or (string->number obj) 0)) |
| 768 |
(define-method x->number :locked ((obj <char>)) (char->integer obj)) |
| 769 |
(define-method x->number :locked ((obj <top>)) 0) |
| 770 |
|
| 771 |
;;---------------------------------------------------------------- |
| 772 |
;; Generic accessor |
| 773 |
;; |
| 774 |
|
| 775 |
(define-method ref :locked ((obj <top>) (slot <symbol>)) |
| 776 |
(slot-ref obj slot)) |
| 777 |
(define-method ref :locked ((obj <top>) (slot <symbol>) fallback) |
| 778 |
(if (slot-bound? obj slot) |
| 779 |
(slot-ref obj slot) |
| 780 |
fallback)) |
| 781 |
(define-method (setter ref) :locked ((obj <top>) (slot <symbol>) value) |
| 782 |
(slot-set! obj slot value)) |
| 783 |
|
| 784 |
(define-method ref :locked ((obj <hash-table>) key) |
| 785 |
(hash-table-get obj key)) |
| 786 |
(define-method ref :locked ((obj <hash-table>) key fallback) |
| 787 |
(hash-table-get obj key fallback)) |
| 788 |
(define-method (setter ref) :locked ((obj <hash-table>) key value) |
| 789 |
(hash-table-put! obj key value)) |
| 790 |
|
| 791 |
(define-method ref :locked ((obj <tree-map>) key) |
| 792 |
(tree-map-get obj key)) |
| 793 |
(define-method ref :locked ((obj <tree-map>) key fallback) |
| 794 |
(tree-map-get obj key fallback)) |
| 795 |
(define-method (setter ref) :locked ((obj <tree-map>) key value) |
| 796 |
(tree-map-put! obj key value)) |
| 797 |
|
| 798 |
(define-method ref :locked ((obj <box>) field) |
| 799 |
(case field |
| 800 |
[(* 0) (unbox obj)] ;'* for srfi-123 |
| 801 |
[else |
| 802 |
(error "Bad field name for box reference. Exact integer or '*, but got:" |
| 803 |
field)])) |
| 804 |
(define-method (setter ref) :locked ((obj <box>) field value) |
| 805 |
(case field |
| 806 |
[(* 0) (set-box! obj value)] ;'* for srfi-123 |
| 807 |
[else |
| 808 |
(error "Bad field name for box reference. Exact integer or '*, but got:" |
| 809 |
field)])) |
| 810 |
|
| 811 |
(define-method ref :locked ((obj <mv-box>) field) |
| 812 |
(assume (exact-integer? field)) |
| 813 |
(unbox-value obj field)) |
| 814 |
(define-method (setter ref) :locked ((obj <mv-box>) field value) |
| 815 |
(assume (exact-integer? field)) |
| 816 |
(set-box-value! obj field value)) |
| 817 |
|
| 818 |
;; gauche.sequence has the generic version for <sequence>, but these |
| 819 |
;; shortcuts would be faster. |
| 820 |
;; NB: (ref <bitvector> k) has a choice of using /int or /bool. We chose |
| 821 |
;; int, somewhat arbitrary but more intuitive with correspondence of the |
| 822 |
;; literal bitvector notation. |
| 823 |
(define-method ref :locked ((obj <list>) (index <integer>)) |
| 824 |
(list-ref obj index)) |
| 825 |
(define-method ref :locked ((obj <vector>) (index <integer>)) |
| 826 |
(vector-ref obj index)) |
| 827 |
(define-method ref :locked ((obj <uvector>) (index <integer>)) |
| 828 |
(uvector-ref obj index)) |
| 829 |
(define-method ref :locked ((obj <bitvector>) (index <integer>)) |
| 830 |
(bitvector-ref/int obj index)) |
| 831 |
(define-method ref :locked ((obj <string>) (index <integer>)) |
| 832 |
(string-ref obj index)) |
| 833 |
(define-method (setter ref) :locked ((obj <list>) (index <integer>) val) |
| 834 |
(set-car! (list-tail obj index) val)) |
| 835 |
(define-method (setter ref) :locked ((obj <vector>) (index <integer>) val) |
| 836 |
(vector-set! obj index val)) |
| 837 |
(define-method (setter ref) :locked ((obj <uvector>) (index <integer>) val) |
| 838 |
(uvector-set! obj index val)) |
| 839 |
(define-method (setter ref) :locked ((obj <bitvector>) (index <integer>) val) |
| 840 |
(bitvector-set! obj index val)) |
| 841 |
(define-method (setter ref) :locked ((obj <string>) (index <integer>) val) |
| 842 |
(string-set! obj index val)) |
| 843 |
|
| 844 |
;; Universal accessor |
| 845 |
(define ~ |
| 846 |
(getter-with-setter |
| 847 |
(case-lambda |
| 848 |
[(obj selector) (ref obj selector)] |
| 849 |
[(obj selector . more) (apply ~ (ref obj selector) more)]) |
| 850 |
(case-lambda |
| 851 |
[(obj selector val) ((setter ref) obj selector val)] |
| 852 |
[(obj selector selector2 . rest) |
| 853 |
(apply (setter ~) (ref obj selector) selector2 rest)]))) |
| 854 |
|
| 855 |
(define ref* ~) ;for the backward compatibility |
| 856 |
|
| 857 |
;;---------------------------------------------------------------- |
| 858 |
;; Generalized application hooks |
| 859 |
;; (should this be in separate file, e.g. apply.scm?) |
| 860 |
|
| 861 |
(define-method object-apply :locked ((self <regexp>) (s <string>)) |
| 862 |
(rxmatch self s)) |
| 863 |
(define-method object-apply :locked ((self <regmatch>)) |
| 864 |
(rxmatch-substring self)) |
| 865 |
(define-method object-apply :locked ((self <regmatch>) (i <integer>)) |
| 866 |
(rxmatch-substring self i)) |
| 867 |
(define-method object-apply :locked ((self <regmatch>) (s <symbol>)) |
| 868 |
(case s |
| 869 |
[(before after) (object-apply self s 0)] |
| 870 |
[else (rxmatch-substring self s)])) |
| 871 |
(define-method object-apply :locked ((self <regmatch>) (s <symbol>) group) |
| 872 |
(case s |
| 873 |
[(before) (rxmatch-before self group)] |
| 874 |
[(after) (rxmatch-after self group)] |
| 875 |
[else |
| 876 |
(errorf "bad symbol argument to ~s: ~s: must be either 'before or 'after" |
| 877 |
self s)])) |
| 878 |
|
| 879 |
;; Char-set membership |
| 880 |
(define-method object-apply :locked ((self <char-set>) (c <char>)) |
| 881 |
(char-set-contains? self c)) |
| 882 |
|
| 883 |
;; A trick to let a condition type behave its own predicate |
| 884 |
(define-method object-apply :locked ((type <condition-meta>) obj) |
| 885 |
(condition-has-type? obj type)) |
| 886 |
|
| 887 |
;; Primitive parameters |
| 888 |
(define-method object-apply :locked ((p <primitive-parameter>) :optional val) |
| 889 |
(if (undefined? val) |
| 890 |
((with-module gauche.internal %primitive-parameter-ref) p) |
| 891 |
((with-module gauche.internal %primitive-parameter-set!) p val))) |
| 892 |
|
| 893 |
;; Regexp printer. It doesn't need speed, and it's easier to write in Scheme. |
| 894 |
;; This is defined here since we can only use it after define-method support. |
| 895 |
;; NB: case-folding regexp is expressed by (?i:...) in regexp->string. |
| 896 |
(define-method write-object ((obj <regexp>) out) |
| 897 |
(with-input-from-string (regexp->string obj) |
| 898 |
(^[] |
| 899 |
(display "#/" out) |
| 900 |
(let loop ([c (read-char)]) |
| 901 |
(unless (eof-object? c) |
| 902 |
(cond [(char=? #\/ c) (display #\\ out) (display c out)] |
| 903 |
[(memq (char-general-category c) '(Mn Mc Me Cc Cf Cs Co Cn)) |
| 904 |
(let1 code (char->ucs c) |
| 905 |
(if (< code #x10000) |
| 906 |
(format out "\\u~4,'0x" code) |
| 907 |
(format out "\\U~8,'0x" code)))] |
| 908 |
[else (display c out)]) |
| 909 |
(loop (read-char)))) |
| 910 |
(display "/" out)))) |
| 911 |
|
| 912 |
(define-method write-object ((obj <write-controls>) port) |
| 913 |
(format port "#<write-controls ~s>" |
| 914 |
`(:length ,(~ obj'length) |
| 915 |
:level ,(~ obj'level) |
| 916 |
:base ,(~ obj'base) |
| 917 |
:radix ,(~ obj'radix) |
| 918 |
:pretty ,(~ obj'pretty) |
| 919 |
:width ,(~ obj'width) |
| 920 |
:indent ,(~ obj'indent) |
| 921 |
:string-length ,(~ obj'string-length) |
| 922 |
:exact-decimal ,(~ obj'exact-decimal)))) |
| 923 |
|
| 924 |
;;---------------------------------------------------------------- |
| 925 |
;; Describe |
| 926 |
;; |
| 927 |
|
| 928 |
;; This feature used to be provided in gauche.interactive. However, |
| 929 |
;; it is better that libraries can customize describe on their data |
| 930 |
;; structures without loading gauche.interactive, so we provide the core |
| 931 |
;; methods here. Other specialized methods on built-in objects are |
| 932 |
;; still defined in gauche.interactive. |
| 933 |
|
| 934 |
;; Setting this dynamic state to true makes describe to show more details, |
| 935 |
;; which may be too much for casual use. (We don't use make-parameter, |
| 936 |
;; for parameter system is not initialized at this point.) |
| 937 |
(define describe-details |
| 938 |
(let1 flag #f |
| 939 |
(case-lambda |
| 940 |
[() flag] |
| 941 |
[(val) |
| 942 |
(assume-type val <boolean> |
| 943 |
"Value of describe-details must be a boolean, but got:" |
| 944 |
val) |
| 945 |
(begin0 flag (set! flag val))]))) |
| 946 |
|
| 947 |
(define (describe-common obj) |
| 948 |
(format #t "~s is an instance of class ~a\n" obj (class-name (class-of obj))) |
| 949 |
(values)) |
| 950 |
|
| 951 |
(define-method describe-slots (obj) |
| 952 |
(let* ([class (class-of obj)] |
| 953 |
[slots (class-slots class)]) |
| 954 |
(unless (null? slots) |
| 955 |
(format #t "slots:\n") |
| 956 |
(dolist [s (map slot-definition-name slots)] |
| 957 |
(when (or (describe-details) |
| 958 |
(not (#/^%/ (symbol->string s)))) |
| 959 |
(format #t " ~10s: ~a\n" s |
| 960 |
(if (slot-bound? obj s) |
| 961 |
(with-output-to-string |
| 962 |
(^[] (write-limited (slot-ref obj s) 60))) |
| 963 |
"#<unbound>")))))) |
| 964 |
(values)) |
| 965 |
|
| 966 |
(define-method describe (object) ; default |
| 967 |
(describe-common object) |
| 968 |
(describe-slots object) |
| 969 |
(values)) |
| 970 |
|
| 971 |
;;; |
| 972 |
;;; Make exported symbol visible from outside |
| 973 |
;;; |
| 974 |
|
| 975 |
(let ((xfer (with-module gauche.internal %transfer-bindings))) |
| 976 |
(xfer (current-module) |
| 977 |
(find-module 'gauche) |
| 978 |
'(compute-slots |
| 979 |
compute-get-n-set compute-slot-accessor |
| 980 |
class-slot-ref class-slot-set! class-slot-bound? |
| 981 |
slot-push! slot-pop! slot-unbound slot-missing |
| 982 |
slot-exists? slot-exists-using-class? |
| 983 |
change-class |
| 984 |
apply-generic sort-applicable-methods |
| 985 |
apply-methods apply-method |
| 986 |
class-of current-class-of is-a? subclass? |
| 987 |
slot-ref slot-set! |
| 988 |
slot-bound? slot-ref-using-accessor slot-bound-using-accessor? |
| 989 |
slot-set-using-accessor! slot-initialize-using-accessor! |
| 990 |
instance-slot-ref instance-slot-set! touch-instance! |
| 991 |
class-name class-precedence-list class-direct-supers |
| 992 |
class-direct-methods class-direct-subclasses |
| 993 |
class-direct-slots class-slots |
| 994 |
class-post-initialize |
| 995 |
slot-definition-name slot-definition-options |
| 996 |
slot-definition-option |
| 997 |
slot-definition-allocation slot-definition-getter |
| 998 |
slot-definition-setter slot-definition-accessor |
| 999 |
class-slot-definition class-slot-accessor |
| 1000 |
x->string x->integer x->number ref |setter of ref| |
| 1001 |
~ ref* |
| 1002 |
describe describe-common describe-slots describe-details |
| 1003 |
))) |