From 3ca249a5a3ad07586c91b143463374a89d1fcf25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Marczyk?= Date: Mon, 1 Apr 2013 05:33:01 +0200 Subject: [PATCH] CLJS-491: avoid layered subvecs cljs.core/build-subvec now checks whether its vector argument is already a Subvec and, if so, unwraps it, so that the return value of cljs.core/subvec is always a view on a regular vector. This is in line with the behaviour of subvec in Clojure. Includes a basic test. --- src/cljs/cljs/core.cljs | 16 +++++++++------- test/cljs/cljs/core_test.cljs | 2 ++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/cljs/cljs/core.cljs b/src/cljs/cljs/core.cljs index d791021866..14e8c7c19b 100644 --- a/src/cljs/cljs/core.cljs +++ b/src/cljs/cljs/core.cljs @@ -3359,13 +3359,15 @@ reduces them without incurring seq initialization" (-lookup coll k not-found))) (defn- build-subvec [meta v start end __hash] - (let [c (count v)] - (when (or (neg? start) - (neg? end) - (> start c) - (> end c)) - (throw (js/Error. "Index out of bounds"))) - (Subvec. meta v start end __hash))) + (if (instance? Subvec v) + (recur meta (.-v v) (+ (.-start v) start) (+ (.-start v) end) __hash) + (let [c (count v)] + (when (or (neg? start) + (neg? end) + (> start c) + (> end c)) + (throw (js/Error. "Index out of bounds"))) + (Subvec. meta v start end __hash)))) (defn subvec "Returns a persistent vector of the items in vector from diff --git a/test/cljs/cljs/core_test.cljs b/test/cljs/cljs/core_test.cljs index 2a473c4252..fc73c29a49 100644 --- a/test/cljs/cljs/core_test.cljs +++ b/test/cljs/cljs/core_test.cljs @@ -1047,6 +1047,8 @@ (assert (= :fail (try (subvec v2 6 10) (catch js/Error e :fail)))) (assert (= :fail (try (subvec v2 6 10) (catch js/Error e :fail)))) (assert (= :fail (try (subvec v2 3 6) (catch js/Error e :fail)))) + ;; no layered subvecs + (assert (identical? v1 (.-v (subvec s 1 4)))) ) ;; TransientVector