Compile in a fresh module by default.

* module/system/base/compile.scm (make-compilation-module,
  language-default-environment): New procedures.
  (read-and-compile, compile): Have ENV default to
  `(language-default-environment from)'.
  (compile-and-load): Compile in `(current-module)'.

* module/system/repl/common.scm (repl-compile): Explicitly compile in
  the current module so that macro definitions are visible.

* libguile/load.c (kw_env): New variable.
  (do_try_autocompile): Call `compile-file' with `#:env (current-module)'.

* test-suite/tests/compiler.test ("psyntax")["compile uses a fresh module by
  default", "compile-time definitions are isolated"]: New tests.
  ["compile in current module"]: Specify `#:env (current-module)'.
  ["redefinition"]: Adjust.

* test-suite/tests/bytevectors.test (c&e): Explicitly compile in the
  current module so that its imports are visible.
This commit is contained in:
Ludovic Courtès 2009-08-14 19:30:14 +02:00
commit 87c595c757
5 changed files with 56 additions and 17 deletions

View file

@ -161,7 +161,8 @@
(define* (compile-and-load file #:key (from 'scheme) (to 'value) (opts '()))
(read-and-compile (open-input-file file)
#:from from #:to to #:opts opts))
#:from from #:to to #:opts opts
#:env (current-module)))
;;;
@ -190,6 +191,23 @@
(else
(lp (cdr in) (caar in))))))
(define (make-compilation-module)
"Return a fresh module to be used as the compilation environment."
;; Ideally we'd duplicate the whole module hierarchy so that `set!',
;; `fluid-set!', etc. don't have any effect in the current environment.
(let ((m (make-module)))
(beautify-user-module! m)
m))
(define (language-default-environment lang)
"Return the default compilation environment for source language LANG."
(if (or (eq? lang 'scheme)
(eq? lang (lookup-language 'scheme)))
(make-compilation-module)
#f))
(define* (read-and-compile port #:key
(env #f)
(from (current-language))
@ -199,7 +217,8 @@
(to (ensure-language to)))
(let ((joint (find-language-joint from to)))
(with-fluids ((*current-language* from))
(let lp ((exps '()) (env #f) (cenv env))
(let lp ((exps '()) (env #f)
(cenv (or env (language-default-environment from))))
(let ((x ((language-reader (current-language)) port)))
(cond
((eof-object? x)
@ -228,7 +247,8 @@
warnings))))
(receive (exp env cenv)
(compile-fold (compile-passes from to opts) x env opts)
(let ((env (or env (language-default-environment from))))
(compile-fold (compile-passes from to opts) x env opts))
exp))