-
Notifications
You must be signed in to change notification settings - Fork 112
/
gx-gambc0.scm
1754 lines (1520 loc) · 49.4 KB
/
gx-gambc0.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; -*- Gerbil -*-
;;; (C) vyzo at hackzen.org
;;; Gerbil stage0 -- Gambit-C host runtime
(##namespace (""))
;;(include "gx-gambc#.scm")
(declare
(block)
(standard-bindings)
(extended-bindings))
;;;
;;; Host Runtime
;;;
(include "gx-version.scm")
(define (gerbil-system-version-string)
(string-append "Gerbil " (gerbil-version-string) " on Gambit " (system-version-string)))
(define (gerbil-system)
'gerbil-gambit)
(define gerbil-greeting
(gerbil-system-version-string))
(set! gerbil-greeting gerbil-greeting) ; allow user mutation
(define (gerbil-runtime-smp?)
;; voodoo hack; this relies on the deq of the thread-group structure having
;; 3 fields in UP and 4 fields in SMP
;; maybe one day marc will provide a primitive/principled way to figure that out, but
;; until that day comes we really need to know in order to have the right cond-expand
;; branch when we include _gambit# or gx-gambc# (which includes _gambit#)
(not (%%vector-ref (thread-thread-group ##primordial-thread) 3)))
(cond-expand
(enable-smp
(unless (gerbil-runtime-smp?)
(display "*** WARNING -- SMP compiled Gerbil on UP Gambit runtime\n" ##stderr-port)))
(else
(when (gerbil-runtime-smp?)
(display "*** WARNING -- UP compiled Gerbil on SMP Gambit runtime\n" ##stderr-port))))
;;; Dynamic Module Loading
(define ¤t-module-libpath
(make-parameter #f))
(define ¤t-module-registry
(make-parameter #f))
(define (load-module modpath #!optional (reload? #f))
(cond
((and (not reload?) (hash-get (¤t-module-registry) modpath))
=> values)
((find-library-module modpath)
=> (lambda (path)
(let ((lpath (load path)))
(hash-put! (¤t-module-registry) modpath lpath)
lpath)))
(else
(error "Cannot load module; not found" modpath))))
(define (find-library-module modpath)
(define (find-compiled-file npath)
(let ((basepath (%%string-append npath ".o")))
(let lp ((current #f) (n 1))
(let ((next (%%string-append basepath (##number->string n))))
(if (##file-exists? next)
(lp next (%%fx+ n 1))
current)))))
(define (find-source-file npath)
(let ((spath (%%string-append npath ".scm")))
(and (##file-exists? spath) spath)))
(let lp ((rest (¤t-module-libpath)))
(core-match rest
((dir . rest)
(let ((npath (path-expand modpath (path-expand dir))))
(cond
((find-compiled-file npath) => path-normalize)
((find-source-file npath) => path-normalize)
(else (lp rest)))))
(else #f))))
(define (file-newer? file1 file2)
(define (modification-time file)
(time->seconds
(file-info-last-modification-time
(file-info file #t))))
(%%fl> (modification-time file1)
(modification-time file2)))
;; hook for loading compiled module phases
;; when this parameter is set, phase modules will be reloaded
(define _gx#reload-module
(make-parameter #f))
(define (_gx#load-module modpath)
(load-module modpath (_gx#reload-module)))
;; introspection repl: this is part of gx-gambc0 so that it is available
;; to all binaries.
;; Starts a nested repl with an exception handler that nests a repl
;; within, so that it can handle introspection exceptions regardless of
;; exception handler in the inspsected thread
(define (replx)
(define (write-reason exn)
(lambda (cont port)
(##display-exception-in-context exn cont port)
#f))
(with-exception-handler
(lambda (exn)
(continuation-capture
(lambda (cont)
(##repl-within cont (write-reason exn) exn))))
##repl))
;;; MOP
;;
;; Gerbil rtd:
;; {##struct-t id super fields name plist ctor slots methods}
;; {##class-t id super fields name plist ctor slots methods}
;;
;; Gambit structure rtd:
;; (define-type type
;; (id unprintable: equality-test:)
;; (name unprintable: equality-skip:)
;; (flags unprintable: equality-skip:)
;; (super unprintable: equality-skip:)
;; (fields unprintable: equality-skip:))
;;
;; Gerbil rtd on gambit
;; ##structure ##type-type
;; 1 ##type-id
;; 2 ##type-name
;; 3 ##type-flags
;; 4 ##type-super
;; 5 ##type-fields
;; 6 type-descriptor-mixin
;; 7 type-descriptor-fields
;; 8 type-descriptor-plist
;; 9 type-descriptor-ctor
;; 10 type-descriptor-slots
;; 11 type-descriptor-methods
;;
(define (type-descriptor? klass)
(and (%%type? klass)
(eq? (%%vector-length klass) 12)))
(define (struct-type? klass)
(and (type-descriptor? klass)
(not (type-descriptor-mixin klass))))
(define (class-type? klass)
(and (type-descriptor? klass)
(type-descriptor-mixin klass)
#t))
(define (make-type-descriptor type-id type-name type-super
rtd-mixin rtd-fields rtd-plist
rtd-ctor rtd-slots rtd-methods)
(define (put-props! ht key)
(define (put-plist! ht key plist)
(cond
((assgetq key plist)
=> (lambda (lst)
(for-each (lambda (id) (hash-put! ht id #t)) lst)))))
(put-plist! ht key rtd-plist)
(when rtd-mixin
(for-each (lambda (klass)
(when (type-descriptor-mixin klass) ; ignore structs
(let ((plist (type-descriptor-plist klass)))
(if (assgetq transparent: plist)
(put-plist! ht slots: plist)
(put-plist! ht key plist)))))
rtd-mixin)))
(let* ((transparent? (assgetq transparent: rtd-plist))
(field-names
(cond
((assq fields: rtd-plist) => cdr)
(else '())))
(field-names
(cond
((assq slots: rtd-plist)
=> (lambda (slots)
(append field-names (cdr slots))))
(else field-names)))
(_
(unless (fx= rtd-fields (length field-names))
(error "Bad field descriptor; length mismatch" type-id rtd-fields field-names)))
(canonical-fields
(if type-super
(list-tail field-names (type-descriptor-fields type-super))
field-names))
(printable
(if transparent?
#f ; all printable
(let ((ht (make-hash-table-eq)))
(put-props! ht print:)
ht)))
(equality
(if transparent?
#f ; all equality comparable
(let ((ht (make-hash-table-eq)))
(put-props! ht equal:)
ht)))
(field-info
(let recur ((rest canonical-fields))
(core-match rest
((id . rest)
(let ((flags
(if transparent? 0
(%%fxior (if (hash-get printable id) 0 1)
(if (hash-get equality id) 0 4)))))
(cons* id flags #f (recur rest))))
(else '()))))
(opaque?
(if (or transparent? (assq equal: rtd-plist))
(if type-super
(%%fx= (%%fxand (%%type-flags type-super) 1) 1)
#f)
#t)))
(%%structure ##type-type
type-id type-name
(+ 24 (if opaque? 1 0))
type-super
(list->vector field-info)
rtd-mixin rtd-fields rtd-plist rtd-ctor
rtd-slots rtd-methods)))
(define (make-struct-type-descriptor id name super fields plist ctor)
(make-type-descriptor id name super #f fields plist ctor #f #f))
(define (make-class-type-descriptor id name super mixin fields plist ctor slots)
(make-type-descriptor id name super mixin fields plist ctor slots #f))
(define (type-descriptor-mixin klass)
(%%vector-ref klass 6))
(define (type-descriptor-fields klass)
(%%vector-ref klass 7))
(define (type-descriptor-plist klass)
(%%vector-ref klass 8))
(define (type-descriptor-ctor klass)
(%%vector-ref klass 9))
(define (type-descriptor-slots klass)
(%%vector-ref klass 10))
(define (type-descriptor-methods klass)
(%%vector-ref klass 11))
(define (type-descriptor-methods-set! klass ht)
(%%vector-set! klass 11 ht))
(define (type-descriptor-sealed? klass)
(%%fxbit-set? 20 (%%type-flags klass)))
(define (type-descriptor-seal! klass)
(%%vector-set! klass 3 (%%fxior (%%fxarithmetic-shift 1 20) (%%type-flags klass))))
(define (make-struct-type id super fields name plist ctor #!optional (field-names #f))
(when (and super (not (struct-type? super)))
(error "Illegal super type; not a struct-type" super))
(when (and super (assgetq final: (type-descriptor-plist super)))
(error "Cannot extend final struct" super))
(let* ((super-fields
(if super (type-descriptor-fields super) 0))
(std-fields
(fx+ fields super-fields))
(std-field-names
(let* ((super-fields
(if super
(assgetq fields: (type-descriptor-plist super))
'()))
(field-names
(or field-names (make-list fields ':))))
(append super-fields field-names)))
(_
(unless (%%fx= std-fields (length std-field-names))
(error "Bad field specification; length mismatch" id std-fields std-field-names)))
(std-plist
(cons (cons fields: std-field-names) plist))
(ctor
(or ctor (and super (type-descriptor-ctor super)))))
(make-struct-type-descriptor id name super std-fields std-plist ctor)))
(define (make-struct-predicate klass)
(let ((tid (%%type-id klass)))
(if (assgetq final: (type-descriptor-plist klass))
(lambda (obj)
(%%structure-direct-instance-of? obj tid))
(lambda (obj)
(%%structure-instance-of? obj tid)))))
(define (make-struct-field-accessor klass field)
(let ((off (%%fx+ (struct-field-offset klass field) 1)))
(lambda (obj)
(##structure-ref obj off klass #f))))
(define (make-struct-field-mutator klass field)
(let ((off (%%fx+ (struct-field-offset klass field) 1)))
(lambda (obj val)
(##structure-set! obj val off klass #f))))
(define (make-struct-field-unchecked-accessor klass field)
(let ((off (%%fx+ (struct-field-offset klass field) 1)))
(lambda (obj)
(%%unchecked-structure-ref obj off klass #f))))
(define (make-struct-field-unchecked-mutator klass field)
(let ((off (%%fx+ (struct-field-offset klass field) 1)))
(lambda (obj val)
(%%unchecked-structure-set! obj val off klass #f))))
(define (struct-field-offset klass field)
(%%fx+ field
(cond
((%%type-super klass) => type-descriptor-fields)
(else 0))))
(define (struct-field-ref klass obj off)
(##structure-ref obj (%%fx+ off 1) klass #f))
(define (struct-field-set! klass obj off val)
(##structure-set! obj val (%%fx+ off 1) klass #f))
(define (struct-subtype? klass xklass)
(let ((klass-t (%%type-id klass)))
(let lp ((next xklass))
(cond
((not next)
#f)
((eq? klass-t (%%type-id next))
#t)
(else
(lp (%%type-super next)))))))
(define (make-class-type id super slots name plist ctor)
(define (class-slots klass)
(assgetq slots: (type-descriptor-plist klass)))
(define (make-slots off)
(let ((slot-table (make-hash-table-eq)))
(let lp ((rest super) (off off) (slot-list '()))
(core-match rest
((hd . rest)
(merge-slots slot-table (class-slots hd) off slot-list
(lambda (off slot-list)
(lp rest off slot-list))))
(else
(merge-slots slot-table slots off slot-list
(lambda (off slot-list)
(values off slot-table (reverse slot-list)))))))))
(define (merge-slots ht lst off r K)
(let lp ((rest lst) (off off) (r r))
(core-match rest
((slot . rest)
(if (hash-get ht slot)
(lp rest off r)
(begin
(hash-put! ht slot off)
(hash-put! ht (symbol->keyword slot) off)
(lp rest (%%fx+ off 1) (cons slot r)))))
(else
(K off r)))))
(define (find-super-ctor super)
(let lp ((rest super) (ctor #f))
(core-match rest
((hd . rest)
(cond
((type-descriptor-ctor hd)
=> (lambda (xctor)
(if (or (not ctor) (eq? ctor xctor))
(lp rest xctor)
(error "Conflicting implicit constructors" ctor xctor))))
(else (lp rest ctor))))
(else ctor))))
(define (find-super-struct super)
(define (base-struct super-struct klass)
(cond
(super-struct
(cond
((struct-subtype? super-struct klass)
(let lp ((klass klass))
(if (struct-type? klass)
klass
(lp (%%type-super klass)))))
((struct-subtype? klass super-struct)
super-struct)
(else
(error "Bad mixin: incompatible struct bases" klass super-struct))))
((struct-type? klass) klass)
((class-type? klass)
(let lp ((next (%%type-super klass)))
(cond
((not next)
#f)
((struct-type? next)
next)
((class-type? next)
(lp next))
(else #f))))
(else #f)))
(let lp ((rest super) (super-struct #f))
(core-match rest
((hd . rest)
(lp rest (base-struct super-struct hd)))
(else super-struct))))
(define (expand-struct-mixin super)
(let lp ((rest super) (mixin '()))
(core-match rest
((hd . rest)
(if (struct-type? hd)
(let lp2 ((next hd) (mixin mixin))
(cond
((not next)
(lp rest mixin))
((struct-type? next)
(lp2 (%%type-super next) (cons next mixin)))
(else
(lp rest mixin))))
(lp rest (cons hd mixin))))
(else
(reverse mixin)))))
(cond
((find (lambda (klass) (not (type-descriptor? klass))) super)
=> (lambda (klass)
(error "Illegal super class; not a type descriptor" klass)))
((find (lambda (klass)
(assgetq final: (type-descriptor-plist klass)))
super)
=> (lambda (klass)
(error "Cannot extend final class" klass))))
(let* ((std-super (find-super-struct super))
(mixin (if std-super (expand-struct-mixin super) super)))
(let-values (((std-fields std-slots std-slot-list)
(make-slots (if std-super (type-descriptor-fields std-super) 0))))
(let* ((std-mixin (class-linearize-mixins mixin))
(std-plist (if std-super
(let ((fields (assgetq fields: (type-descriptor-plist std-super))))
(cons (cons fields: fields) plist))
plist))
(std-plist (cons (cons slots: std-slot-list) std-plist))
(std-ctor (or ctor (find-super-ctor super))))
(make-class-type-descriptor id name std-super std-mixin std-fields std-plist std-ctor std-slots)))))
(define (class-linearize-mixins klass-lst)
(define (class->list klass)
(cons klass (or (type-descriptor-mixin klass) '())))
(core-match klass-lst
(() '())
((klass)
(class->list klass))
(else
(&linearize-mixins
(map class->list klass-lst)))))
(define (&linearize-mixins lst)
(define (K rest r)
(core-match rest
((hd . rest)
(linearize1 hd rest r))
(else
(reverse r))))
(define (linearize1 hd rest r)
(core-match hd
((hd-first . hd-rest)
(if (findq hd-first rest)
(linearize2 rest (list hd) r)
(K (cons hd-rest rest)
(putq hd-first r))))
(else
(K rest r))))
(define (linearize2 rest pre r)
(let lp ((rest rest) (pre pre))
(core-match rest
((hd . rest)
(core-match hd
((hd-first . hd-rest)
(if (findq hd-first rest)
(lp rest (cons hd pre))
(K (foldl cons (cons hd-rest rest) pre)
(putq hd-first r))))
(else
(lp rest pre)))))))
(define (putq hd lst)
(if (memq hd lst) lst
(cons hd lst)))
(define (findq hd rest)
(find (lambda (lst) (memq hd lst)) rest))
(K lst '()))
(define (make-class-predicate klass)
(if (assgetq final: (type-descriptor-plist klass))
(lambda (obj)
(direct-class-instance? klass obj))
(lambda (obj)
(class-instance? klass obj))))
(define (make-class-slot-accessor klass slot)
(lambda (obj)
(slot-ref obj slot)))
(define (make-class-slot-mutator klass slot)
(lambda (obj val)
(slot-set! obj slot val)))
(define (make-class-slot-unchecked-accessor klass slot)
(lambda (obj)
(unchecked-slot-ref obj slot)))
(define (make-class-slot-unchecked-mutator klass slot)
(lambda (obj val)
(unchecked-slot-set! obj slot val)))
(define (class-slot-offset klass slot)
(cond
((type-descriptor-slots klass)
=> (lambda (slots) (hash-get slots slot)))
(else #f)))
(define (class-slot-ref klass obj slot)
(if (class-instance? klass obj)
(let ((off (class-slot-offset (object-type obj) slot)))
(%%unchecked-structure-ref obj (%%fx+ off 1) klass #f))
(raise-type-error 'class-slot-ref klass obj)))
(define (class-slot-set! klass obj slot val)
(if (class-instance? klass obj)
(let ((off (class-slot-offset (object-type obj) slot)))
(%%unchecked-structure-set! obj val (%%fx+ off 1) klass #f))
(raise-type-error 'class-slot-set! klass obj)))
(define (class-subtype? klass xklass)
(let ((klass-t (%%type-id klass)))
(cond
((eq? klass-t (%%type-id xklass)))
((type-descriptor-mixin xklass)
=> (lambda (mixin)
(and (find (lambda (xklass) (eq? klass-t (%%type-id xklass)))
mixin)
#t)))
(else #f))))
(define object?
##structure?)
(define object-type
##structure-type)
(define (direct-instance? klass obj)
(%%structure-direct-instance-of? obj (%%type-id klass)))
(define (struct-instance? klass obj)
(%%structure-instance-of? obj (%%type-id klass)))
(define direct-struct-instance?
direct-instance?)
(define (class-instance? klass obj)
(and (object? obj)
(let ((klass-id (%%type-id klass))
(type (object-type obj)))
(and (type-descriptor? type)
(or (eq? (%%type-id type) klass-id)
(cond
((type-descriptor-mixin type)
=> (lambda (mixin)
(ormap (lambda (type) (eq? (%%type-id type) klass-id))
mixin)))
(else #f)))))))
(define direct-class-instance?
direct-instance?)
(define (make-object klass k)
(let ((obj (%%make-vector (%%fx+ k 1) #f)))
(%%vector-set! obj 0 klass)
(%%subtype-set! obj (macro-subtype-structure))
obj))
(define (make-struct-instance klass . args)
(let ((fields (type-descriptor-fields klass)))
(cond
((type-descriptor-ctor klass)
=> (lambda (kons-id)
(&constructor-init! klass kons-id (make-object klass fields) args)))
((%%fx= fields (length args))
(apply ##structure klass args))
(else
(error "Arguments don't match object size"
klass fields args)))))
(define (make-class-instance klass . args)
(let ((obj (make-object klass (type-descriptor-fields klass))))
(cond
((type-descriptor-ctor klass)
=> (lambda (kons-id)
(&constructor-init! klass kons-id obj args)))
(else
(&class-instance-init! klass obj args)))))
(define (struct-instance-init! obj . args)
(if (%%fx< (length args) (%%vector-length obj))
(&struct-instance-init! obj args)
(error "Too many arguments for struct" obj args)))
(define (&struct-instance-init! obj args)
(let lp ((k 1) (rest args))
(core-match rest
((hd . rest)
(%%vector-set! obj k hd)
(lp (%%fx+ k 1) rest))
(else obj))))
(define (class-instance-init! obj . args)
(&class-instance-init! (object-type obj) obj args))
(define (&class-instance-init! klass obj args)
(let lp ((rest args))
(core-match rest
((key val . rest)
(cond
((class-slot-offset klass key)
=> (lambda (off)
(%%vector-set! obj (%%fx+ off 1) val)
(lp rest)))
(else
(error "No slot for keyword initializer" klass key))))
(else
(if (null? rest) obj
(error "Unexpected class initializer arguments" rest))))))
(define (constructor-init! klass kons-id obj . args)
(&constructor-init! klass kons-id obj args))
(define (&constructor-init! klass kons-id obj args)
(cond
((&find-method klass kons-id)
=> (lambda (kons)
(apply kons obj args)
obj))
(else
(error "Missing constructor" klass kons-id))))
(define (struct-copy struct)
(unless (##structure? struct)
(error "Not a structure" 'struct-copy struct))
(##structure-copy struct))
(define (struct->list obj)
(if (object? obj)
(%%vector->list obj)
(error "Not an object" obj)))
(define (class->list obj)
(if (object? obj)
(let ((klass (object-type obj)))
(if (type-descriptor? klass)
(cond
((type-descriptor-slots klass)
=> (lambda (slots)
(cons klass
(hash-fold
(lambda (slot off r)
(if (keyword? slot)
(cons* slot (unchecked-field-ref obj off) r)
r))
'() slots))))
(else
(list klass)))
(error "Not a class type" obj klass)))
(error "Not an object" obj)))
(define (unchecked-field-ref obj off)
(%%vector-ref obj (%%fx+ off 1)))
(define (unchecked-field-set! obj off val)
(%%vector-set! obj (%%fx+ off 1) val))
(define (unchecked-slot-ref obj slot)
(unchecked-field-ref obj (class-slot-offset (object-type obj) slot)))
(define (unchecked-slot-set! obj slot val)
(unchecked-field-set! obj (class-slot-offset (object-type obj) slot) val))
(define-macro (&slot-e obj slot K E)
`(if (object? ,obj)
(let ((klass (object-type ,obj)))
(cond
((and (type-descriptor? klass) (class-slot-offset klass ,slot))
=> ,K)
(else (,E ,obj ,slot))))
(,E ,obj ,slot)))
(define (slot-ref obj slot #!optional (E &slot-error))
(&slot-e obj slot (lambda (off) (%%vector-ref obj (%%fx+ off 1))) E))
(define (slot-set! obj slot val #!optional (E &slot-error))
(&slot-e obj slot (lambda (off) (%%vector-set! obj (%%fx+ off 1) val)) E))
(define (&slot-error obj slot)
(error "Cannot find slot" obj slot))
(define (call-method obj id . args)
(cond
((method-ref obj id)
=> (lambda (method) (apply method obj args)))
(else
(error "Cannot find method" obj id))))
;; Methods
(define &builtin-type-methods
(make-table test: eq?))
(define (method-ref obj id)
(and (object? obj)
(find-method (object-type obj) id)))
(define (checked-method-ref obj id)
(or (method-ref obj id)
(error "Missing method" obj id)))
(define (bound-method-ref obj id)
(cond
((method-ref obj id)
=> (lambda (method)
(lambda args
(apply method obj args))))
(else #f)))
(define (checked-bound-method-ref obj id)
(let ((method (checked-method-ref obj id)))
(lambda args
(apply method obj args))))
(define (find-method klass id)
(cond
((type-descriptor? klass)
(&find-method klass id))
((%%type? klass)
(or (builtin-method-ref klass id)
(builtin-find-method (%%type-super klass) id)))
(else #f)))
(define (&find-method klass id)
(cond
((direct-method-ref klass id)
=> values)
((type-descriptor-sealed? klass)
#f)
((type-descriptor-mixin klass)
=> (lambda (mixin)
(mixin-find-method mixin id)))
(else
(struct-find-method (%%type-super klass) id))))
(define (struct-find-method klass id)
(cond
((type-descriptor? klass)
(or (direct-method-ref klass id)
(struct-find-method (%%type-super klass) id)))
((%%type? klass)
(or (builtin-method-ref klass id)
(builtin-find-method (%%type-super klass) id)))
(else #f)))
(define (class-find-method klass id)
(and (type-descriptor? klass)
(or (direct-method-ref klass id)
(mixin-method-ref klass id))))
(define (mixin-find-method mixin id)
(let lp ((rest mixin))
(core-match rest
((klass . rest)
(or (direct-method-ref klass id)
(lp rest)))
(else #f))))
(define (builtin-find-method klass id)
(and (%%type? klass)
(or (builtin-method-ref klass id)
(builtin-find-method (%%type-super klass) id))))
(define (direct-method-ref klass id)
(cond
((type-descriptor-methods klass)
=> (lambda (ht) (hash-get ht id)))
(else #f)))
(define (mixin-method-ref klass id)
(cond
((type-descriptor-mixin klass)
=> (lambda (mixin)
(mixin-find-method mixin id)))
(else #f)))
(define (builtin-method-ref klass id)
(cond
((hash-get &builtin-type-methods (%%type-id klass))
=> (lambda (mtab)
(hash-get mtab id)))
(else #f)))
(define (bind-method! klass id proc #!optional (rebind? #t))
(define (bind! ht)
(if (and (not rebind?) (hash-get ht id))
(error "Method already bound" klass id)
(hash-put! ht id proc)))
(unless (procedure? proc)
(error "Bad method; expected procedure" proc))
(cond
((type-descriptor? klass)
(let ((ht (type-descriptor-methods klass)))
(if ht
(bind! ht)
(let ((ht (make-hash-table-eq)))
(type-descriptor-methods-set! klass ht)
(bind! ht)))))
((%%type? klass)
(let ((ht
(cond
((hash-get &builtin-type-methods (%%type-id klass)) => values)
(else
(let ((ht (make-hash-table-eq)))
(hash-put! &builtin-type-methods (%%type-id klass) ht)
ht)))))
(bind! ht)))
(else
(error "Bad class; expected type-descriptor" klass))))
(define &method-specializers
(make-table test: eq?))
(define (bind-specializer! proc specializer)
(hash-put! &method-specializers proc specializer))
(define (seal-class! klass)
(define (collect-methods! mtab)
(define (merge! tab)
(hash-for-each (lambda (id proc) (hash-put! mtab id proc))
tab))
(define (collect-direct-methods! klass)
(cond
((type-descriptor-methods klass) => merge!)))
(cond
((type-descriptor-mixin klass)
=> (lambda (mixin)
(let recur ((rest mixin))
(core-match rest
((klass . rest)
(recur rest)
(cond
((type-descriptor? klass)
(collect-direct-methods! klass))
((and (%%type? klass) (hash-get &builtin-type-methods (%%type-id klass)))
=> merge!)))
(else (void))))))
(else
(let recur ((klass (%%type-super klass)))
(cond
((type-descriptor? klass)
(recur (%%type-super klass))
(collect-direct-methods! klass))
((%%type? klass)
(recur (%%type-super klass))
(cond
((hash-get &builtin-type-methods (%%type-id klass))
=> merge!)))))))
(collect-direct-methods! klass))
(when (type-descriptor? klass)
(unless (type-descriptor-sealed? klass)
(unless (assgetq final: (type-descriptor-plist klass))
(error "Cannot seal non-final class" klass))
(let ((vtab (make-hash-table-eq))
(mtab (make-hash-table-eq)))
(collect-methods! mtab)
(hash-for-each
(lambda (id proc)
(cond
((hash-get &method-specializers proc)
=> (lambda (specializer)
(let ((proc (specializer klass))
(gid (make-symbol (%%type-id klass) "::[" id "]")))
;; give the proecure a name and make it accesible to the debugger
(eval `(define ,gid (quote ,proc)))
(hash-put! vtab id proc))))
(else
(hash-put! vtab id proc))))
mtab)
(type-descriptor-methods-set! klass vtab)
(type-descriptor-seal! klass)))))
(define (next-method subklass obj id)
(let ((klass (object-type obj))
(type-id (%%type-id subklass)))
(cond
((type-descriptor? klass)
(cond
((type-descriptor-mixin klass)
=> (lambda (mixin)
(let lp ((rest (cons klass mixin)))
(core-match rest
((klass . rest)
(if (eq? type-id (%%type-id klass))
(mixin-find-method rest id)
(lp rest)))
(else #f)))))
(else
(let lp ((klass klass))
(cond
((eq? type-id (%%type-id klass))
(struct-find-method (%%type-super klass) id))
((%%type-super klass)
=> lp)
(else #f))))))
((%%type? klass)
(let lp ((klass klass))
(cond
((eq? type-id (%%type-id klass))
(builtin-find-method (%%type-super klass) id))
((%%type-super klass)
=> lp)
(else #f))))
(else #f))))
(define (call-next-method subklass obj id . args)
(cond
((next-method subklass obj id)
=> (lambda (methodf) (apply methodf obj args)))
(else
(error "Cannot find next method" obj id))))
;; custom writers
(define (write-style we)
(macro-writeenv-style we))
(define (write-object we obj)
(cond
((method-ref obj ':wr)
=> (lambda (method) (method obj we)))
(else
(##default-wr we obj))))
(##wr-set! write-object)
;;; etc
;; use gambit type for this
(define (raise-type-error where type obj)
(##raise-type-exception obj type where (list obj)))
(define absent-obj
(macro-absent-obj))
(define absent-value
'#(#!void))
(define (true . _)
#t)
(define (true? obj)
(eq? obj #t))
(define (false . _)
#f)
(define (void . _)
#!void)
(define (void? obj)
(eq? obj #!void))
(define (eof-object . _)
'#!eof)
(define (identity obj)
obj)
(define (dssl-object? obj)
(and (memq obj '(#!key #!rest #!optional)) #t))
(define (dssl-key-object? obj)
(eq? obj #!key))
(define (dssl-rest-object? obj)
(eq? obj #!rest))