[#293] reset ns after load-file

This commit is contained in:
Michiel Borkent 2020-03-20 15:12:15 +01:00
parent 4a3df13381
commit 4db28b45e8
2 changed files with 14 additions and 6 deletions

View file

@ -29,7 +29,8 @@
[sci.impl.interpreter :refer [eval-string* eval-form]] [sci.impl.interpreter :refer [eval-string* eval-form]]
[sci.impl.opts :as sci-opts] [sci.impl.opts :as sci-opts]
[sci.impl.unrestrict :refer [*unrestricted*]] [sci.impl.unrestrict :refer [*unrestricted*]]
[sci.impl.vars :as vars]) [sci.impl.vars :as vars]
[sci.impl.types :as sci-types])
(:gen-class)) (:gen-class))
(binding [*unrestricted* true] (binding [*unrestricted* true]
@ -207,9 +208,12 @@ Everything after that is bound to *command-line-args*."))
(defn load-file* [sci-ctx f] (defn load-file* [sci-ctx f]
(let [f (io/file f) (let [f (io/file f)
s (slurp f)] s (slurp f)
prev-ns @vars/current-ns]
(sci/with-bindings {vars/current-file (.getCanonicalPath f)} (sci/with-bindings {vars/current-file (.getCanonicalPath f)}
(eval-string* sci-ctx s)))) (try
(eval-string* sci-ctx s)
(finally (sci-types/setVal vars/current-ns prev-ns))))))
(defn start-socket-repl! [address ctx] (defn start-socket-repl! [address ctx]
(socket-repl/start-repl! address ctx) (socket-repl/start-repl! address ctx)

View file

@ -118,9 +118,13 @@
(deftest load-file-test (deftest load-file-test
(let [tmp (java.io.File/createTempFile "script" ".clj")] (let [tmp (java.io.File/createTempFile "script" ".clj")]
(spit tmp "(defn foo [x y] (+ x y)) (defn bar [x y] (* x y))") (spit tmp "(ns foo) (defn foo [x y] (+ x y)) (defn bar [x y] (* x y))")
(is (= "120\n" (test-utils/bb nil (format "(load-file \"%s\") (bar (foo 10 30) 3)" (is (= "120\n" (test-utils/bb nil (format "(load-file \"%s\") (foo/bar (foo/foo 10 30) 3)"
(.getPath tmp))))))) (.getPath tmp)))))
(testing "namespace is restored after load file"
(is (= 'start-ns
(bb nil (format "(ns start-ns) (load-file \"%s\") (ns-name *ns*)"
(.getPath tmp))))))))
(deftest eval-test (deftest eval-test
(is (= "120\n" (test-utils/bb nil "(eval '(do (defn foo [x y] (+ x y)) (is (= "120\n" (test-utils/bb nil "(eval '(do (defn foo [x y] (+ x y))