Skip to content

Commit 9a55107

Browse files
authored
cljs.proxy doesn't handle for .. of correctly (#298)
- check for Symbol.iterator case in vec and map handler - need to bind iterator to target for it to work - add cljs.proxy.impl/MapIterator * apply the proxy ctor to the iterator results
1 parent 5bb9912 commit 9a55107

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/main/cljs/cljs/proxy.cljs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns cljs.proxy
2-
(:refer-global :only [Proxy isNaN])
3-
(:require [cljs.proxy.impl :refer [SimpleCache]]))
2+
(:refer-global :only [isNaN Proxy Symbol])
3+
(:require [cljs.proxy.impl :refer [SimpleCache MapIterator]]))
44

55
(defn- write-through [f]
66
(let [cache (SimpleCache. #js {} 0)]
@@ -45,16 +45,28 @@
4545
(js* "var __ctor")
4646
(let [cache-key-fn (write-through key-fn)
4747
vec-handler #js {:get (fn [^cljs.core/IIndexed target prop receiver]
48-
(if (identical? prop "length")
48+
(cond
49+
(identical? "length" prop)
4950
(-count ^cljs.core/ICounted target)
51+
52+
(identical? (. Symbol -iterator) prop)
53+
(fn []
54+
(MapIterator.
55+
((.bind (unchecked-get target prop) target)) js/__ctor))
56+
57+
:else
5058
(let [n (js* "+~{}" prop)]
5159
(when (and (number? n)
5260
(not (isNaN n)))
5361
(js/__ctor (-nth target n nil))))))
5462

5563
:has (fn [^cljs.core/IAssociative target prop]
56-
(if (identical? prop "length")
57-
true
64+
(cond
65+
(identical? prop "length") true
66+
67+
(identical? (. Symbol -iterator) prop) true
68+
69+
:else
5870
(let [n (js* "+~{}" prop)]
5971
(and (number? n)
6072
(not (isNaN n))
@@ -150,5 +162,7 @@
150162

151163
(def proxied-deep (proxy [{:foo "Hello"}]))
152164
(-> proxied-deep (aget 0) (unchecked-get "foo"))
165+
166+
(aget ((cljs.proxy/builder) [{}]) 0)
153167

154168
)

src/main/cljs/cljs/proxy/impl.cljs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,12 @@
1313
(clear [this]
1414
(set! obj #js {})
1515
(set! cnt 0)))
16+
17+
(deftype MapIterator [^:mutable iter f]
18+
Object
19+
(next [_]
20+
(let [x (.next iter)]
21+
(if-not ^boolean (. x -done)
22+
#js {:value (f (. x -value))
23+
:done false}
24+
x))))

0 commit comments

Comments
 (0)