Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
CLJS-767: Fix (assoc [0] nil 1); make PersistentVector and Subvec -as…
…soc call -assoc-n.
  • Loading branch information
favila authored and swannodette committed Feb 20, 2014
1 parent e16a3da commit a7f7ea3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/cljs/cljs/core.cljs
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
11 changes: 11 additions & 0 deletions test/cljs/cljs/core_test.cljs
Expand Up @@ -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
)

0 comments on commit a7f7ea3

Please sign in to comment.