Skip to content

Commit 89914d2

Browse files
athosswannodette
authored andcommitted
CLJS-2352: Emit valid JS for NaN etc. even when used w/ CLJ >= 1.9.0-alpha20
1 parent 245bdee commit 89914d2

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/main/clojure/cljs/compiler.cljc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,24 @@
248248
(defmethod emit-constant Integer [x] (emits x))) ; reader puts Integers in metadata
249249

250250
#?(:clj
251-
(defmethod emit-constant Double [x] (emits x))
251+
(defmethod emit-constant Double [x]
252+
(let [x (double x)]
253+
(cond (Double/isNaN x)
254+
(emits "NaN")
255+
256+
(Double/isInfinite x)
257+
(emits (if (pos? x) "Infinity" "-Infinity"))
258+
259+
:else (emits x))))
252260
:cljs
253-
(defmethod emit-constant js/Number [x] (emits "(" x ")")))
261+
(defmethod emit-constant js/Number [x]
262+
(cond (js/isNaN x)
263+
(emits "NaN")
264+
265+
(not (js/isFinite x))
266+
(emits (if (pos? x) "Infinity" "-Infinity"))
267+
268+
:else (emits "(" x ")"))))
254269

255270
#?(:clj
256271
(defmethod emit-constant BigDecimal [x] (emits (.doubleValue ^BigDecimal x))))

src/test/clojure/cljs/compiler_tests.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@
6565
(ana/analyze (assoc aenv :context :expr) 'js/-Infinity)))
6666
"-Infinity")))
6767

68+
(deftest test-cljs-2352
69+
(are [form result]
70+
(= (with-out-str
71+
(comp/emit
72+
(ana/analyze (assoc aenv :context :expr) form)))
73+
result)
74+
Double/NaN "##NaN"
75+
Double/POSITIVE_INFINITY "##Inf"
76+
Double/NEGATIVE_INFINITY "##-Inf"))
77+
6878
(deftest test-munge-dotdot
6979
(is (= 'cljs.core._DOT__DOT_ (comp/munge 'cljs.core/..)))
7080
(is (= "cljs.core._DOT__DOT_" (comp/munge "cljs.core/..")))

0 commit comments

Comments
 (0)