Skip to content

Commit

Permalink
* split number? into js-number? and bigint? checks
Browse files Browse the repository at this point in the history
* emit BigInt as JS
* switch to js/Number.isNaN instead of js/isNaN
  • Loading branch information
swannodette committed Sep 25, 2023
1 parent df04ee2 commit 516564e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/main/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,10 @@
(instance? js/Array x)))

(defn ^boolean number?
"Returns true if x is a JavaScript number."
"Returns true if x is a JavaScript Number or BigInt"
[x]
(cljs.core/number? x))
(or (cljs.core/js-number? x)
(cljs.core/bigint? x)))

(defn not
"Returns true if x is logical false, false otherwise."
Expand Down Expand Up @@ -2313,7 +2314,7 @@ reduces them without incurring seq initialization"
"Returns true if n is a JavaScript number with no decimal part."
[n]
(and (number? n)
(not ^boolean (js/isNaN n))
(not ^boolean (js/Number.isNaN n))
(not (identical? n js/Infinity))
(== (js/parseFloat n) (js/parseInt n 10))))

Expand Down Expand Up @@ -10509,10 +10510,10 @@ reduces them without incurring seq initialization"
(number? obj)
(-write writer
(cond
^boolean (js/isNaN obj) "##NaN"
^boolean (js/Number.isNaN obj) "##NaN"
(identical? obj js/Number.POSITIVE_INFINITY) "##Inf"
(identical? obj js/Number.NEGATIVE_INFINITY) "##-Inf"
:else (str obj)))
:else (str obj (when (bigint? obj) "N"))))

(object? obj)
(do
Expand Down Expand Up @@ -12211,7 +12212,7 @@ reduces them without incurring seq initialization"
(defn ^boolean NaN?
"Returns true if num is NaN, else false"
[val]
(js/isNaN val))
(js/Number.isNaN val))

(defn ^:private parsing-err
"Construct message for parsing for non-string parsing error"
Expand Down
3 changes: 2 additions & 1 deletion src/main/clojure/cljs/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@
(defmethod emit-constant* BigDecimal [x] (emits (.doubleValue ^BigDecimal x))))

#?(:clj
(defmethod emit-constant* clojure.lang.BigInt [x] (emits (.doubleValue ^clojure.lang.BigInt x))))
(defmethod emit-constant* clojure.lang.BigInt [x]
(emits "(" (.toString ^clojure.lang.BigInt x) "n)")))

(defmethod emit-constant* #?(:clj String :cljs js/String) [x]
(emits (wrap-in-double-quotes (escape-string x))))
Expand Down
5 changes: 4 additions & 1 deletion src/main/clojure/cljs/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1007,9 +1007,12 @@
`(let [c# ~c x# ~x]
(~'js* "(~{} instanceof ~{})" x# c#)))))

(core/defmacro number? [x]
(core/defmacro js-number? [x]
(bool-expr (core/list 'js* "typeof ~{} === 'number'" x)))

(core/defmacro bigint? [x]
(bool-expr (core/list 'js* "typeof ~{} === 'bigint'" x)))

(core/defmacro symbol? [x]
(bool-expr `(instance? Symbol ~x)))

Expand Down

0 comments on commit 516564e

Please sign in to comment.