From 516564e585f36fc3a1764c672615dc5f416832b8 Mon Sep 17 00:00:00 2001 From: davidnolen Date: Mon, 25 Sep 2023 14:57:24 -0400 Subject: [PATCH] * split number? into js-number? and bigint? checks * emit BigInt as JS * switch to js/Number.isNaN instead of js/isNaN --- src/main/cljs/cljs/core.cljs | 13 +++++++------ src/main/clojure/cljs/compiler.cljc | 3 ++- src/main/clojure/cljs/core.cljc | 5 ++++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main/cljs/cljs/core.cljs b/src/main/cljs/cljs/core.cljs index 620bc8abc..29e793e9b 100644 --- a/src/main/cljs/cljs/core.cljs +++ b/src/main/cljs/cljs/core.cljs @@ -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." @@ -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)))) @@ -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 @@ -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" diff --git a/src/main/clojure/cljs/compiler.cljc b/src/main/clojure/cljs/compiler.cljc index b96c09b36..2ef7c8c76 100644 --- a/src/main/clojure/cljs/compiler.cljc +++ b/src/main/clojure/cljs/compiler.cljc @@ -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)))) diff --git a/src/main/clojure/cljs/core.cljc b/src/main/clojure/cljs/core.cljc index 72f465427..548cec089 100644 --- a/src/main/clojure/cljs/core.cljc +++ b/src/main/clojure/cljs/core.cljc @@ -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)))