Add a noise flag indicator to `enter!'.

This commit is contained in:
Eli Barzilay 2010-12-12 03:36:39 -05:00
parent 8fb4e55d72
commit c65a2ba9ab
2 changed files with 55 additions and 47 deletions

View File

@ -5,22 +5,24 @@
(provide enter!) (provide enter!)
(define-syntax (enter! stx) (define-syntax (enter! stx)
(define (do-enter mod noise)
(unless (or (not (syntax-e mod)) (module-path? (syntax->datum mod)))
(raise-syntax-error #f "not a valid module path, and not #f" stx mod))
(unless (memq (syntax-e noise) '(#:verbose #:quiet #:verbose-reload))
(raise-syntax-error #f "not a valid verbosity keyword" stx noise))
#`(do-enter! '#,mod '#,noise))
(syntax-case stx () (syntax-case stx ()
[(enter! mod) [(enter! mod) (do-enter #'mod #'#:verbose-reload)]
(if (or (not (syntax-e #'mod)) [(enter! mod noise) (do-enter #'mod #'noise)]
(module-path? (syntax->datum #'mod)))
#'(do-enter! 'mod)
(raise-syntax-error
#f "not a valid module path, and not #f" stx #'mod))]
[_ (raise-syntax-error [_ (raise-syntax-error
#f "bad syntax; should be `(enter! <module-path-or-#f>)'" stx)])) #f "bad syntax; should be `(enter! <module-path-or-#f> [noise-flag])'"
stx)]))
(define orig-namespace (current-namespace)) (define orig-namespace (current-namespace))
(define (do-enter! mod) (define (do-enter! mod noise)
(if mod (if mod
(begin (begin (enter-require mod noise)
(enter-require mod)
(let ([ns (module->namespace mod)]) (let ([ns (module->namespace mod)])
(current-namespace ns) (current-namespace ns)
(namespace-require 'racket/enter))) (namespace-require 'racket/enter)))
@ -30,18 +32,22 @@
(define loaded (make-hash)) (define loaded (make-hash))
(define (enter-require mod) (define (enter-require mod noise)
;; Collect dependencies while loading: ;; Collect dependencies while loading:
(parameterize ([current-load/use-compiled (parameterize ([current-load/use-compiled
(enter-load/use-compiled (current-load/use-compiled) #f)]) (enter-load/use-compiled (current-load/use-compiled)
#f noise)])
(dynamic-require mod #f)) (dynamic-require mod #f))
;; Reload anything that's not up to date: ;; Reload anything that's not up to date:
(check-latest mod)) (check-latest mod noise))
(define (notify re? path) (define ((enter-load/use-compiled orig re? noise) path name)
(fprintf (current-error-port) " [~aloading ~a]\n" (if re? "re-" "") path)) (define notify
(if (case noise [(#:verbose-reload) re?] [(#:verbose) #t] [(#:quiet) #f])
(define ((enter-load/use-compiled orig re?) path name) (lambda (path)
(fprintf (current-error-port)
" [~aloading ~a]\n" (if re? "re-" "") path))
void))
(if name (if name
;; Module load: ;; Module load:
(let* ([code (get-module-code (let* ([code (get-module-code
@ -49,10 +55,8 @@
(lambda (e) (lambda (e)
(parameterize ([compile-enforce-module-constants #f]) (parameterize ([compile-enforce-module-constants #f])
(compile e))) (compile e)))
(lambda (ext loader?) (lambda (ext loader?) (load-extension ext) #f)
(load-extension ext) #:notify notify)]
#f)
#:notify (lambda (chosen) (notify re? chosen)))]
[dir (or (current-load-relative-directory) (current-directory))] [dir (or (current-load-relative-directory) (current-directory))]
[path (path->complete-path path dir)] [path (path->complete-path path dir)]
[path (normal-case-path (simplify-path path))]) [path (normal-case-path (simplify-path path))])
@ -66,9 +70,7 @@
;; Evaluate the module: ;; Evaluate the module:
(eval code)) (eval code))
;; Not a module: ;; Not a module:
(begin (begin (notify path) (orig path name))))
(notify re? path)
(orig path name))))
(define (get-timestamp path) (define (get-timestamp path)
(file-or-directory-modify-seconds path #f (file-or-directory-modify-seconds path #f
@ -78,7 +80,7 @@
(path-replace-suffix path #".ss") #f (lambda () -inf.0)) (path-replace-suffix path #".ss") #f (lambda () -inf.0))
-inf.0)))) -inf.0))))
(define (check-latest mod) (define (check-latest mod noise)
(define mpi (module-path-index-join mod #f)) (define mpi (module-path-index-join mod #f))
(define done (make-hash)) (define done (make-hash))
(let loop ([mpi mpi]) (let loop ([mpi mpi])
@ -95,6 +97,7 @@
(when (ts . > . (mod-timestamp mod)) (when (ts . > . (mod-timestamp mod))
(define orig (current-load/use-compiled)) (define orig (current-load/use-compiled))
(parameterize ([current-load/use-compiled (parameterize ([current-load/use-compiled
(enter-load/use-compiled orig #f)] (enter-load/use-compiled orig #f noise)]
[current-module-declare-name rpath]) [current-module-declare-name rpath])
((enter-load/use-compiled orig #t) npath (mod-name mod))))))))) ((enter-load/use-compiled orig #t noise)
npath (mod-name mod)))))))))

View File

@ -7,28 +7,33 @@
@note-init-lib[racket/enter] @note-init-lib[racket/enter]
@defform*[[(enter! module-path) @defform*[[(enter! module-path)
(enter! #f)]]{ (enter! #f)
(enter! module-path noise-flag)]]{
Intended for use in a @tech{REPL}, such as when @exec{mzscheme} is Intended for use in a @tech{REPL}, such as when @exec{racket} is
started in interactive mode. When a @scheme[module-path] is provided started in interactive mode. When a @racket[module-path] is provided
(in the same sense as for @scheme[require]), the corresponding module (in the same sense as for @racket[require]), the corresponding module
is loaded or invoked, and the current @tech{namespace} is changed to is loaded or invoked, and the current @tech{namespace} is changed to
the body of the module via @scheme[module->namespace]. When the body of the module via @racket[module->namespace]. When
@scheme[#f] is provided, then the current @tech{namespace} is restored @racket[#f] is provided, then the current @tech{namespace} is restored
to the original one. to the original one.
If invoking @scheme[module-path] requires loading any files, then If invoking @racket[module-path] requires loading any files, then
modification dates of the files are recorded. If the file is modified, modification dates of the files are recorded. If the file is modified,
then a later @scheme[enter!] re-loads the module from source; see also then a later @racket[enter!] re-loads the module from source; see also
@secref["module-redeclare"]. Similarly if a later @scheme[enter!] @secref["module-redeclare"]. Similarly if a later @racket[enter!]
transitively @scheme[require]s a modified module, then the required transitively @racket[require]s a modified module, then the required
module is re-loaded. Re-loading support works only for modules that module is re-loaded. Re-loading support works only for modules that
are first loaded (either directly or indirectly through transitive are first loaded (either directly or indirectly through transitive
@scheme[require]s) via @scheme[enter!]. @racket[require]s) via @racket[enter!].
After switching namespaces to the designated module, @scheme[enter!] After switching namespaces to the designated module, @racket[enter!]
automatically requires @scheme[racket/enter] into the namespace, so automatically requires @racket[racket/enter] into the namespace, so
that @scheme[enter!] can be used to switch namespaces again. that @racket[enter!] can be used to switch namespaces again.
When it loads or re-loads a module from a file, @scheme[enter!] prints When @racket[enter!] loads or re-loads a module from a file, it can
a message to @scheme[(current-error-port)].} print a message to @racket[(current-error-port)], as determined by the
optional @racket[noise-flag]. It can be @racket[#:verbose] to print a
message about such loads and re-loads, @racket[#:verbose-reload] to
print a message only for re-loaded modules, and it can be
@racket[#:quiet] for no printouts.}