Skip to content

Commit

Permalink
CLJS-541: Fix macro arg eval orders
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonbloom authored and swannodette committed Jul 16, 2013
1 parent 1b9eae2 commit 37b4cce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/clj/cljs/core.clj
Expand Up @@ -215,6 +215,7 @@

;; internal - do not use.
(defmacro truth_ [x]
(assert (clojure.core/symbol? x) "x is substituted twice")
(list 'js* "(~{} != null && ~{} !== false)" x x))

;; internal - do not use
Expand Down Expand Up @@ -243,7 +244,13 @@
(bool-expr (list 'js* "(~{} === ~{})" a b)))

(defmacro instance? [t o]
(bool-expr (list 'js* "(~{} instanceof ~{})" o t)))
;; Google Closure warns about some references to RegExp, so
;; (instance? RegExp ...) needs to be inlined, but the expansion
;; should preserve the order of argument evaluation.
(bool-expr (if (clojure.core/symbol? t)
(list 'js* "(~{} instanceof ~{})" o t)
`(let [t# ~t o# ~o]
(~'js* "(~{} instanceof ~{})" o# t#)))))

(defmacro number? [x]
(bool-expr (list 'js* "typeof ~{} === 'number'" x)))
Expand Down Expand Up @@ -388,12 +395,14 @@

(defmacro max
([x] x)
([x y] (list 'js* "((~{} > ~{}) ? ~{} : ~{})" x y x y))
([x y] `(let [x# ~x, y# ~y]
(~'js* "((~{} > ~{}) ? ~{} : ~{})" x# y# x# y#)))
([x y & more] `(max (max ~x ~y) ~@more)))

(defmacro min
([x] x)
([x y] (list 'js* "((~{} < ~{}) ? ~{} : ~{})" x y x y))
([x y] `(let [x# ~x, y# ~y]
(~'js* "((~{} < ~{}) ? ~{} : ~{})" x# y# x# y#)))
([x y & more] `(min (min ~x ~y) ~@more)))

(defmacro js-mod [num div]
Expand Down Expand Up @@ -457,6 +466,7 @@

;; internal
(defmacro caching-hash [coll hash-fn hash-key]
(assert (clojure.core/symbol? hash-key) "hash-key is substituted twice")
`(let [h# ~hash-key]
(if-not (nil? h#)
h#
Expand Down
9 changes: 9 additions & 0 deletions test/cljs/cljs/core_test.cljs
Expand Up @@ -1917,5 +1917,14 @@
;; CLJS-518
(assert (nil? (:test "test")))

;; CLJS-541
(letfn [(f! [x] (print \f) x)
(g! [x] (print \g) x)]
(assert (== "ffgfg"
(with-out-str
(instance? Symbol (f! 'foo))
(max (f! 5) (g! 10))
(min (f! 5) (g! 10))))))

:ok
)

0 comments on commit 37b4cce

Please sign in to comment.