[plt-scheme] Attaching modules to namespaces in compiled code
On 1/26/09 1:32 AM, Matthew Flatt wrote:
> At Mon, 26 Jan 2009 00:51:41 +0100, Jakub Piotr Cłapa wrote:
>> I already asked this question here once but AFAICT some things changed
>> since then.
>>
>> I have the following code fragment:
>> (let ([ns (make-base-namespace)]
>> [sepack-mod (module-path-index-resolve (module-path-index-join
>> "sepack.ss" (syntax-source-module #'here)))])
>> (namespace-attach-module (current-namespace) sepack-mod ns)
>> (parameterize ([current-namespace ns]
>> [current-prompt-read prompt])
>> (namespace-require sepack-mod)
>> (read-eval-print-loop)))
>>
>> The last time I tried to do this I had to manually resolve the name
>> recursively (I basically copied and pasted the code Matthew suggested).
>> The module-path-index-resolve is certainly nicer but I am curious
>> whether it can be improved upon:
>> 1. Quite a few functions are needed to acquire one module path.
>> 2. As far as I understand I still need to require the module statically
>> somewhere (even if it is needed only in dynamically evaluated code) for
>> mzc to compile it in. Can this be fused with define-runtime-path somehow?
>
> We have better pieces for making this work, now, and I've put them
> together into a `define-runtime-module-path' form that is exported by
> `scheme/runtime-path'.
I found a -list version useful in my case:
(define-syntax (define-runtime-module-path-list stx)
(syntax-case stx ()
[(_ id (mod-path ...))
(begin
(unless (memq (syntax-local-context) '(top-level module
module-begin))
(raise-syntax-error #f
"allowed only in a module top-level or
top-level context"
stx))
(unless (identifier? #'id)
(raise-syntax-error #f
"expected an identifier to bind"
stx
#'id))
(unless (andmap module-path? (syntax->datum #'(mod-path ...)))
(raise-syntax-error #f
"expected a list of literal module paths"
stx
#'(mod-path ...)))
#`(begin
(require (only-in (for-label mod-path))) ...
(define id
(let ([vr (#%variable-reference)])
(list (combine-module-path vr 'mod-path) ...)))))]))
An example of usage is:
(define-runtime-module-path-list repl-modules
("crc16.ss" "util.ss" "sepack.ss" "intel-hex.ss" scheme))
(let ([old-ns (current-namespace)])
(parameterize ([current-namespace (make-empty-namespace)])
(for ([path (in-list repl-modules)])
(namespace-attach-module old-ns path (current-namespace))
(namespace-require path))
(read-eval-print-loop)))
--
regards,
Jakub Piotr Cłapa