Skip to content

Commit

Permalink
Set current namespace to the current module. Fixes #208.
Browse files Browse the repository at this point in the history
The pconvert-lib printer checks the namespace for (pre-)defined symbols.

If the current namespace is not configured, as what would happen during module
instantiation, user-written functions like my-add1 would be printed differently.
This also affects the error message from the check-expects.

    #lang htdp/isl+
    (define (my-add1 n) (+ n 1))
    my-add1
    (check-expect my-add1 2)

Output:

    Welcome to DrRacket.
    (lambda (a1) ...)

    Ran 1 test.
    0 tests passed.

    check-expect ... error ... :: first argument of equality cannot
    be a function, given (lambda (a1) ...)

    > my-add1
    my-add1
  • Loading branch information
shhyou committed Oct 25, 2024
1 parent d9f49ec commit d9486ca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
19 changes: 19 additions & 0 deletions htdp-lib/htdp/bsl/runtime.rkt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#lang racket/base
(provide configure configure/settings
current-module-namespace-variable-reference
options->sl-runtime-settings
(struct-out sl-runtime-settings)
sl-render-value/format)
Expand Down Expand Up @@ -47,6 +48,21 @@
(and (memq 'use-function-output-syntax options) #t)
(and (memq 'output-function-instead-of-lambda options) #t)))

(define current-module-namespace (make-parameter #f))

;; (parameter/c (or/c #f variable-reference?))
(define current-module-namespace-variable-reference
(make-derived-parameter current-module-namespace
;; guard
(λ (varref)
(if (variable-reference? varref)
(variable-reference->namespace varref)
#f))
;; wrap -- cannot retrieve value
(λ (ns)
(raise-user-error 'current-module-namespace-variable-reference
"parameter is write-only"))))

(define (configure options)
(configure/settings (options->sl-runtime-settings options)))

Expand Down Expand Up @@ -80,6 +96,9 @@
(or (signature-name val)
'<signature>)]
[(is-image? val) val]
[(current-module-namespace) => (λ (ns)
(parameterize ([current-namespace ns])
(ph val basic sub)))]
[else (ph val basic sub)]))))
(use-named/undefined-handler
(lambda (x)
Expand Down
4 changes: 4 additions & 0 deletions htdp-lib/lang/private/teach-module-begin.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@
#`(#%plain-module-begin
#,(stepper-syntax-property
#`(begin
(#%require (only htdp/bsl/runtime
current-module-namespace-variable-reference))
(current-module-namespace-variable-reference
(#%variable-reference))
(define repl? #f) ; whether we're in the REPL now
(let ((handle (uncaught-exception-handler)))
(uncaught-exception-handler
Expand Down

0 comments on commit d9486ca

Please sign in to comment.