forked from per-gron/blackhole
/
module.scm
1303 lines (1135 loc) · 41.1 KB
/
module.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
;;; -------------------------------------------------------------- ;;;
;;; ;;;
;;; The actual system ;;;
;;; ;;;
;;; -------------------------------------------------------------- ;;;
;; I have tried to organize this file so that there is no code that
;; depends on code that is below itself as much as possible/convenient.
;; This file only contains the neccesary functions. Utilities are
;; supposed to be in a separare module in the standard library.
;;;; ---------- Loaders ----------
(define-type loader
id: 786F06E1-BAF1-45A5-B31F-ED09AE93514F
;; Returns a module-info object for the given module
(calculate-info unprintable: equality-skip: read-only:)
;; Takes a relative module identifier symbol and optionally an
;; origin path, to which the path should be interpreted relative
;; to, and returns the object that should be in the path field of a
;; module object.
(path-absolutize unprintable: equality-skip: read-only:)
;; Returns what should be in the module-file field of the module
;; object.
(absolute-file unprintable: equality-skip: read-only:)
;; Takes a module and returns a string for the name of the module
(module-name unprintable: equality-skip: read-only:))
;;;; ---------- Module info ----------
(define-type module-info
id: 726DB40B-AB18-4396-A570-BB715B602DB9
(symbols read-only:)
(exports read-only:)
(imports read-only:)
(uses read-only:)
(options read-only:)
(cc-options read-only:)
(ld-options-prelude read-only:)
(ld-options read-only:)
(force-compile? read-only:)
(environment read-only:))
;;;; ---------- Module objects ----------
;; A module object consists of the loader and the module path
(define-type module
id: 48AC4955-EC9E-466F-B8EF-B7F0B9BBC63D
;; The loader object
(loader read-only:)
;; The absolute module path. It must be a hashable object.
(path read-only:)
;; The namespace, lazily initialized
(ns unprintable: init: #f equality-skip:)
;; An absolute file name that uniqely identifies this particular
;; module. This file needs to exist as long as the module exists on
;; this particular machine. Lazily initialized
(file module-file-lazy module-file-set! unprintable: init: #f equality-skip:))
(define (module-file mod)
(or (module-file-lazy mod)
(let ((val ((loader-absolute-file
(module-loader mod)) mod)))
(module-file-set! mod val)
val)))
;;;; ---------- Module resolvers ----------
;; A module resolver is a function that takes a list (like (/srfi/1)
;; or (spork /core)), the current loader and the current module path
;; (or #f) and returns a module identifier.
;; This is the 'here module resolver function
(define (current-module-resolver loader path . ids)
(map (lambda (id)
(make-module
loader
((loader-path-absolutize loader) id path)))
ids))
(define (package-module-resolver path)
(let ((path
(string-append
(path-strip-trailing-directory-separator path) "/")))
(lambda (_ __ . ids)
(map (lambda (id)
(make-module
local-loader
((loader-path-absolutize local-loader) id path)))
ids))))
;; This is a helper function for singleton loaders, for instance 'module
(define (make-singleton-module-resolver pkg)
(lambda (_ __)
(list (make-module pkg #f))))
(define *module-resolvers* '())
(define (module-resolver-add! name res)
(set! *module-resolvers*
(cons (cons name res)
*module-resolvers*)))
(define (resolve-module name #!optional cm)
(if (module? name)
(list name)
(call-with-values
(lambda ()
(cond
((symbol? name)
(values 'here (list name)))
((string? name)
(values 'lib (list name)))
((pair? name)
(values (car name) (cdr name)))
(else
(error "Invalid module identifier:" name))))
(lambda (resolver-id resolver-args)
(let ((resolver (let ((pair (assq resolver-id
*module-resolvers*)))
(and pair (cdr pair)))))
(if (not resolver)
(error "Module resolver not found:" resolver-id)
(apply resolver
`(,(if cm
(module-loader cm)
(current-loader))
,(if cm
(module-path cm)
(let ((current-mod (current-module)))
(and current-mod (module-path current-mod))))
,@resolver-args))))))))
(define (resolve-modules names #!optional cm)
(apply append
(map (lambda (x)
(resolve-module x cm))
names)))
(define (resolve-one-module name #!optional cm)
(let ((res (resolve-module name cm)))
(if (or (null? res)
(not (pair? res))
(not (null? (cdr res))))
(error "Module identifier must refer to one module only:" name)
(car res))))
;;;; ---------- Import resolvers ----------
;; Import resolvers are the implementation of (only: [mod] names),
;; add-prefix:, and similar features. They are functions that take the
;; current module, and their arguments ((only: [mod] names ...) would
;; give the arguments '([mod] names ...) to the only: resolver) and
;; return two values: the imported symbols (in the same format as
;; module-info-exports) and a list of the modules that this import
;; depends on.
(define *import-resolvers* '())
(define (resolve-import val #!optional cm)
(with-module-cache
(lambda ()
(cond
((and (pair? val)
(keyword? (car val)))
(let* ((resolver-id (car val))
(resolver (let ((pair (assq resolver-id
*import-resolvers*)))
(and pair (cdr pair)))))
(if (not resolver)
(error "Import resolver not found:" resolver-id)
(apply resolver
(cons (or cm (current-module))
(cdr val))))))
(else
(let ((mods (resolve-module val cm)))
(values (apply
append
(map (lambda (mod)
(module-info-exports
(module-info mod)))
mods))
mods)))))))
(define (resolve-imports vals #!optional cm)
(with-module-cache
(lambda ()
(let ((defs '())
(mods '()))
(for-each (lambda (val)
(call-with-values
(lambda ()
(resolve-import val cm))
(lambda (def mod)
(set! defs (cons def defs))
(set! mods (cons mod mods)))))
vals)
(values (flatten1 defs)
(flatten1 mods))))))
(define (only-resolver cm mod . names)
(call-with-values
(lambda () (resolve-import mod cm))
(lambda (defs modules)
(values
(map (lambda (name)
(or (assq name defs)
(error "only: Symbol not defined" name mod)))
names)
modules))))
(define (except-resolver cm mod . names)
(call-with-values
(lambda () (resolve-import mod cm))
(lambda (defs modules)
(let ((def-clone (map (lambda (x) x) defs)))
(for-each
(lambda (name)
(let ((found? #f))
(set! def-clone
(remove!
(lambda (x)
(and (eq? (car x) name)
(begin
(set! found? #t)
#t)))
def-clone))
(if (not found?)
(error "except: Symbol not defined" name mod))))
names)
(values def-clone modules)))))
(define (prefix-resolver cm mod prefix)
(let ((prefix-str
(if (symbol? prefix)
(symbol->string prefix)
(error "prefix: prefix must be a symbol" prefix))))
(call-with-values
(lambda () (resolve-import mod cm))
(lambda (defs modules)
(values
(map (lambda (def)
(cons (string->symbol
(string-append
prefix-str
(symbol->string (car def))))
(cdr def)))
defs)
modules)))))
(define (rename-resolver cm mod . renames)
(call-with-values
(lambda () (resolve-import mod cm))
(lambda (defs modules)
(let ((def-clone (map (lambda (x) x) defs)))
(for-each
(lambda (rename)
(if (not (and (list? rename)
(eq? 2 (length rename))
(symbol? (car rename))
(symbol? (cadr rename))))
(error "rename: Invalid rename form" rename))
(let ((pair (assq (car rename)
def-clone)))
(if pair
(if (assq (cadr rename) def-clone)
(error "rename: Symbol already in set"
(cadr rename))
(set-car! pair (cadr rename)))
(error "rename: Symbol not found" (car rename)))))
renames)
(values def-clone modules)))))
(set! *import-resolvers*
`((only: . ,only-resolver)
(except: . ,except-resolver)
(prefix: . ,prefix-resolver)
(rename: . ,rename-resolver)))
;;;; ---------- Export resolvers ----------
;; Export resolvers are the export equivalent of import
;; resolvers. They parse forms like (rename: (from to)), (re-export:
;; srfi/1). Similar to import resolvers, they take the current
;; environment and a list as arguments. Similar to import resolvers,
;; they return two values: the symbols to be exported, and modules
;; that need to loaded to use this export. (This is to make it
;; possible to implement re-export:)
(define *export-resolvers* '())
(define (export-helper env name as)
(if (not (and (symbol? name)
(symbol? as)))
(error "Invalid exports declaration" name as))
(cond
((environment-get env name) =>
(lambda (val)
(if (eq? 'mac (car val))
(list as
'mac
(cadr val) ;; Macro procedure
(caddr val)) ;; Macro environment
(list as
'def
(cadr val)))))
(else
(error "Name can't be exported because it isn't defined"
name))))
;; TODO This code is very similar to resolve-import, which is bad.
(define (resolve-export val env)
(with-module-cache
(lambda ()
(cond
((and (pair? val)
(keyword? (car val)))
(let* ((resolver-id (car val))
(resolver (let ((pair (assq resolver-id
*export-resolvers*)))
(and pair (cdr pair)))))
(if (not resolver)
(error "Export resolver not found:" resolver-id)
(apply resolver
(cons env (cdr val))))))
((symbol? val)
(values (list (export-helper env val val))
'()))
(else
(error "Invalid exports declaration" val))))))
;; TODO This code is pretty much a copy/pase of resolve-imports, which
;; is bad.
(define (resolve-exports vals env)
(with-module-cache
(lambda ()
(let ((defs '())
(mods '()))
(for-each (lambda (val)
(call-with-values
(lambda ()
(resolve-export val env))
(lambda (def mod)
(set! defs (cons def defs))
(set! mods (cons mod mods)))))
vals)
(values (flatten1 defs)
(flatten1 mods))))))
(define (rename-export-resolver env . renames)
(values (map (lambda (rename)
(if (not (and (list? rename)
(eq? 2 (length rename))))
(error "Invalid exports declaration"
rename))
(export-helper env
(car rename)
(cadr rename)))
renames)
'()))
(define (re-export-export-resolver env . import-decls)
(resolve-imports import-decls (environment-module env)))
(set! *export-resolvers*
`((rename: . ,rename-export-resolver)
(re-export: . ,re-export-export-resolver)))
;;;; ---------- Module info utilities ----------
(define empty-module-info
(make-module-info
'() '() '() '() '() "" "" "" #f builtin-environment))
;; The placement of this code is a little counterintuitive. It is
;; here, because this is the code that actually does the
;; calc-info.
(define-env load-environment
"module-loadenv#"
((import
(lambda (source env mac-env)
(let ((pkgs (extract-synclosure-crawler
(cdr (expr*:strip-locationinfo source)))))
(with-module-cache
(lambda ()
(call-with-values
(lambda () (resolve-imports
(extract-synclosure-crawler pkgs)))
(lambda (def mod)
(*module-loadenv-uses*
(append (*module-loadenv-uses*)
mod))
(*module-loadenv-imports*
(append (*module-loadenv-imports*)
def))
(module-add-defs-to-env def env))))))))
(module
(lambda (code env mac-env)
(error "Ill-placed module form" code)))
(define
(lambda (source env mac-env)
(let* ((code (expr*:value source))
(src (transform-to-lambda (cdr code)))
(sym (synclosure-extract-form (car src))))
(environment-add-def! env
sym
(gen-symbol
(module-namespace (current-module))
sym))
(*module-loadenv-symbols*
(cons (cons sym 'def) (*module-loadenv-symbols*)))
(void))))
(module#define-macro-register
(lambda (form env mac-env)
(let ((src (transform-to-lambda (cdr form))))
(*module-loadenv-symbols*
(cons (cons (car src) 'mac)
(*module-loadenv-symbols*))))
(void)))
(let
(lambda (code env mac-env)
(void)))
(letrec
(lambda (code env mac-env)
(void)))
(lambda
(lambda (code env mac-env)
(void)))
(export
(lambda (code env mac-env)
(*module-loadenv-exports*
(append (cdr (extract-synclosure-crawler code))
(or (*module-loadenv-exports*) '())))))
(compile-options
(nh-macro-transformer
(lambda (#!key options
cc-options
ld-options-prelude
ld-options
force-compile)
(if options
(*module-loadenv-options*
options))
(if cc-options
(*module-loadenv-cc-options*
cc-options))
(if ld-options-prelude
(*module-loadenv-ld-options-prelude*
ld-options-prelude))
(if ld-options
(*module-loadenv-ld-options*
ld-options))
(if force-compile
(*module-loadenv-force-compile*
force-compile))
(void))))
(c-declare
(lambda args
(*module-loadenv-force-compile* #t)
(void)))
(c-initialize
(lambda args
(*module-loadenv-force-compile* #t)
(void)))
(c-define-type
(lambda args
(*module-loadenv-force-compile* #t)
(void)))
(c-lambda
(lambda args
(*module-loadenv-force-compile* #t)
(void)))
(c-define
(lambda args
(*module-loadenv-force-compile* #t)
(void)))))
(define-macro (make-loadenv-vars . vars)
(let ((syms (map (lambda (var)
(string->symbol
(string-append
"*module-loadenv-"
(symbol->string (car var))
"*")))
vars))
(defaults (map cadr vars)))
`(begin
,@(map (lambda (sym)
`(define ,sym (make-parameter #f)))
syms)
(define (with-module-loadenv thunk)
(parameterize
,(map (lambda (sym default)
`(,sym ,default))
syms
defaults)
(thunk))))))
(make-loadenv-vars (symbols '())
(exports #f)
(imports '())
(uses '())
(options '())
(cc-options "")
(ld-options-prelude "")
(ld-options "")
(force-compile #f))
(define (loadenv-symbol-defs symbols env)
(let ((ns (module-namespace
(environment-module env))))
(map (lambda (pair)
(let ((name (car pair))
(type (cdr pair)))
(if (eq? 'def type)
(list name 'def (gen-symbol ns name))
(let ((mac (environment-get env name)))
(if (or (not mac)
(not (eq? 'mac (car mac))))
(error "Internal error in loadenv-symbol-defs:"
mac))
(list name ;; exported name
'mac
(cadr mac) ;; macro procedure
(caddr mac)))))) ;; macro environment
symbols)))
;; This function makes use of the module-loadenv dynamic environment.
(define (interpret-loadenv-exports env)
(let* ((exps (*module-loadenv-exports*)))
(if exps
(resolve-exports exps env)
(values
(loadenv-symbol-defs (*module-loadenv-symbols*)
env)
'()))))
(define calc-mode (make-parameter 'repl)) ;; one of 'repl, 'calc, 'load
(define (module-info-calculate module #!optional filename)
(with-module-loadenv
(lambda ()
(let ((env #f))
(if filename
(parameterize
((calc-mode 'calc)
(top-environment (make-top-environment
(resolve-one-module module))))
(expand-macro
(expr*:strip-locationinfo
(file-read-as-expr filename)))
(set! env (top-environment))))
(call-with-values
(lambda () (interpret-loadenv-exports env))
(lambda (exports export-uses)
;; TODO Add something to check for duplicate imports and
;; exports.
(make-module-info
(reverse (*module-loadenv-symbols*))
exports
(*module-loadenv-imports*)
(remove-duplicates ;; TODO This is an n^2 algorithm = sloow
(append export-uses
(*module-loadenv-uses*))
equal?)
(*module-loadenv-options*)
(*module-loadenv-cc-options*)
(*module-loadenv-ld-options-prelude*)
(*module-loadenv-ld-options*)
(*module-loadenv-force-compile*)
env)))))))
;;;; ---------- Module utility functions ----------
(define (current-module)
(environment-module (top-environment)))
(define (current-loader)
(let ((cm (current-module)))
(if cm
(module-loader (current-module))
local-loader)))
(define *calc-info-cache* (make-parameter #f))
(define (with-module-cache thunk)
(if (*calc-info-cache*)
(thunk)
(parameterize
((*calc-info-cache* (make-table)))
(suspend-ns-table-changes thunk))))
(define (make-module-util-function fn)
(lambda (mod)
(with-module-cache
(lambda ()
(fn (resolve-one-module mod))))))
(define module-info
(make-module-util-function
(lambda (mod)
(let ((mp (module-path mod)))
(or (table-ref (*calc-info-cache*) mp #f)
(let ((ret ((loader-calculate-info
(module-loader mod))
mod)))
(table-set! (*calc-info-cache*) mp ret)
ret))))))
(define module-needs-compile?
(make-module-util-function
(lambda (mod)
(let* ((path (module-file mod))
(of (last-object-file path)))
(if of
(not (file-newer? of path))
'not-compiled)))))
(define (module-compile! mod
#!key
continue-on-error
to-c)
(let ((mod (resolve-one-module mod)))
(with-module-cache
(lambda ()
(with-exception-catcher
(lambda (e)
(if continue-on-error
(begin
(display "Warning: Compilation failed: ")
(display-exception e)
#f)
(raise e)))
(lambda ()
(let ((info (module-info mod)))
(with-module-loadenv ;; For *module-loadenv-uses*
(lambda ()
(let ((result (compile-with-options
mod
(module-file mod)
to-c: to-c
options: (module-info-options info)
cc-options: (module-info-cc-options info)
ld-options-prelude: (module-info-ld-options-prelude
info)
ld-options: (module-info-ld-options info))))
(if (not result)
(error "Compilation failed"))))))))))))
(define module-clean!
(make-module-util-function
(lambda (mod)
(let ((fn (module-file mod)))
(and fn (clean-file (module-file mod)))))))
;;; This is the functionality for choosing unique namespaces
(define ns-file "~~/lib/modules/ns.dat")
(define ns-table #f)
(define ns-table-timestamp #f)
(define *ns-table-dont-read-file*
(make-parameter #f))
(define (ns-file-timestamp)
(time->seconds
(file-info-last-modification-time
(file-info ns-file))))
(define (ns-table-up-to-date?)
(and ns-table-timestamp
(>= ns-table-timestamp
(ns-file-timestamp))))
(define (read-ns-table)
(if (file-exists? ns-file)
(let* ((size (file-info-size
(file-info ns-file)))
(vec (make-u8vector size)))
(with-input-from-file
ns-file
(lambda ()
(read-subu8vector vec 0 size)))
;; This is here to fix an odd bug that put table-set! into an
;; infinite loop sometimes.
(list->table
(table->list
(u8vector->object vec))))
(make-table)))
(define (save-ns-table tbl)
(if (not tbl)
(error "Cannot save non-existent ns-table"))
(with-output-to-file
ns-file
(lambda ()
(let ((vect (object->u8vector tbl)))
(write-subu8vector vect
0
(u8vector-length vect)))))
(set! ns-table-timestamp
(ns-file-timestamp)))
(define (ns-table-cleanup tbl)
(let ((updated #f))
(table-for-each
(lambda (k v)
(if (not (file-exists? k))
(begin
(table-set! tbl k)
(set! updated #t))))
tbl)
(if updated
(save-ns-table tbl))))
(define (get-ns-table)
(cond
((or (*ns-table-dont-read-file*)
(and ns-table
(ns-table-up-to-date?)))
ns-table)
(else
(let ((tbl (read-ns-table)))
;; If this is the first time ns-table is loaded, do a cleanup
(if (not ns-table)
(ns-table-cleanup tbl))
(set! ns-table tbl)
tbl))))
(define (suspend-ns-table-changes thunk)
(if (*ns-table-dont-read-file*)
(thunk)
(begin
(get-ns-table)
(parameterize
((*ns-table-dont-read-file* #t))
(dynamic-wind
(lambda ()
#f)
thunk
(lambda ()
(if (eq? (*ns-table-dont-read-file*)
'changed)
(save-ns-table ns-table))))))))
(define (update-ns-table name path)
(table-set! ns-table path name)
(if (*ns-table-dont-read-file*)
(*ns-table-dont-read-file* 'changed)
(save-ns-table ns-table)))
(define (namespace-rename-reserved str)
(cond
((or (string->number str)
(string-contains str #\~)
(equal? str "module")
(equal? str "c"))
(string-append str "_"))
(else
str)))
(define (namespace-choose-unique mod)
(or (module-ns mod)
(let ((ns
(let ((abs-path (module-file mod))
(loader (module-loader mod)))
(get-ns-table)
(or (table-ref ns-table abs-path #f)
(let ((ns-no-reserved
(namespace-rename-reserved
((loader-module-name loader) mod))))
(let loop ((i 0))
(let* ((name
(if (eq? i 0)
ns-no-reserved
(string-append
ns-no-reserved
"_"
(number->string i))))
(found #f))
(table-for-each
(lambda (k v)
(if (equal? v name)
(set! found #t)))
ns-table)
(if found
(loop (+ 1 i))
(begin
(update-ns-table name abs-path)
name)))))))))
(module-ns-set! mod ns)
ns)))
(define module-namespace
(let ((fn
(make-module-util-function
(lambda (mod)
(string-append
(let ((loader (module-loader mod)))
(if (eq? loader module-module-loader)
"module"
(namespace-choose-unique mod)))
"#")))))
(lambda (mod)
;; This function might be called with #f as argument
(if mod (fn mod) "~#"))))
(define (module-add-defs-to-env defs #!optional (te (top-environment)))
(with-module-cache
(lambda ()
(for-each
(lambda (def)
(if (eq? 'def (cadr def))
;; Regular define
(environment-add-def!
te
(car def) ;; The name it's imported as
(caddr def)) ;; The name it's imported from
;; Macro
(environment-add-mac!
te
;; The name it's imported as
(car def)
;; The macro procedure
(caddr def)
;; The macro's environment
(cadddr def))))
defs))))
(define (module-load/deps modules)
(with-module-cache
(lambda ()
(let ((load-table (make-table)))
(for-each
(lambda (module)
(let rec ((module module))
(cond
((not (table-ref load-table
(module-path module)
#f))
(table-set! load-table
(module-path module)
#t)
(for-each rec
(module-info-uses
(module-info module)))
(let ((fn (module-file module)))
(if fn
(load-once fn module)))))))
modules)))))
(define (module-import modules #!optional (env (top-environment)))
(with-module-cache
(lambda ()
(call-with-values
(lambda () (resolve-imports modules))
(lambda (defs mods)
(if (or (eq? (calc-mode) 'repl)
(> (environment-phase env) 0))
(module-load/deps mods))
(module-add-defs-to-env defs env))))))
(define module-module
(let* ((repl-environment #f)
(fn (make-module-util-function
(lambda (mod)
(if (not (environment-module (top-environment)))
(set! repl-environment (top-environment)))
(top-environment (make-top-environment mod))
(module-load/deps (list mod))
(let ((info (module-info mod)))
(module-add-defs-to-env (module-info-imports info))
(module-add-defs-to-env
(loadenv-symbol-defs (module-info-symbols info)
(module-info-environment info))))
(void)))))
(lambda (mod)
;; This function might be called with #f as argument
(if mod
(fn mod)
(begin
(top-environment repl-environment)
(void))))))
;;;; ---------- Loader utility functions ----------
(define *load-once-registry* (make-table))
(define *load-and-init-registry* (make-table))
(define (load-and-init file mod)
(with-module-loadenv
(lambda ()
(let* ((fn
(let* ((ol (string-append file ".ol"))
(fn (if (file-exists? ol)
(with-input-from-file ol
read-line)
(last-object-file file))))
(and fn
(path-normalize fn
#f
file))))
(result
(and fn
(or (table-ref *load-and-init-registry*
fn
#f)
(##load-object-file fn #t)))))
(cond
((not fn)
(load file))
(else
(if (not
(and (vector? result)
(= 3 (vector-length result))))
(error "Failed to load file:" file result))
(table-set! *load-and-init-registry*
fn
result)
(let* ((exec-vect
(vector-ref result 0))
(exec-len
(vector-length exec-vect))
(ns
(let ((str (module-namespace mod)))
(substring str 0 (- (string-length str) 1))))
(procedure-or-vector
(if (= exec-len 1)
(vector-ref exec-vect 0)
(let loop ((i 0))
(cond
((>= i exec-len)
(error "Module initializer not found for:" ns))
((let ((name-sym (##procedure-name
(vector-ref exec-vect i))))
(and
name-sym
(let* ((name-str
(symbol->string name-sym))
(name
(substring name-str
1
(string-length name-str))))
(equal? name ns))))