Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CLJS-767: Fix (assoc [0] nil 1); make PersistentVector and Subvec -as…

…soc call -assoc-n.
  • Loading branch information...
commit a7f7ea3b5be1dd450d5503766024cbde9c731147 1 parent e16a3da
@favila favila authored swannodette committed
Showing with 29 additions and 14 deletions.
  1. +18 −14 src/cljs/cljs/core.cljs
  2. +11 −0 test/cljs/cljs/core_test.cljs
View
32 src/cljs/cljs/core.cljs
@@ -3391,18 +3391,21 @@ reduces them without incurring seq initialization"
IAssociative
(-assoc [coll k v]
+ (if (number? k)
+ (-assoc-n coll k v)
+ (throw (js/Error. "Vector's key for assoc must be a number."))))
+
+ IVector
+ (-assoc-n [coll n val]
(cond
- (and (<= 0 k) (< k cnt))
- (if (<= (tail-off coll) k)
+ (and (<= 0 n) (< n cnt))
+ (if (<= (tail-off coll) n)
(let [new-tail (aclone tail)]
- (aset new-tail (bit-and k 0x01f) v)
+ (aset new-tail (bit-and n 0x01f) val)
(PersistentVector. meta cnt shift root new-tail nil))
- (PersistentVector. meta cnt shift (do-assoc coll shift root k v) tail nil))
- (== k cnt) (-conj coll v)
- :else (throw (js/Error. (str "Index " k " out of bounds [0," cnt "]")))))
-
- IVector
- (-assoc-n [coll n val] (-assoc coll n val))
+ (PersistentVector. meta cnt shift (do-assoc coll shift root n val) tail nil))
+ (== n cnt) (-conj coll val)
+ :else (throw (js/Error. (str "Index " n " out of bounds [0," cnt "]")))))
IReduce
(-reduce [v f]
@@ -3629,13 +3632,14 @@ reduces them without incurring seq initialization"
IAssociative
(-assoc [coll key val]
- (let [v-pos (+ start key)]
- (build-subvec meta (assoc v v-pos val)
- start (max end (inc v-pos))
- nil)))
+ (if (number? key)
+ (-assoc-n coll key val)
+ (throw (js/Error. "Subvec's key for assoc must be a number."))))
IVector
- (-assoc-n [coll n val] (-assoc coll n val))
+ (-assoc-n [coll n val]
+ (let [v-pos (+ start n)]
+ (build-subvec meta (assoc v v-pos val) start (max end (inc v-pos)) nil)))
IReduce
(-reduce [coll f]
View
11 test/cljs/cljs/core_test.cljs
@@ -2076,5 +2076,16 @@
(assert (= (-> (transient {}) (assoc! :a 1 :b 2) persistent!) {:a 1 :b 2}))
(assert (= (-> (transient {:a 1 :b 2 :c 3}) (dissoc! :a :b) persistent!) {:c 3}))
+
+ ;; CLJS-767
+
+ (doseq [n [nil "-1" "" "0" "1" false true (js-obj)]]
+ (assert (= :fail (try (assoc [1 2] n 4)
+ (catch js/Error e :fail))))
+ (assert (= :fail (try (assoc (subvec [1 2 3] 2) n 4)
+ (catch js/Error e :fail))))
+ (assert (= :fail (try (assoc (range 1 3) n 4)
+ (catch js/Error e :fail)))))
+
:ok
)
Please sign in to comment.
Something went wrong with that request. Please try again.