Skip to content

Instantly share code, notes, and snippets.

@gwatt
Created January 14, 2020 23:09
Show Gist options
  • Save gwatt/d249273ebd56bbcbdb8e88e606e528d7 to your computer and use it in GitHub Desktop.
Save gwatt/d249273ebd56bbcbdb8e88e606e528d7 to your computer and use it in GitHub Desktop.
Some functions to work with libraries
#!chezscheme
(library (chez-library-extras)
(export reimport load-internals)
(import (chezscheme))
(define (loaded? libspec)
(let ([name (extract-library-name libspec)])
(exists (lambda (builtin)
(equal? name builtin))
(library-list))))
(define (extract-library-name libspec)
(filter atom? libspec))
(define (find-library-file libspec)
((library-search-handler) 'find-library-file libspec
(library-directories) (library-extensions)))
;
; Reimport a library and all its dependencies.
;
(define (reimport libspec)
(let ([name (extract-library-name libspec)])
(unless (#%$system-library? libspec)
(when (loaded? libspec)
(for-each reimport (library-requirements libspec))
(let-values ([(source object has-object?) (find-library-file libspec)])
(unless source
(errorf 'reimport-library "Unable to find library: ~a" libspec))
(cond
[has-object?
(maybe-compile-library source)
(load-library object)]
[else
(load-library source)])))
(eval `(import ,libspec)))))
;
; load a library and expose its internals.
;
(define (load-internals libspec)
(let-values ([(source . #:_) (find-library-file libspec)])
(with-input-from-file source
(rec loop
(lambda ()
(let ([obj (read)])
(unless (eof-object? obj)
(syntax-case obj (library export import)
[(library (name ...)
(export _ ...)
(import libs ...)
body ...)
(equal? (extract-library-name #'(name ...)) (extract-library-name libspec))
(begin
(eval (cons 'import #'(libs ...)))
(for-each eval #'(body ...)))]
[_ (void)])
(loop))))))))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment