Debian Codegraph @

scheme/psyntax.library-manager.ss

raw
1 ;;; Copyright (c) 2006, 2007 Abdulaziz Ghuloum and Kent Dybvig
2 ;;;
3 ;;; Permission is hereby granted, free of charge, to any person obtaining a
4 ;;; copy of this software and associated documentation files (the "Software"),
5 ;;; to deal in the Software without restriction, including without limitation
6 ;;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 ;;; and/or sell copies of the Software, and to permit persons to whom the
8 ;;; Software is furnished to do so, subject to the following conditions:
9 ;;;
10 ;;; The above copyright notice and this permission notice shall be included in
11 ;;; all copies or substantial portions of the Software.
12 ;;;
13 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 ;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 ;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 ;;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 ;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 ;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 ;;; DEALINGS IN THE SOFTWARE.
20
21 (library (psyntax library-manager)
22 (export imported-label->binding library-subst installed-libraries
23 visit-library library-name library-version library-exists?
24 find-library-by-name install-library library-spec invoke-library
25 current-library-expander uninstall-library
26 current-library-collection library-path library-extensions
27 serialize-all current-precompiled-library-loader)
28 (import (rnrs) (psyntax compat) (rnrs r5rs))
29
30 (define (make-collection)
31 (let ((set '()))
32 (define (set-cons x ls)
33 (cond
34 ((memq x ls) ls)
35 (else (cons x ls))))
36 (case-lambda
37 (() set)
38 ((x) (set! set (set-cons x set)))
39 ((x del?)
40 (if del?
41 (set! set (remq x set))
42 (set! set (set-cons x set)))))))
43
44 (define current-library-collection
45 ;;; this works now because make-collection is a lambda
46 ;;; binding and this turns into a complex binding as far
47 ;;; as letrec is concerned. It will be more ok once we do
48 ;;; letrec*.
49 (make-parameter (make-collection)
50 (lambda (x)
51 (unless (procedure? x)
52 (assertion-violation 'current-library-collection "not a procedure" x))
53 x)))
54
55 (define-record library
56 (id name version imp* vis* inv* subst env visit-state
57 invoke-state visit-code invoke-code guard-code guard-req*
58 visible? source-file-name)
59 (lambda (x p wr)
60 (unless (library? x)
61 (assertion-violation 'record-type-printer "not a library"))
62 (display
63 (format "#<library ~s>"
64 (if (null? (library-version x))
65 (library-name x)
66 (append (library-name x) (list (library-version x)))))
67 p)))
68
69 (define (find-dependencies ls)
70 (cond
71 ((null? ls) '())
72 (else (assertion-violation 'find-dependencies "cannot handle deps yet"))))
73
74 (define (find-library-by pred)
75 (let f ((ls ((current-library-collection))))
76 (cond
77 ((null? ls) #f)
78 ((pred (car ls)) (car ls))
79 (else (f (cdr ls))))))
80
81 (define library-path
82 (make-parameter
83 '(".")
84 (lambda (x)
85 (if (and (list? x) (for-all string? x))
86 (map (lambda (x) x) x)
87 (assertion-violation 'library-path "not a list of strings" x)))))
88
89 (define library-extensions
90 (make-parameter
91 '(".sls" ".ss" ".scm")
92 (lambda (x)
93 (if (and (list? x) (for-all string? x))
94 (map (lambda (x) x) x)
95 (assertion-violation 'library-extensions
96 "not a list of strings" x)))))
97
98 (define (library-name->file-name ls)
99 (let-values (((p extract) (open-string-output-port)))
100 (define (display-hex n)
101 (cond
102 ((<= 0 n 9) (display (number->string n) p))
103 (else (write-char
104 (integer->char
105 (+ (char->integer #\a)
106 (- n 10)))
107 p))))
108 (define (main*? x)
109 (and (>= (string-length x) 4)
110 (string=? (substring x 0 4) "main")
111 (for-all (lambda (x) (char=? x #\_))
112 (string->list (substring x 4 (string-length x))))))
113 (let f ((x (car ls)) (ls (cdr ls)) (fst #t))
114 (write-char #\/ p)
115 (let ([name (symbol->string x)])
116 (for-each
117 (lambda (n)
118 (let ([c (integer->char n)])
119 (cond
120 ((or (char<=? #\a c #\z)
121 (char<=? #\A c #\Z)
122 (char<=? #\0 c #\9)
123 (memv c '(#\. #\- #\+ #\_)))
124 (write-char c p))
125 (else
126 (write-char #\% p)
127 (display-hex (quotient n 16))
128 (display-hex (remainder n 16))))))
129 (bytevector->u8-list (string->utf8 name)))
130 (if (null? ls)
131 (when (and (not fst) (main*? name)) (write-char #\_ p))
132 (f (car ls) (cdr ls) #f))))
133 (extract)))
134
135 (define file-locator
136 (make-parameter
137 (lambda (x)
138 (let ((str (library-name->file-name x)))
139 (let f ((ls (library-path))
140 (exts (library-extensions))
141 (failed-list '()))
142 (cond
143 ((null? ls)
144 (file-locator-resolution-error x
145 (reverse failed-list)
146 (let ([ls (external-pending-libraries)])
147 (if (null? ls)
148 (error 'library-manager "BUG")
149 (cdr ls)))))
150 ((null? exts)
151 (f (cdr ls) (library-extensions) failed-list))
152 (else
153 (let ((name (string-append (car ls) str (car exts))))
154 (if (file-exists? name)
155 name
156 (f ls (cdr exts) (cons name failed-list)))))))))
157 (lambda (f)
158 (if (procedure? f)
159 f
160 (assertion-violation 'file-locator "not a procedure" f)))))
161
162 (define (serialize-all serialize compile)
163 (define (library-desc x)
164 (list (library-id x) (library-name x)))
165 (for-each
166 (lambda (x)
167 (when (library-source-file-name x)
168 (serialize
169 (library-source-file-name x)
170 (list (library-id x)
171 (library-name x)
172 (library-version x)
173 (map library-desc (library-imp* x))
174 (map library-desc (library-vis* x))
175 (map library-desc (library-inv* x))
176 (library-subst x)
177 (library-env x)
178 (compile (library-visit-code x))
179 (compile (library-invoke-code x))
180 (compile (library-guard-code x))
181 (map library-desc (library-guard-req* x))
182 (library-visible? x)))))
183 ((current-library-collection))))
184
185 (define current-precompiled-library-loader
186 (make-parameter (lambda (filename sk) #f)))
187
188 (define (try-load-from-file filename)
189 ((current-precompiled-library-loader)
190 filename
191 (case-lambda
192 ((id name ver imp* vis* inv* exp-subst exp-env
193 visit-proc invoke-proc guard-proc guard-req* visible?)
194 ;;; make sure all dependencies are met
195 ;;; if all is ok, install the library
196 ;;; otherwise, return #f so that the
197 ;;; library gets recompiled.
198 (let f ((deps (append imp* vis* inv* guard-req*)))
199 (cond
200 ((null? deps)
201 ;;; CHECK
202 (for-each
203 (lambda (x)
204 (let ([label (car x)] [dname (cadr x)])
205 (let ([lib (find-library-by-name dname)])
206 (invoke-library lib))))
207 guard-req*)
208 (cond
209 [(guard-proc) ;;; stale
210 (library-stale-warning name filename)
211 #f]
212 [else
213 (install-library id name ver imp* vis* inv*
214 exp-subst exp-env visit-proc invoke-proc
215 #f #f ''#f '() visible? #f)
216 #t]))
217 (else
218 (let ((d (car deps)))
219 (let ((label (car d)) (dname (cadr d)))
220 (let ((l (find-library-by-name dname)))
221 (cond
222 ((and (library? l) (eq? label (library-id l)))
223 (f (cdr deps)))
224 (else
225 (library-version-mismatch-warning name dname filename)
226 #f)))))))))
227 (others #f))))
228
229 (define library-loader
230 (make-parameter
231 (lambda (x)
232 (let ((file-name ((file-locator) x)))
233 (cond
234 ((not file-name)
235 (assertion-violation #f "cannot find library" x))
236 ((try-load-from-file file-name))
237 (else
238 ((current-library-expander)
239 (read-library-source-file file-name)
240 file-name
241 (lambda (name)
242 (unless (equal? name x)
243 (assertion-violation 'import
244 (let-values (((p e) (open-string-output-port)))
245 (display "expected to find library " p)
246 (write x p)
247 (display " in file " p)
248 (display file-name p)
249 (display ", found " p)
250 (write name p)
251 (display " instead" p)
252 (e))))))))))
253 (lambda (f)
254 (if (procedure? f)
255 f
256 (assertion-violation 'library-locator
257 "not a procedure" f)))))
258
259 (define current-library-expander
260 (make-parameter
261 (lambda (x)
262 (assertion-violation 'library-expander "not initialized"))
263 (lambda (f)
264 (if (procedure? f)
265 f
266 (assertion-violation 'library-expander
267 "not a procedure" f)))))
268
269 (define external-pending-libraries
270 (make-parameter '()))
271
272 (define (find-external-library name)
273 (when (member name (external-pending-libraries))
274 (assertion-violation #f
275 "circular attempt to import library was detected" name))
276 (parameterize ((external-pending-libraries
277 (cons name (external-pending-libraries))))
278 ((library-loader) name)
279 (or (find-library-by
280 (lambda (x) (equal? (library-name x) name)))
281 (assertion-violation #f
282 "handling external library did not yield the correct library"
283 name))))
284
285 (define (find-library-by-name name)
286 (or (find-library-by
287 (lambda (x) (equal? (library-name x) name)))
288 (find-external-library name)))
289
290 (define uninstall-library
291 (case-lambda
292 [(name err?)
293 (define who 'uninstall-library)
294 ;;; FIXME: check that no other import is in progress
295 ;;; FIXME: need to unintern labels and locations of
296 ;;; library bindings
297 (let ([lib
298 (find-library-by
299 (lambda (x) (equal? (library-name x) name)))])
300 (when (and err? (not lib))
301 (assertion-violation who "library not installed" name))
302 ((current-library-collection) lib #t)
303 (for-each
304 (lambda (x)
305 (let ((label (car x)) (binding (cdr x)))
306 (remove-location label)
307 (when (memq (car binding)
308 '(global global-macro global-macro! global-ctv))
309 (remove-location (cdr binding)))))
310 (library-env lib)))]
311 [(name) (uninstall-library name #t)]))
312
313 (define (library-exists? name)
314 (and (find-library-by
315 (lambda (x) (equal? (library-name x) name)))
316 #t))
317
318 (define (find-library-by-spec/die spec)
319 (let ((id (car spec)))
320 (or (find-library-by
321 (lambda (x) (eq? id (library-id x))))
322 (assertion-violation #f
323 "cannot find library with required spec" spec))))
324
325
326 (define (install-library-record lib)
327 (let ((exp-env (library-env lib)))
328 (for-each
329 (lambda (x)
330 (let ((label (car x)) (binding (cdr x)))
331 (let ((binding
332 (case (car binding)
333 ((global)
334 (cons 'global (cons lib (cdr binding))))
335 ((global-macro)
336 (cons 'global-macro (cons lib (cdr binding))))
337 ((global-macro!)
338 (cons 'global-macro! (cons lib (cdr binding))))
339 ((global-ctv)
340 (cons 'global-ctv (cons lib (cdr binding))))
341 (else binding))))
342 (set-label-binding! label binding))))
343 exp-env))
344 ((current-library-collection) lib))
345
346 (define install-library
347 (case-lambda
348 ((id name ver imp* vis* inv* exp-subst exp-env
349 visit-proc invoke-proc visit-code invoke-code
350 guard-code guard-req*
351 visible? source-file-name)
352 (let ((imp-lib* (map find-library-by-spec/die imp*))
353 (vis-lib* (map find-library-by-spec/die vis*))
354 (inv-lib* (map find-library-by-spec/die inv*))
355 (guard-lib* (map find-library-by-spec/die guard-req*)))
356 (unless (and (symbol? id) (list? name) (list? ver))
357 (assertion-violation 'install-library
358 "invalid spec with id/name/ver" id name ver))
359 (when (library-exists? name)
360 (assertion-violation 'install-library
361 "library is already installed" name))
362 (let ((lib (make-library id name ver imp-lib* vis-lib* inv-lib*
363 exp-subst exp-env visit-proc invoke-proc
364 visit-code invoke-code guard-code guard-lib*
365 visible? source-file-name)))
366 (install-library-record lib))))))
367
368 (define (imported-label->binding lab)
369 (label-binding lab))
370
371 (define (invoke-library lib)
372 (let ((invoke (library-invoke-state lib)))
373 (when (procedure? invoke)
374 (set-library-invoke-state! lib
375 (lambda () (assertion-violation 'invoke "circularity detected" lib)))
376 (for-each invoke-library (library-inv* lib))
377 (set-library-invoke-state! lib
378 (lambda ()
379 (assertion-violation 'invoke "first invoke did not return" lib)))
380 (invoke)
381 (set-library-invoke-state! lib #t))))
382
383
384 (define (visit-library lib)
385 (let ((visit (library-visit-state lib)))
386 (when (procedure? visit)
387 (set-library-visit-state! lib
388 (lambda () (assertion-violation 'visit "circularity detected" lib)))
389 (for-each invoke-library (library-vis* lib))
390 (set-library-visit-state! lib
391 (lambda ()
392 (assertion-violation 'invoke "first visit did not return" lib)))
393 (visit)
394 (set-library-visit-state! lib #t))))
395
396
397 (define (invoke-library-by-spec spec)
398 (invoke-library (find-library-by-spec/die spec)))
399
400 (define installed-libraries
401 (case-lambda
402 ((all?)
403 (let f ((ls ((current-library-collection))))
404 (cond
405 ((null? ls) '())
406 ((or all? (library-visible? (car ls)))
407 (cons (car ls) (f (cdr ls))))
408 (else (f (cdr ls))))))
409 (() (installed-libraries #f))))
410
411 (define library-spec
412 (lambda (x)
413 (unless (library? x)
414 (assertion-violation 'library-spec "not a library" x))
415 (list (library-id x) (library-name x) (library-version x))))
416 )
417