Skip to content

Commit f165420

Browse files
author
dnolen
committed
CLJS-1284: IndexedSeq -seq implementation incorrect for i >= alength of internal array
for performance reasons we sometimes inline calls to the IndexedSeq constructor, however this bypasses the validation offered by prim-seq and array-seq. Fix IndexedSeq -seq so that it checks current index to length of array. Fix IndexedSeq -count so that negative values are never returned. Add tests.
1 parent 46af737 commit f165420

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,9 @@ reduces them without incurring seq initialization"
12561256
(-clone [_] (IndexedSeq. arr i))
12571257

12581258
ISeqable
1259-
(-seq [this] this)
1259+
(-seq [this]
1260+
(when (< i (alength arr))
1261+
this))
12601262

12611263
ASeq
12621264
ISeq
@@ -1271,7 +1273,8 @@ reduces them without incurring seq initialization"
12711273
nil))
12721274

12731275
ICounted
1274-
(-count [_] (- (alength arr) i))
1276+
(-count [_]
1277+
(max 0 (- (alength arr) i)))
12751278

12761279
IIndexed
12771280
(-nth [coll n]

src/test/cljs/cljs/core_test.cljs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,6 +2833,22 @@
28332833
(is (= (get #js [\h \i] 1.7) \i))
28342834
(is (= (get "hi" 1.7) \i)))
28352835

2836+
(defn foo-1284
2837+
([] nil)
2838+
([x0 & xs] [x0 xs]))
2839+
2840+
(deftest test-cljs-1284
2841+
(let [xs (IndexedSeq. #js [] 0)
2842+
ys (IndexedSeq. #js [1] 3)]
2843+
(is (nil? (first xs)))
2844+
(is (nil? (seq xs)))
2845+
(is (= (rest xs) ()))
2846+
(is (= (pr-str xs) "()"))
2847+
(is (= (foo-1284 0) [0 ()]))
2848+
(is (= (pr-str (foo-1284 0)) "[0 ()]"))
2849+
(is (zero? (count ys)))
2850+
(is (= (transduce (map inc) conj [] ys) []))))
2851+
28362852
(comment
28372853
;; ObjMap
28382854
;; (let [ks (map (partial str "foo") (range 500))

0 commit comments

Comments
 (0)