vile / scheme / goops_protocol.scm
475 lines · 422 sloc · 20.82 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1;; GOOPS protocols. Native operations provide storage and dispatch primitives;
2;; Scheme methods keep callbacks and their continuations on the evaluator.
3
4(define-generic allocate-instance)
5(define-generic initialize)
6(define-generic make-instance)
7(define-generic compute-cpl)
8(define-generic compute-slots)
9(define-generic compute-get-n-set)
10(define-generic compute-getter-method)
11(define-generic compute-setter-method)
12(define-generic direct-slot-definition-class)
13(define-generic compute-direct-slot-definition)
14(define-generic effective-slot-definition-class)
15(define-generic compute-effective-slot-definition)
16(define-generic apply-generic)
17(define-generic compute-applicable-methods)
18(define-generic compute-effective-method)
19(define-generic compute-specialized-effective-method)
20(define-generic sort-applicable-methods)
21(define-generic method-more-specific?)
22(define-generic apply-method)
23(define-generic apply-methods)
24(define-generic shallow-clone)
25(define-generic deep-clone)
26(define-generic change-class)
27(define-generic update-instance-for-different-class)
28(define-generic class-redefinition)
29
30(define-method (allocate-instance (class <class>) args)
31 (%goops-allocate class args))
32
33(define-method (initialize (object <object>) args)
34 (%goops-validate-initargs args)
35 (let slots ((remaining (class-slots (class-of object))))
36 (if (pair? remaining)
37 (let* ((slot (car remaining))
38 (key (slot-definition-init-keyword slot))
39 (value (if key (get-keyword key args %goops-unbound) %goops-unbound)))
40 (if (%goops-unbound? value)
41 (let ((thunk (slot-definition-init-thunk slot)))
42 (if (and thunk
43 (not (memq (slot-definition-allocation slot)
44 '(#:class #:each-subclass))))
45 ((slot-ref slot 'slot-set!) object (thunk))))
46 ((slot-ref slot 'slot-set!) object value))
47 (slots (cdr remaining)))))
48 object)
49
50(define-method (initialize (slot <slot>) args)
51 (next-method)
52 (%goops-configure-slot! slot args))
53
54(define-method (make-instance (class <class>) . args)
55 (let ((object (allocate-instance class args)))
56 (initialize object args)
57 object))
58
59(define-method (compute-cpl (class <class>))
60 (compute-std-cpl class))
61
62(define-method (compute-slots (class <class>))
63 (%goops-std-slots class))
64
65(define-method (direct-slot-definition-class (class <class>) args)
66 (get-keyword #:class args <slot>))
67
68(define-method (compute-direct-slot-definition (class <class>) args)
69 (apply make (direct-slot-definition-class class args) args))
70
71(define-method (effective-slot-definition-class (class <class>) slot)
72 (class-of slot))
73
74(define-method (compute-effective-slot-definition (class <class>) slot)
75 (apply make (effective-slot-definition-class class slot)
76 (slot-definition-options slot)))
77
78(define (%goops-shared-initial-value slot)
79 (let ((thunk (slot-definition-init-thunk slot)))
80 (if thunk (thunk) (slot-definition-init-value slot))))
81
82(define-method (compute-get-n-set (class <class>) slot)
83 (if (%goops-static-slot? slot)
84 (%goops-slot-accessors slot)
85 (let ((allocation (slot-definition-allocation slot)))
86 (cond
87 ((eq? allocation #:instance)
88 (let ((index (slot-ref class 'nfields)))
89 (slot-set! class 'nfields (+ index 1))
90 index))
91 ((eq? allocation #:each-subclass)
92 (%goops-share-slot! slot (%goops-shared-initial-value slot)))
93 ((eq? allocation #:class)
94 (if (memq (slot-definition-name slot)
95 (map slot-definition-name (class-direct-slots class)))
96 (%goops-share-slot! slot (%goops-shared-initial-value slot))
97 (let find ((parents (cdr (class-precedence-list class))))
98 (if (null? parents)
99 (goops-error "inherited shared slot not found" (slot-definition-name slot))
100 (let ((inherited (class-slot-definition (car parents)
101 (slot-definition-name slot))))
102 (if inherited
103 (%goops-inherit-slot! slot inherited)
104 (find (cdr parents))))))))
105 ((eq? allocation #:virtual)
106 (let ((options (slot-definition-options slot)))
107 (list (get-keyword #:slot-ref options #f)
108 (get-keyword #:slot-set! options #f))))
109 (else (goops-error "unknown slot allocation" allocation))))))
110
111(define-method (compute-getter-method (class <class>) slot)
112 (%goops-getter-method class slot))
113
114(define-method (compute-setter-method (class <class>) slot)
115 (%goops-setter-method class slot))
116
117(define (%goops-install-slot-methods class slot)
118 (let ((getter (slot-definition-getter slot))
119 (set (slot-definition-setter slot))
120 (accessor (slot-definition-accessor slot)))
121 (if getter (add-method! getter (compute-getter-method class slot)))
122 (if set (add-method! set (compute-setter-method class slot)))
123 (if accessor
124 (begin
125 (add-method! (%goops-slot-accessor-target accessor)
126 (compute-getter-method class slot))
127 (add-method! (setter accessor) (compute-setter-method class slot))))))
128
129(define-method (initialize (class <class>) args)
130 (next-method)
131 (slot-set! class 'name (get-keyword #:name args '???))
132 (slot-set! class 'direct-supers (get-keyword #:dsupers args '()))
133 (slot-set! class 'nfields 0)
134 (slot-set! class 'cpl (compute-cpl class))
135 (slot-set! class 'direct-slots
136 (map (lambda (spec)
137 (if (is-a? spec <slot>) spec
138 (compute-direct-slot-definition
139 class (%goops-direct-initargs class spec))))
140 (get-keyword #:slots args '())))
141 (let ((slots (map (lambda (slot)
142 (let ((effective (compute-effective-slot-definition class slot)))
143 (%goops-install-slot! class effective
144 (compute-get-n-set class effective))
145 effective))
146 (compute-slots class))))
147 (slot-set! class 'slots slots)
148 (for-each (lambda (slot) (%goops-install-slot-methods class slot)) slots))
149 (%goops-finish-class! class)
150 class)
151
152(define-method (initialize (object <applicable-struct>) args)
153 (next-method)
154 (let ((procedure (get-keyword #:procedure args #f)))
155 (if procedure (slot-set! object 'procedure procedure)))
156 object)
157
158(define-method (initialize (generic <generic>) args)
159 (next-method)
160 (let ((name (get-keyword #:name args #f)))
161 (slot-set! generic 'name (if name name 'cache-miss)))
162 (slot-set! generic 'methods '())
163 (let ((previous (get-keyword #:default args #f)))
164 (if previous (%goops-add-default! generic previous)))
165 generic)
166
167(define-method (initialize (generic <generic-with-setter>) args)
168 (next-method)
169 (slot-set! generic 'setter (get-keyword #:setter args #f))
170 generic)
171
172(define-method (initialize (generic <extended-generic>) args)
173 (next-method)
174 (slot-set! generic 'extends (get-keyword #:extends args '()))
175 generic)
176
177(define-method (initialize (method <method>) args)
178 (next-method)
179 method)
180
181(define (make-class supers slots . args)
182 (%goops-validate-initargs args)
183 (let* ((normalized (if (let scan ((parents supers))
184 (and (pair? parents)
185 (or (memq <object> (class-precedence-list (car parents)))
186 (scan (cdr parents)))))
187 supers (append supers (list <object>))))
188 (metaclass (get-keyword #:metaclass args #f))
189 (chosen (if metaclass metaclass (ensure-metaclass normalized))))
190 (apply make chosen #:dsupers normalized #:slots slots
191 #:name (get-keyword #:name args %goops-unbound) args)))
192
193(define (ensure-metaclass supers)
194 (ensure-metaclass-with-supers (map class-of supers)))
195
196(define (ensure-metaclass-with-supers metas)
197 (let* ((normalized (%goops-normalize-metas metas))
198 (existing (%goops-common-meta normalized)))
199 (if existing existing
200 (%goops-cache-meta! normalized
201 (make <class> #:dsupers normalized #:slots '()
202 #:name %goops-unbound)))))
203
204(define class-name
205 (%goops-field-accessor 'class-name <class> 'name))
206(define class-direct-supers
207 (%goops-field-accessor 'class-direct-supers <class> 'direct-supers))
208(define class-precedence-list
209 (%goops-field-accessor 'class-precedence-list <class> 'cpl))
210(define class-direct-slots
211 (%goops-field-accessor 'class-direct-slots <class> 'direct-slots))
212(define class-slots
213 (%goops-field-accessor 'class-slots <class> 'slots))
214(define class-direct-subclasses
215 (%goops-field-accessor 'class-direct-subclasses <class> 'direct-subclasses))
216(define class-direct-methods
217 (%goops-field-accessor 'class-direct-methods <class> 'direct-methods))
218(define slot-definition-name
219 (%goops-field-accessor 'slot-definition-name <slot> 'name))
220(define slot-definition-allocation
221 (%goops-field-accessor 'slot-definition-allocation <slot> 'allocation))
222(define slot-definition-init-keyword
223 (%goops-field-accessor 'slot-definition-init-keyword <slot> 'init-keyword))
224(define slot-definition-init-form
225 (%goops-field-accessor 'slot-definition-init-form <slot> 'init-form))
226(define slot-definition-init-value
227 (%goops-field-accessor 'slot-definition-init-value <slot> 'init-value))
228(define slot-definition-init-thunk
229 (%goops-field-accessor 'slot-definition-init-thunk <slot> 'init-thunk))
230(define slot-definition-options
231 (%goops-field-accessor 'slot-definition-options <slot> 'options))
232(define slot-definition-getter
233 (%goops-field-accessor 'slot-definition-getter <slot> 'getter))
234(define slot-definition-setter
235 (%goops-field-accessor 'slot-definition-setter <slot> 'setter))
236(define slot-definition-accessor
237 (%goops-field-accessor 'slot-definition-accessor <slot> 'accessor))
238(define method-specializers
239 (%goops-field-accessor 'method-specializers <method> 'specializers))
240(define method-procedure
241 (%goops-field-accessor 'method-procedure <method> 'procedure))
242(define method-generic-function
243 (%goops-field-accessor 'method-generic-function <method> 'generic-function))
244(define method-formals
245 (%goops-field-accessor 'method-formals <method> 'formals))
246(define method-source
247 (%goops-field-accessor 'method-source <method> 'source))
248(define accessor-method-slot-definition
249 (%goops-field-accessor 'accessor-method-slot-definition <accessor-method> 'slot-definition))
250
251;; Reusable procedures keep these hot paths from creating let closures that
252;; capture the enclosing activation on every access.
253(define (%goops-class-slot-value class name value)
254 (if (%goops-unbound? value) (slot-unbound class name) value))
255(define (%goops-class-slot-read class name slot)
256 (if (and slot (memq (slot-definition-allocation slot) '(#:class #:each-subclass)))
257 (%goops-class-slot-value class name ((slot-ref slot 'slot-ref/raw) #f))
258 (slot-missing class name)))
259(define (class-slot-ref class name)
260 (%goops-class-slot-read class name (class-slot-definition class name)))
261
262(define (%goops-class-slot-write class name value slot)
263 (if (and slot (memq (slot-definition-allocation slot) '(#:class #:each-subclass)))
264 (begin ((slot-ref slot 'slot-set!) #f value) (if #f #f))
265 (slot-missing class name)))
266(define (class-slot-set! class name value)
267 (%goops-class-slot-write class name value (class-slot-definition class name)))
268
269(define-method (compute-applicable-methods (generic <generic>) args)
270 (%compute-applicable-methods generic args))
271(define-method (sort-applicable-methods (generic <generic>) methods args)
272 (%goops-sort-methods generic methods args))
273(define-method (method-more-specific? (a <method>) (b <method>) types)
274 (%goops-more-specific? a b types))
275
276;; Bind the next procedure while constructing the chain. A lexical next-method
277;; still captures the argument locations of each invocation of the bound body.
278(define (%goops-bind-effective-method method next)
279 (let ((bind (slot-ref method 'make-procedure)))
280 (if bind (bind next) (method-procedure method))))
281
282(define-method (compute-specialized-effective-method
283 (generic <generic>) (method <method>) types next)
284 (%goops-bind-effective-method method next))
285
286(define-method (compute-specialized-effective-method
287 (generic <generic>) (method <accessor-method>) types next)
288 (let ((arity (vector-length types)))
289 (if (or (= arity 1) (= arity 2))
290 (let* ((name (slot-definition-name (accessor-method-slot-definition method)))
291 (slot (class-slot-definition (vector-ref types 0) name)))
292 (slot-ref slot (if (= arity 1) 'slot-ref 'slot-set!)))
293 (next-method))))
294
295(define (%goops-build-effective-method generic methods types)
296 (let* ((method (car methods))
297 (remaining (cdr methods))
298 (ordinary? (eq? (class-of generic) <generic>))
299 (next (cond ((null? remaining)
300 (lambda args (no-next-method generic args)))
301 (ordinary?
302 (%goops-build-effective-method generic remaining types))
303 (else (compute-effective-method generic remaining types)))))
304 (if (and ordinary? (eq? (class-of method) <method>))
305 (%goops-bind-effective-method method next)
306 (compute-specialized-effective-method generic method types next))))
307
308(define-method (compute-effective-method (generic <generic>) methods types)
309 (%goops-build-effective-method generic methods types))
310
311(define-method (apply-method (generic <generic>) methods build-next args)
312 (let* ((method (car methods))
313 (next (build-next (cdr methods) args))
314 (factory (slot-ref method 'make-procedure)))
315 (if factory
316 (apply (factory next) args)
317 (apply (method-procedure method) args))))
318
319(define-method (apply-methods (generic <generic>) methods args)
320 (letrec ((build-next
321 (lambda (remaining previous)
322 (lambda arguments
323 (if (null? remaining)
324 (no-next-method generic arguments)
325 (apply-method generic remaining build-next arguments))))))
326 (if (null? methods) (no-next-method generic args)
327 (apply-method generic methods build-next args))))
328
329(define-method (apply-generic (generic <generic>) args)
330 (let ((methods (compute-applicable-methods generic args)))
331 (if methods
332 (apply-methods generic (sort-applicable-methods generic methods args) args)
333 (begin (no-applicable-method generic args)
334 (apply-generic generic args)))))
335
336(define (make-extended-generic parents . name)
337 (let* ((parents (if (list? parents) parents (list parents)))
338 (name (if (null? name) #f (car name)))
339 (setter-name (and name (string->symbol
340 (string-append "setter:" (symbol->string name)))))
341 (setters (let collect ((rest parents) (out '()))
342 (if (null? rest) (reverse out)
343 (collect (cdr rest)
344 (if (is-a? (car rest) <generic-with-setter>)
345 (cons (ensure-generic (setter (car rest)) setter-name) out)
346 out))))))
347 (if (null? setters)
348 (make <extended-generic> #:extends parents #:name name)
349 (make <extended-generic-with-setter> #:extends parents #:name name
350 #:setter (make <extended-generic> #:extends setters #:name setter-name)))))
351
352(define (%goops-unique values)
353 (let scan ((remaining values) (seen '()) (result '()))
354 (if (null? remaining) (reverse result)
355 (if (memq (car remaining) seen)
356 (scan (cdr remaining) seen result)
357 (scan (cdr remaining) (cons (car remaining) seen)
358 (cons (car remaining) result))))))
359
360(define (class-subclasses class)
361 (let scan ((pending (class-direct-subclasses class)) (seen (list class)) (result '()))
362 (if (null? pending) (reverse result)
363 (if (memq (car pending) seen)
364 (scan (cdr pending) seen result)
365 (scan (append (class-direct-subclasses (car pending)) (cdr pending))
366 (cons (car pending) seen) (cons (car pending) result))))))
367
368(define (class-methods class)
369 (%goops-unique
370 (apply append (map class-direct-methods (cons class (class-subclasses class))))))
371
372(define (%goops-allocate-clone object class)
373 (if (is-a? object <class>)
374 (make-class (class-direct-supers object) (class-direct-slots object)
375 #:name (class-name object) #:metaclass class)
376 (allocate-instance class '())))
377
378(define (%goops-clone-fields object copy class deep?)
379 (for-each
380 (lambda (slot)
381 (let ((name (slot-definition-name slot)))
382 (if (and (%goops-clone-slot? object slot) (slot-bound? object name))
383 (let* ((value (slot-ref object name))
384 (new (if (and deep? (instance? value)) (deep-clone value) value)))
385 (if (not (%goops-copy-native-slot! copy slot new object))
386 (slot-set! copy name new))))))
387 (class-slots class))
388 copy)
389
390(define-method (shallow-clone (object <object>))
391 (let ((class (class-of object)))
392 (%goops-clone-fields object (%goops-allocate-clone object class) class #f)))
393
394(define %goops-clone-context (make-parameter #f))
395(define-method (deep-clone (object <object>))
396 (let ((context (%goops-clone-context)))
397 (if context
398 (let ((existing (hashtable-ref context object #f)))
399 (if existing existing
400 (let* ((class (class-of object)) (copy (%goops-allocate-clone object class)))
401 (hashtable-set! context object copy)
402 (%goops-clone-fields object copy class #t))))
403 (parameterize ((%goops-clone-context (make-eq-hashtable)))
404 (deep-clone object)))))
405
406(define-method (update-instance-for-different-class (old <object>) (new <object>)) new)
407
408(define-method (change-class (object <object>) (target <class>))
409 ;; A getter or initializer can resume after another callback has committed
410 ;; the copy. Resolve the destination after the value is available.
411 (define (store-copied-slot! value plan slot source name)
412 (let ((destination (%goops-copy-target plan)))
413 (if (and destination (not (%goops-copy-native-slot! destination slot value source)))
414 (slot-set! destination name value))))
415 (let* ((old-class (class-of object))
416 (plan (%goops-prepare-copy object old-class target))
417 (copy (%goops-bind-copy plan (allocate-instance target '()))))
418 (dynamic-wind (%goops-copy-winder plan #t)
419 (lambda ()
420 (for-each
421 (lambda (slot)
422 (let* ((name (slot-definition-name slot))
423 (old-slot (class-slot-definition old-class name)))
424 (if (and old-slot
425 (memq (slot-definition-allocation old-slot) '(#:instance #:virtual))
426 (slot-bound? object name))
427 (store-copied-slot! (slot-ref object name) plan slot object name)
428 (if (not (memq (slot-definition-allocation slot) '(#:class #:each-subclass)))
429 (let ((thunk (slot-definition-init-thunk slot)))
430 (if thunk (store-copied-slot! (thunk) plan slot object name)))))))
431 (class-slots target))
432 (let ((previous (%goops-commit-instance! object copy plan)))
433 (if previous (update-instance-for-different-class previous object)))
434 object)
435 (%goops-copy-winder plan #f))))
436
437(define (%goops-migrate object target enter leave)
438 (dynamic-wind enter (lambda () (change-class object target)) leave))
439
440(define-method (class-redefinition (old <top>) (new <class>)) new)
441(define-method (class-redefinition (old <redefinable-class>) (new <redefinable-class>))
442 (%goops-redefinition-preflight old new)
443 (let ((children (class-direct-subclasses old)))
444 (%goops-redefine-class! old new)
445 (for-each
446 (lambda (child)
447 (class-redefinition child
448 (make-class (class-direct-supers child) (class-direct-slots child)
449 #:name (class-name child) #:metaclass (class-of child))))
450 children)
451 old))
452
453(define (%goops-reduce-chain primitive left right . rest)
454 (let loop ((value (primitive left right)) (tail rest))
455 (if (null? tail) value (loop (primitive value (car tail)) (cdr tail)))))
456
457(define (%goops-compare-chain primitive left right . rest)
458 (let loop ((a left) (b right) (tail rest))
459 (let ((result (primitive a b)))
460 (if (or (not result) (null? tail)) result
461 (loop b (car tail) (cdr tail))))))
462
463(define %goops-print-parts
464 (let ((at vector-ref) (size vector-length) (text? string?) (emit write-string))
465 (lambda (parts port)
466 (let loop ((i 0))
467 (if (< i (size parts))
468 (let ((part (at parts i)))
469 (if (text? part)
470 (begin (emit part port) (loop (+ i 1)))
471 (begin (part (at parts (+ i 1)) port) (loop (+ i 2))))))))))
472
473;; Publish only after every default method has been installed.
474(define make (%goops-enable-protocols! make-instance initialize allocate-instance make-class))
475(set! make-instance make)
476