Skip to content
Browse files

Encapsulating namespace access and env creation

  • Loading branch information...
1 parent 9179a35 commit 67f55ee0f0744e4b6cb98de6496f353e0156156c @raph-amiard raph-amiard committed with David Nolen Jun 13, 2012
Showing with 18 additions and 12 deletions.
  1. +1 −4 src/clj/cljs/closure.clj
  2. +12 −3 src/clj/cljs/compiler.clj
  3. +5 −5 src/clj/cljs/repl.clj
5 src/clj/cljs/closure.clj
@@ -314,17 +314,14 @@
;; Compile
;; =======
-(defn empty-env []
- {:ns (@comp/namespaces comp/*cljs-ns*) :context :statement :locals {}})
(defn compile-form-seq
"Compile a sequence of forms to a JavaScript source string."
(binding [comp/*cljs-ns* 'cljs.user]
(doseq [form forms]
- (comp/emit (comp/analyze (empty-env) form)))))))
+ (comp/emit (comp/analyze (comp/empty-env) form)))))))
(defn output-directory [opts]
(or (:output-dir opts) "out"))
15 src/clj/cljs/compiler.clj
@@ -48,6 +48,12 @@
'{cljs.core {:name cljs.core}
cljs.user {:name cljs.user}}))
+(defn get-namespace [key]
+ (@namespaces key))
+(defn set-namespace [key val]
+ (swap! namespaces assoc key val))
(def ^:dynamic *cljs-ns* 'cljs.user)
(def ^:dynamic *cljs-file* nil)
(def ^:dynamic *cljs-warn-on-redef* true)
@@ -58,6 +64,9 @@
(def ^:dynamic *cljs-static-fns* false)
(def ^:dynamic *position* nil)
+(defn empty-env []
+ {:ns (@namespaces *cljs-ns*) :context :statement :locals {}})
(defmacro ^:private debug-prn
[& args]
`(.println System/err (str ~@args)))
@@ -1604,11 +1613,11 @@
(binding [*cljs-ns* 'cljs.user
*cljs-file* (.getPath ^ res)]
(with-open [r (io/reader res)]
- (let [env {:ns (@namespaces *cljs-ns*) :context :statement :locals {}}
+ (let [env (empty-env)
pbr (clojure.lang.LineNumberingPushbackReader. r)
eof (Object.)]
(loop [r (read pbr false eof false)]
- (let [env (assoc env :ns (@namespaces *cljs-ns*))]
+ (let [env (assoc env :ns (get-namespace *cljs-ns*))]
(when-not (identical? eof r)
(analyze env r)
(recur (read pbr false eof false))))))))))
@@ -1654,7 +1663,7 @@
ns-name nil
deps nil]
(if (seq forms)
- (let [env {:ns (@namespaces *cljs-ns*) :context :statement :locals {}}
+ (let [env (empty-env)
ast (analyze env (first forms))]
(do (emit ast)
(if (= (:op ast) :ns)
10 src/clj/cljs/repl.clj
@@ -90,11 +90,11 @@
(defn load-stream [repl-env filename stream]
(with-open [r (io/reader stream)]
- (let [env {:ns (@comp/namespaces comp/*cljs-ns*) :context :statement :locals {}}
+ (let [env (comp/empty-env)
pbr (clojure.lang.LineNumberingPushbackReader. r)
eof (Object.)]
(loop [r (read pbr false eof false)]
- (let [env (assoc env :ns (@comp/namespaces comp/*cljs-ns*))]
+ (let [env (assoc env :ns (comp/get-namespace comp/*cljs-ns*))]
(when-not (identical? eof r)
(evaluate-form repl-env env filename r)
(recur (read pbr false eof false))))))))
@@ -118,7 +118,7 @@
(defn- eval-and-print [repl-env env form]
(let [ret (evaluate-form repl-env
- (assoc env :ns (@comp/namespaces comp/*cljs-ns*))
+ (assoc env :ns (comp/get-namespace comp/*cljs-ns*))
"<cljs repl>"
(wrap-fn form))]
@@ -140,8 +140,8 @@
(let [load-file-fn (fn [repl-env file] (load-file repl-env file))]
{'in-ns (fn [_ quoted-ns]
(let [ns-name (second quoted-ns)]
- (when-not (@comp/namespaces ns-name)
- (swap! comp/namespaces assoc ns-name {:name ns-name}))
+ (when-not (comp/get-namespace ns-name)
+ (comp/set-namespace ns-name {:name ns-name}))
(set! comp/*cljs-ns* ns-name)))
'load-file load-file-fn
'clojure.core/load-file load-file-fn

0 comments on commit 67f55ee

Please sign in to comment.
Something went wrong with that request. Please try again.