fix `lazy-require' in phase levels other than 0
The `lazy-require' form expands to `define-runtime-module-path-index', whch doesn't work right at phase levels other than 0. Work around the problem by generating a submodule to hold the `define-runtime-module-path-index' form. This repair fixes `raco exe' on certain uses of `match', which in turn uses `lazy-require' at compile time. Also, use `register-external-module' to generate appropriate dependencies on lazily loaded modules.
This commit is contained in:
parent
06ffb74d66
commit
6b16679b39
|
@ -165,7 +165,7 @@
|
||||||
c))
|
c))
|
||||||
(raise
|
(raise
|
||||||
(make-exn:get-module-code
|
(make-exn:get-module-code
|
||||||
(format "get-module-code: cannot find submodule: ~e" submodule-path)
|
(format "get-module-code: cannot find submodule: ~e" sm-path)
|
||||||
(current-continuation-marks)
|
(current-continuation-marks)
|
||||||
#f)))
|
#f)))
|
||||||
(cdr sm-path))]))
|
(cdr sm-path))]))
|
||||||
|
|
12
collects/tests/racket/embed-me21.rkt
Normal file
12
collects/tests/racket/embed-me21.rkt
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#lang racket/base
|
||||||
|
(require racket/match)
|
||||||
|
|
||||||
|
;; check using `racket/match', particularly with a pattern
|
||||||
|
;; that eneds run-time support that may go through a
|
||||||
|
;; compile-time `lazy-require':
|
||||||
|
|
||||||
|
(match "x"
|
||||||
|
[(pregexp "x")
|
||||||
|
(with-output-to-file "stdout"
|
||||||
|
(lambda () (printf "This is 21.\n"))
|
||||||
|
#:exists 'append)])
|
|
@ -227,6 +227,7 @@
|
||||||
(one-mz-test "embed-me17.rkt" "This is 17.\n" #f)
|
(one-mz-test "embed-me17.rkt" "This is 17.\n" #f)
|
||||||
(one-mz-test "embed-me18.rkt" "This is 18.\n" #f)
|
(one-mz-test "embed-me18.rkt" "This is 18.\n" #f)
|
||||||
(one-mz-test "embed-me19.rkt" "This is 19.\n" #f)
|
(one-mz-test "embed-me19.rkt" "This is 19.\n" #f)
|
||||||
|
(one-mz-test "embed-me21.rkt" "This is 21.\n" #f)
|
||||||
|
|
||||||
;; Try unicode expr and cmdline:
|
;; Try unicode expr and cmdline:
|
||||||
(prepare dest "unicode")
|
(prepare dest "unicode")
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require (for-syntax racket/base
|
(require (for-syntax racket/base)
|
||||||
compiler/cm-accomplice)
|
compiler/cm-accomplice
|
||||||
racket/runtime-path
|
racket/runtime-path
|
||||||
racket/promise)
|
racket/promise)
|
||||||
(provide lazy-require
|
(provide lazy-require
|
||||||
|
@ -8,14 +8,21 @@
|
||||||
|
|
||||||
(define-syntax (lazy-require stx)
|
(define-syntax (lazy-require stx)
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
|
[(lazy-require #:requires-for-path (extra-req ...)
|
||||||
|
[modpath (thing ...)] ...)
|
||||||
|
#`(begin
|
||||||
|
(lazy-require1 modpath (extra-req ...) (thing ...) #,stx)
|
||||||
|
...)]
|
||||||
[(lazy-require [modpath (thing ...)] ...)
|
[(lazy-require [modpath (thing ...)] ...)
|
||||||
#`(begin (define-namespace-anchor anchor)
|
(syntax/loc stx
|
||||||
(lazy-require1 modpath (thing ...) anchor #,stx)
|
(lazy-require #:requires-for-path ()
|
||||||
...)]))
|
[modpath (thing ...)] ...))]))
|
||||||
|
|
||||||
|
(define-for-syntax counter 0)
|
||||||
|
|
||||||
(define-syntax (lazy-require1 stx)
|
(define-syntax (lazy-require1 stx)
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
[(lazy-require1 modpath (name ...) anchor orig-stx)
|
[(lazy-require1 modpath (extra-req ...) (name ...) orig-stx)
|
||||||
(with-syntax ([(defn ...)
|
(with-syntax ([(defn ...)
|
||||||
(for/list ([name (in-list (syntax->list #'(name ...)))])
|
(for/list ([name (in-list (syntax->list #'(name ...)))])
|
||||||
(unless (identifier? name)
|
(unless (identifier? name)
|
||||||
|
@ -26,17 +33,37 @@
|
||||||
(define-syntax name
|
(define-syntax name
|
||||||
(make-rename-transformer
|
(make-rename-transformer
|
||||||
(syntax-property (quote-syntax aux)
|
(syntax-property (quote-syntax aux)
|
||||||
'not-provide-all-defined #t))))))])
|
'not-provide-all-defined #t))))))]
|
||||||
|
[define-mpi-var
|
||||||
|
(let ([phase (sub1 (variable-reference->phase (#%variable-reference)))])
|
||||||
|
(if (zero? phase)
|
||||||
|
;; `define-runtime-module-path-index' works right at phase-level 0:
|
||||||
|
#'(define-runtime-module-path-index mpi-var (quasiquote modpath))
|
||||||
|
;; need a submodule:
|
||||||
|
(with-syntax ([lazy-require-path-n
|
||||||
|
(string->symbol
|
||||||
|
(format "lazy-require-path-~a-~a"
|
||||||
|
phase
|
||||||
|
counter))])
|
||||||
|
(set! counter (add1 counter))
|
||||||
|
#'(begin
|
||||||
|
(module lazy-require-path-n racket/base
|
||||||
|
(require racket/runtime-path
|
||||||
|
(for-syntax racket/base)
|
||||||
|
extra-req ...)
|
||||||
|
(provide mpi-var)
|
||||||
|
(define-runtime-module-path-index mpi-var (quasiquote modpath)))
|
||||||
|
(require 'lazy-require-path-n)))))])
|
||||||
;; implicit quasiquote, so can use normal module-path syntax
|
;; implicit quasiquote, so can use normal module-path syntax
|
||||||
;; or escape to compute a the module-path via expression
|
;; or escape to compute a the module-path via expression
|
||||||
#'(begin (define-runtime-module-path-index mpi-var (quasiquote modpath))
|
#'(begin
|
||||||
(define-values ()
|
define-mpi-var
|
||||||
(let-syntax ([_ (do-registration (#%variable-reference) (quasiquote modpath))])
|
(define (get-sym sym)
|
||||||
(values)))
|
(parameterize ((current-namespace (variable-reference->namespace (#%variable-reference))))
|
||||||
(define (get-sym sym)
|
(begin0
|
||||||
(parameterize ((current-namespace (namespace-anchor->namespace anchor)))
|
(dynamic-require mpi-var sym)
|
||||||
(dynamic-require mpi-var sym)))
|
(do-registration (#%variable-reference) (quasiquote modpath)))))
|
||||||
defn ...))]))
|
defn ...))]))
|
||||||
|
|
||||||
(define (make-lazy-function name get-sym)
|
(define (make-lazy-function name get-sym)
|
||||||
;; Use 'delay/sync' because 'delay' promise is not reentrant.
|
;; Use 'delay/sync' because 'delay' promise is not reentrant.
|
||||||
|
@ -48,15 +75,14 @@
|
||||||
(keyword-apply (force fun-p) kws kwargs args)))
|
(keyword-apply (force fun-p) kws kwargs args)))
|
||||||
name)))
|
name)))
|
||||||
|
|
||||||
(begin-for-syntax
|
(define (do-registration vr modpath)
|
||||||
(define (do-registration vr modpath)
|
(let ([path (resolved-module-path-name
|
||||||
(let ([path (resolved-module-path-name
|
(module-path-index-resolve
|
||||||
(module-path-index-resolve
|
(module-path-index-join
|
||||||
(module-path-index-join
|
modpath
|
||||||
modpath
|
(variable-reference->resolved-module-path vr))))])
|
||||||
(variable-reference->resolved-module-path vr))))])
|
(when (path? path)
|
||||||
(when (path? path)
|
(register-external-module path))))
|
||||||
(register-external-file path)))))
|
|
||||||
|
|
||||||
#|
|
#|
|
||||||
begin-on-demand notes/todo
|
begin-on-demand notes/todo
|
||||||
|
|
|
@ -3,7 +3,8 @@
|
||||||
"utils.rkt"
|
"utils.rkt"
|
||||||
(for-label racket/base
|
(for-label racket/base
|
||||||
racket/runtime-path
|
racket/runtime-path
|
||||||
unstable/lazy-require))
|
unstable/lazy-require
|
||||||
|
compiler/cm-accomplice))
|
||||||
|
|
||||||
@title[#:tag "lazy-require"]{Lazy Require}
|
@title[#:tag "lazy-require"]{Lazy Require}
|
||||||
@unstable[@author+email["Ryan Culpepper" "ryanc@racket-lang.org"]]
|
@unstable[@author+email["Ryan Culpepper" "ryanc@racket-lang.org"]]
|
||||||
|
@ -11,9 +12,12 @@
|
||||||
@defmodule[unstable/lazy-require]
|
@defmodule[unstable/lazy-require]
|
||||||
|
|
||||||
@defform/subs[#:literals (unquote)
|
@defform/subs[#:literals (unquote)
|
||||||
(lazy-require [mod (imported-fun-id ...)] ...)
|
(lazy-require maybe-requires
|
||||||
|
[mod (imported-fun-id ...)] ...)
|
||||||
([mod module-path
|
([mod module-path
|
||||||
(unquote module-path-expr)])
|
(unquote module-path-expr)]
|
||||||
|
[maybe-requires code:blank
|
||||||
|
(code:line #:requires-for-path (require-spec ...))])
|
||||||
#:contracts ([module-path-expr module-path?])]{
|
#:contracts ([module-path-expr module-path?])]{
|
||||||
|
|
||||||
Defines each @racket[imported-fun-id] as a function that, when called,
|
Defines each @racket[imported-fun-id] as a function that, when called,
|
||||||
|
@ -25,8 +29,23 @@ The module @racket[mod] can be specified as a @racket[_module-path]
|
||||||
(see @racket[require]) or as an @racket[unquote]-escaped expression
|
(see @racket[require]) or as an @racket[unquote]-escaped expression
|
||||||
that computes a module path. As with
|
that computes a module path. As with
|
||||||
@racket[define-runtime-module-path-index], a @racket[module-path-expr]
|
@racket[define-runtime-module-path-index], a @racket[module-path-expr]
|
||||||
is evaluated both in phase 0 and phase 1.
|
is evaluated both in phase level 0 and phase level 1 relative to its
|
||||||
}
|
enclosing phase level.
|
||||||
|
|
||||||
|
If the enclosing relative phase level is not 0, then
|
||||||
|
@racket[module-path-expr] is also placed in a submodule (with a use of
|
||||||
|
@racket[define-runtime-module-path-index] at phase level 0 within the
|
||||||
|
submodule); supply extra @racket[require-spec]s with
|
||||||
|
@racket[#:requires-for-path] to bind within the submodule for
|
||||||
|
@racket[module-path-expr]. Introduced submodules have the names
|
||||||
|
@racket[lazy-require-]@racket[_n]@racketidfont{-}@racket[_m], where
|
||||||
|
@racket[_n] is a phase-level number and @racket[_m] is a number.
|
||||||
|
|
||||||
|
When the use of a lazily-required function triggers module loading,
|
||||||
|
@racket[register-external-module] declares a potential compilation
|
||||||
|
dependency (in case the function is used in the process of compiling a
|
||||||
|
module).}
|
||||||
|
|
||||||
|
|
||||||
@defform[(begin-on-demand #:export (fun-id ...)
|
@defform[(begin-on-demand #:export (fun-id ...)
|
||||||
body ...+)]{
|
body ...+)]{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user