Skip to content
Browse files

ASYNC-15 : add locals to &env

  • Loading branch information...
1 parent 12af7d8 commit 9371afbe6b90270f005eb003a08fdcbb9aaa6117 @halgari halgari committed Aug 2, 2013
View
3 src/main/clojure/cljs/core/async/impl/ioc_macros.clj
@@ -544,7 +544,8 @@
(if (or (get locals s)
(special-override? s))
form
- (let [expanded (cljs/macroexpand-1 env form)]
+ (let [new-env (update-in env [:locals] merge locals)
+ expanded (cljs/macroexpand-1 new-env form)]
(if (= expanded form)
form
(recur expanded))))
View
8 src/main/clojure/clojure/core/async/impl/ioc_macros.clj
@@ -550,7 +550,13 @@
(if (or (get locals s)
(special-override? s))
form
- (let [expanded (macroexpand-1 form)]
+ (let [LOCAL_ENV clojure.lang.Compiler/LOCAL_ENV
+ expanded (try
+ (push-thread-bindings
+ {LOCAL_ENV (merge @LOCAL_ENV locals)})
+ (macroexpand-1 form)
+ (finally
+ (pop-thread-bindings)))]
(if (= expanded form)
form
(recur expanded))))
View
134 src/test/cljs/cljs/core/async/runner_tests.cljs
@@ -4,7 +4,7 @@
[cljs.core.async.impl.buffers :as buff]
[cljs.core.async.impl.protocols :refer [full? add! remove!]]
[cljs.core.async.impl.ioc-helpers :as ioch])
- (:require-macros [cljs.core.async.test-helpers :as h :refer [is= is deftest testing runner]]
+ (:require-macros [cljs.core.async.test-helpers :as h :refer [is= is deftest testing runner locals-test]]
[cljs.core.async.macros :as m :refer [go]]
[cljs.core.async.impl.ioc-macros :as ioc]))
@@ -13,71 +13,75 @@
:recur)
(deftest runner-tests
+ (testing "macros add locals to the env"
+ (is= :pass
+ (runner (let [x 42]
+ (pause (locals-test))))))
(testing "do blocks"
(is= 42
- (runner (do (pause 42))))
+ (runner (do (pause 42))))
(is= 42
- (runner (do (pause 44)
- (pause 42)))))
+ (runner (do (pause 44)
+ (pause 42)))))
(testing "if expressions"
(is= true
- (runner (if (pause true)
- (pause true)
- (pause false))))
+ (runner (if (pause true)
+ (pause true)
+ (pause false))))
(is= false
- (runner (if (pause false)
- (pause true)
- (pause false))))
+ (runner (if (pause false)
+ (pause true)
+ (pause false))))
(is= true
- (runner (when (pause true)
- (pause true))))
+ (runner (when (pause true)
+ (pause true))))
(is= nil
- (runner (when (pause false)
- (pause true)))))
+ (runner (when (pause false)
+ (pause true)))))
(testing "loop expressions"
(is= 100
- (runner (loop [x 0]
- (if (< x 100)
- (recur (inc (pause x)))
- (pause x))))))
+ (runner (loop [x 0]
+ (if (< x 100)
+ (recur (inc (pause x)))
+ (pause x))))))
(testing "let expressions"
(is= 3
- (runner (let [x 1 y 2]
- (+ x y)))))
+ (runner (let [x 1 y 2]
+ (+ x y)))))
(testing "vector destructuring"
(is= 3
(runner (let [[x y] [1 2]]
- (+ x y)))))
+ (+ x y)))))
(testing "hash-map destructuring"
(is= 3
- (runner (let [{:keys [x y] x2 :x y2 :y :as foo} {:x 1 :y 2}]
- (assert (and foo (pause x) y x2 y2 foo))
- (+ x y)))))
+ (runner (let [{:keys [x y] x2 :x y2 :y :as foo} {:x 1 :y 2}]
+ (assert (and foo (pause x) y x2 y2 foo))
+ (+ x y)))))
(testing "hash-map literals"
(is= {:1 1 :2 2 :3 3}
- (runner {:1 (pause 1)
- :2 (pause 2)
- :3 (pause 3)})))
+ (runner {:1 (pause 1)
+ :2 (pause 2)
+ :3 (pause 3)})))
(testing "hash-set literals"
(is= #{1 2 3}
- (runner #{(pause 1)
- (pause 2)
- (pause 3)})))
+ (runner #{(pause 1)
+ (pause 2)
+ (pause 3)})))
(testing "vector literals"
(is= [1 2 3]
- (runner [(pause 1)
- (pause 2)
- (pause 3)])))
+ (runner [(pause 1)
+ (pause 2)
+ (pause 3)])))
(testing "dotimes"
- (is= 42 (runner
- (dotimes [x 10]
- (pause x))
- 42)))
+ (is= 42 (runner
+ (dotimes [x 10]
+ (pause x))
+ 42)))
(testing "keywords as functions"
(is (= :bar
@@ -89,47 +93,47 @@
(testing "quote"
(is= '(1 2 3)
- (runner (pause '(1 2 3)))))
+ (runner (pause '(1 2 3)))))
(testing "fn closures"
(is= 42
- (runner
- (let [x 42
- _ (pause x)
- f (fn [] x)]
- (f)))))
+ (runner
+ (let [x 42
+ _ (pause x)
+ f (fn [] x)]
+ (f)))))
(testing "case"
(is= 43
- (runner
- (let [value :bar]
- (case value
- :foo (pause 42)
- :bar (pause 43)
- :baz (pause 44)))))
+ (runner
+ (let [value :bar]
+ (case value
+ :foo (pause 42)
+ :bar (pause 43)
+ :baz (pause 44)))))
(is= :default
- (runner
- (case :baz
- :foo 44
- :default)))
+ (runner
+ (case :baz
+ :foo 44
+ :default)))
(is= 42
- (runner
- (loop [x 0]
- (case (int x)
- 0 (recur (inc x))
- 1 42)))))
+ (runner
+ (loop [x 0]
+ (case (int x)
+ 0 (recur (inc x))
+ 1 42)))))
(testing "try"
(is= 42
- (runner
- (try 42
- (catch js/Error ex ex))))
+ (runner
+ (try 42
+ (catch js/Error ex ex))))
(is= 42
- (runner
- (try
- (assert false)
- (catch js/Error ex 42))))
+ (runner
+ (try
+ (assert false)
+ (catch js/Error ex 42))))
(let [a (atom false)
v (runner
View
5 src/test/cljs/cljs/core/async/test_helpers.clj
@@ -31,3 +31,8 @@
(defmacro is
[a]
`(assert ~a))
+
+(defmacro locals-test []
+ (if (get-in &env [:locals] 'x)
+ :pass
+ :fail))
View
10 src/test/clojure/clojure/core/async/ioc_macros_test.clj
@@ -18,7 +18,17 @@
(assert (ioc/finished? state#) "state did not return finished")
(ioc/aget-object state# ioc/VALUE-IDX))))
+(defmacro locals-test []
+ (if (get &env 'x)
+ :pass
+ :fail))
+
+
(deftest runner-tests
+ (testing "macros add locals to the env"
+ (is (= :pass
+ (runner (let [x 42]
+ (pause (locals-test)))))))
(testing "fn as first arg in sexpr"
(is (= 42
(runner ((fn [] 42))))))

0 comments on commit 9371afb

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