Skip to content

Commit

Permalink
Implement in-ns as a function. (#824)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Cherry <[email protected]>
  • Loading branch information
2 people authored and borkdude committed Oct 28, 2022
1 parent ee98174 commit f8662b9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
11 changes: 1 addition & 10 deletions src/sci/impl/analyzer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1355,14 +1355,6 @@
(let [snd (second expr)]
(->constant snd)))

(defn analyze-in-ns [ctx expr]
(let [ns-expr (analyze ctx (second expr))]
(sci.impl.types/->Node
(let [ns-sym (types/eval ns-expr ctx bindings)]
(set-namespace! ctx ns-sym nil)
nil)
nil)))

(defn analyze-import [_ctx expr]
(let [args (rest expr)
stack (assoc (meta expr)
Expand Down Expand Up @@ -1466,8 +1458,7 @@
import (analyze-import ctx expr)
or (return-or ctx expr (rest expr))
and (return-and ctx expr (rest expr))
recur (return-recur ctx expr (analyze-children (without-recur-target ctx) (rest expr)))
in-ns (analyze-in-ns ctx expr))
recur (return-recur ctx expr (analyze-children (without-recur-target ctx) (rest expr))))
:else
(try
(if (macro? f)
Expand Down
7 changes: 7 additions & 0 deletions src/sci/impl/namespaces.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,12 @@
(assert (symbol? ns-sym))
(sci.impl.utils/namespace-object (:env ctx) ns-sym false nil))

(defn sci-in-ns [ctx ns-sym]
(assert (symbol? ns-sym))
(when-not (sci-find-ns ctx ns-sym)
(sci-create-ns ctx ns-sym))
(sci.impl.utils/set-namespace! ctx ns-sym {}))

(defn sci-the-ns [ctx x]
(if (instance? #?(:clj sci.lang.Namespace
:cljs sci.lang/Namespace) x) x
Expand Down Expand Up @@ -1225,6 +1231,7 @@
'ex-cause (copy-core-var ex-cause)
'find-ns (core-var 'find-ns sci-find-ns true #_{:sci.impl/op needs-ctx})
'create-ns (core-var 'create-ns sci-create-ns true #_{:sci.impl/op needs-ctx})
'in-ns (core-var 'in-ns sci-in-ns true #_{:sci.impl/op needs-ctx})
'find-var (core-var 'find-var sci-find-var true)
'first (copy-core-var first)
'float? (copy-core-var float?)
Expand Down
5 changes: 5 additions & 0 deletions test/sci/namespaces_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
(is (= :clojure.string/foo (eval* "(in-ns 'foo) (require '[clojure.string :as str]) ::str/foo")))
(is (= :clojure.string/foo (eval* "(ns foo (:require [clojure.string :as s])) ::s/foo"))))

(deftest in-ns-test
(is (= :user/foo (eval* "::foo")))
(is (= :bar/foo (eval* "(in-ns 'bar) ::foo")))
(is (= :bar/foo (eval* "(in-ns 'bar) (def just-one-ns ::foo) (in-ns 'bar) just-one-ns"))))

(deftest vars-partitioned-by-namespace-test
(is (= 10 (eval* "(in-ns 'foo) (def x 10) (in-ns 'bar) (def x 11) (in-ns 'foo) x"))))

Expand Down

0 comments on commit f8662b9

Please sign in to comment.