Skip to content

Commit

Permalink
CLJS-982: Var derefing should respect Clojure semantics
Browse files Browse the repository at this point in the history
Var derefing now happens via a thunk. This supports the Clojure pattern
of passing a function to some library you don't control where you still
want redef to work correctly.
  • Loading branch information
swannodette committed Jan 19, 2015
1 parent 359ea4e commit 522fbdc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
6 changes: 4 additions & 2 deletions src/clj/cljs/compiler.clj
Expand Up @@ -254,8 +254,10 @@

(defmethod emit* :var-special
[{:keys [env var sym meta] :as arg}]
(emit-wrap env
(emits "new cljs.core.Var(" var "," sym "," meta ")")))
(let [{:keys [name]} (:info var)]
(emit-wrap env
(emits "new cljs.core.Var(function(){return " (munge name) ";},"
sym "," meta ")"))))

(defmethod emit* :meta
[{:keys [expr meta env]}]
Expand Down
46 changes: 23 additions & 23 deletions src/cljs/cljs/core.cljs
Expand Up @@ -631,55 +631,55 @@

(deftype Var [val sym _meta]
IDeref
(-deref [_] val)
(-deref [_] (val))
IMeta
(-meta [_] _meta)
Fn
IFn
(-invoke [_]
(val))
((val)))
(-invoke [_ a]
(val a))
((val) a))
(-invoke [_ a b]
(val a b))
((val) a b))
(-invoke [_ a b c]
(val a b c))
((val) a b c))
(-invoke [_ a b c d]
(val a b c d))
((val) a b c d))
(-invoke [_ a b c d e]
(val a b c d e))
((val) a b c d e))
(-invoke [_ a b c d e f]
(val a b c d e f))
((val) a b c d e f))
(-invoke [_ a b c d e f g]
(val a b c d e f g))
((val) a b c d e f g))
(-invoke [_ a b c d e f g h]
(val a b c d e f g h))
((val) a b c d e f g h))
(-invoke [_ a b c d e f g h i]
(val a b c d e f g h i))
((val) a b c d e f g h i))
(-invoke [_ a b c d e f g h i j]
(val a b c d e f g h i j))
((val) a b c d e f g h i j))
(-invoke [_ a b c d e f g h i j k]
(val a b c d e f g h i j k))
((val) a b c d e f g h i j k))
(-invoke [_ a b c d e f g h i j k l]
(val a b c d e f g h i j k l))
((val) a b c d e f g h i j k l))
(-invoke [_ a b c d e f g h i j k l m]
(val a b c d e f g h i j k l m))
((val) a b c d e f g h i j k l m))
(-invoke [_ a b c d e f g h i j k l m n]
(val a b c d e f g h i j k l m n))
((val) a b c d e f g h i j k l m n))
(-invoke [_ a b c d e f g h i j k l m n o]
(val a b c d e f g h i j k l m n o))
((val) a b c d e f g h i j k l m n o))
(-invoke [_ a b c d e f g h i j k l m n o p]
(val a b c d e f g h i j k l m n o p))
((val) a b c d e f g h i j k l m n o p))
(-invoke [_ a b c d e f g h i j k l m n o p q]
(val a b c d e f g h i j k l m n o p q))
((val) a b c d e f g h i j k l m n o p q))
(-invoke [_ a b c d e f g h i j k l m n o p q r]
(val a b c d e f g h i j k l m n o p q r))
((val) a b c d e f g h i j k l m n o p q r))
(-invoke [_ a b c d e f g h i j k l m n o p q r s]
(val a b c d e f g h i j k l m n o p q r s))
((val) a b c d e f g h i j k l m n o p q r s))
(-invoke [_ a b c d e f g h i j k l m n o p q r s t]
(val a b c d e f g h i j k l m n o p q r s t))
((val) a b c d e f g h i j k l m n o p q r s t))
(-invoke [_ a b c d e f g h i j k l m n o p q r s t rest]
(apply val a b c d e f g h i j k l m n o p q r s t rest)))
(apply (val) a b c d e f g h i j k l m n o p q r s t rest)))

;;;;;;;;;;;;;;;;;;; fundamentals ;;;;;;;;;;;;;;;

Expand Down
13 changes: 13 additions & 0 deletions test/cljs/cljs/core_test.cljs
Expand Up @@ -2624,6 +2624,19 @@
(is (= (get m 3) 4))
(is (= m {1 2 3 4}))))

(defn foo-var [f]
(fn [x]
(f x)))

(defn foo-set [x]
(first x))

(deftest test-cljs-982-var-deref []
(let [f (foo-var #'foo-set)]
(is (= (f [1 2 3]) 1))
(set! foo-set (fn [x] :oops))
(is (= (f [1 2 3]) :oops))))

(comment
;; ObjMap
;; (let [ks (map (partial str "foo") (range 500))
Expand Down

0 comments on commit 522fbdc

Please sign in to comment.