Skip to content

Commit d64b233

Browse files
committed
CLJS-2886: count specializations for string and array
1 parent f312d5d commit d64b233

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/main/clojure/cljs/compiler.cljc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,11 @@
11361136
(not ('#{any clj clj-or-nil clj-nil number string boolean function object array js} tag))
11371137
(when-let [ps (:protocols (ana/resolve-existing-var env tag))]
11381138
(ps protocol)))))))
1139+
first-arg-tag (ana/infer-tag env (first (:args expr)))
11391140
opt-not? (and (= (:name info) 'cljs.core/not)
1140-
(= (ana/infer-tag env (first (:args expr))) 'boolean))
1141+
(= first-arg-tag 'boolean))
1142+
opt-count? (and (= (:name info) 'cljs.core/count)
1143+
(boolean ('#{string array} first-arg-tag)))
11411144
ns (:ns info)
11421145
js? (or (= ns 'js) (= ns 'Math))
11431146
goog? (when ns
@@ -1193,6 +1196,9 @@
11931196
opt-not?
11941197
(emits "(!(" (first args) "))")
11951198

1199+
opt-count?
1200+
(emits "((" (first args) ").length)")
1201+
11961202
proto?
11971203
(let [pimpl (str (munge (protocol-prefix protocol))
11981204
(munge (name (:name info))) "$arity$" (count args))]

src/test/cljs/cljs/core_test.cljs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,14 @@
17031703
(is (= "ab" (str "a" nil "b")))
17041704
(is (= "ahellob" (str "a" (str-fn-2865) "b"))))
17051705

1706+
(deftest test-cljs-2886
1707+
(is (zero? (count "")))
1708+
(is (== 1 (count "a")))
1709+
(is (zero? (count #js [])))
1710+
(is (== 1 (count #js [1])))
1711+
(is (zero? (count [])))
1712+
(is (== 1 (count [1]))))
1713+
17061714
(deftest test-cljs-2934
17071715
(let [x (delay 1)]
17081716
(is (= "#object[cljs.core.Delay {:status :pending, :val nil}]" (pr-str x)))

0 commit comments

Comments
 (0)