Skip to content

Commit b549ba5

Browse files
committed
es6 interop tests passing
- implement es6 collection compatibility - include cljs.intero-test in lite-test-runner
1 parent 37255cd commit b549ba5

File tree

3 files changed

+84
-24
lines changed

3 files changed

+84
-24
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12432,6 +12432,8 @@ reduces them without incurring seq initialization"
1243212432
IPrintWithWriter
1243312433
(-pr-writer [coll writer opts] (pr-sequential-writer writer pr-writer "[" " " "]" opts coll)))
1243412434

12435+
(es6-iterable PersistentVector)
12436+
1243512437
(set! (. Vector -EMPTY) (Vector. nil (array) nil))
1243612438

1243712439
(set! (. Vector -fromArray) (fn [xs] (Vector. nil xs nil)))
@@ -12501,19 +12503,38 @@ reduces them without incurring seq initialization"
1250112503
i
1250212504
(recur (+ i incr)))))))
1250312505

12504-
(deftype ObjMap [meta keys strobj ^:mutable __hash]
12506+
(deftype ObjMap [meta strkeys strobj ^:mutable __hash]
1250512507
Object
1250612508
(toString [coll]
1250712509
(pr-str* coll))
12510+
(keys [coll]
12511+
(es6-iterator
12512+
(prim-seq
12513+
(.map (.sort strkeys obj-map-compare-keys)
12514+
obj-map-key->keyword))))
12515+
(entries [coll]
12516+
(es6-entries-iterator (-seq coll)))
12517+
(values [coll]
12518+
(es6-iterator
12519+
(prim-seq
12520+
(.map (.sort strkeys obj-map-compare-keys)
12521+
#(unchecked-get strobj %)))))
12522+
(has [coll k]
12523+
(contains? coll k))
12524+
(get [coll k not-found]
12525+
(-lookup coll k not-found))
12526+
(forEach [coll f]
12527+
(.forEach (.sort strkeys obj-map-compare-keys)
12528+
#(f (unchecked-get strobj %) (obj-map-key->keyword %))))
1250812529

1250912530
IWithMeta
12510-
(-with-meta [coll meta] (ObjMap. meta keys strobj __hash))
12531+
(-with-meta [coll meta] (ObjMap. meta strkeys strobj __hash))
1251112532

1251212533
IMeta
1251312534
(-meta [coll] meta)
1251412535

1251512536
ICloneable
12516-
(-clone [coll] (ObjMap. meta keys strobj __hash))
12537+
(-clone [coll] (ObjMap. meta strkeys strobj __hash))
1251712538

1251812539
ICollection
1251912540
(-conj [coll entry]
@@ -12532,33 +12553,33 @@ reduces them without incurring seq initialization"
1253212553

1253312554
ISeqable
1253412555
(-seq [coll]
12535-
(when (pos? (alength keys))
12556+
(when (pos? (alength strkeys))
1253612557
(prim-seq
12537-
(.map (.sort keys obj-map-compare-keys)
12558+
(.map (.sort strkeys obj-map-compare-keys)
1253812559
#(simple-map-entry (obj-map-key->keyword %) (unchecked-get strobj %))))))
1253912560

1254012561
ICounted
12541-
(-count [coll] (alength keys))
12562+
(-count [coll] (alength strkeys))
1254212563

1254312564
ILookup
1254412565
(-lookup [coll k] (-lookup coll k nil))
1254512566
(-lookup [coll k not-found]
1254612567
(let [k (if-not (keyword? k) k (keyword->obj-map-key k))]
1254712568
(if (and (string? k)
12548-
(not (nil? (scan-array 1 k keys))))
12569+
(not (nil? (scan-array 1 k strkeys))))
1254912570
(unchecked-get strobj k)
1255012571
not-found)))
1255112572

1255212573
IAssociative
1255312574
(-assoc [coll k v]
1255412575
(let [k (if-not (keyword? k) k (keyword->obj-map-key k))]
1255512576
(if (string? k)
12556-
(if-not (nil? (scan-array 1 k keys))
12557-
(let [new-strobj (obj-clone strobj keys)]
12577+
(if-not (nil? (scan-array 1 k strkeys))
12578+
(let [new-strobj (obj-clone strobj strkeys)]
1255812579
(gobject/set new-strobj k v)
12559-
(ObjMap. meta keys new-strobj nil)) ;overwrite
12560-
(let [new-strobj (obj-clone strobj keys) ; append
12561-
new-keys (aclone keys)]
12580+
(ObjMap. meta strkeys new-strobj nil)) ;overwrite
12581+
(let [new-strobj (obj-clone strobj strkeys) ; append
12582+
new-keys (aclone strkeys)]
1256212583
(gobject/set new-strobj k v)
1256312584
(.push new-keys k)
1256412585
(ObjMap. meta new-keys new-strobj nil)))
@@ -12572,21 +12593,21 @@ reduces them without incurring seq initialization"
1257212593
(-contains-key? [coll k]
1257312594
(let [k (if-not (keyword? k) k (keyword->obj-map-key k))]
1257412595
(if (and (string? k)
12575-
(not (nil? (scan-array 1 k keys))))
12596+
(not (nil? (scan-array 1 k strkeys))))
1257612597
true
1257712598
false)))
1257812599

1257912600
IFind
1258012601
(-find [coll k]
1258112602
(let [k (if-not (keyword? k) k (keyword->obj-map-key k))]
1258212603
(when (and (string? k)
12583-
(not (nil? (scan-array 1 k keys))))
12604+
(not (nil? (scan-array 1 k strkeys))))
1258412605
(MapEntry. k (unchecked-get strobj k) nil))))
1258512606

1258612607
IKVReduce
1258712608
(-kv-reduce [coll f init]
12588-
(let [len (alength keys)]
12589-
(loop [keys (.sort keys obj-map-compare-keys)
12609+
(let [len (alength strkeys)]
12610+
(loop [keys (.sort strkeys obj-map-compare-keys)
1259012611
init init]
1259112612
(if (seq keys)
1259212613
(let [k (first keys)
@@ -12600,9 +12621,9 @@ reduces them without incurring seq initialization"
1260012621
(-dissoc [coll k]
1260112622
(let [k (if-not (keyword? k) k (keyword->obj-map-key k))]
1260212623
(if (and (string? k)
12603-
(not (nil? (scan-array 1 k keys))))
12604-
(let [new-keys (aclone keys)
12605-
new-strobj (obj-clone strobj keys)]
12624+
(not (nil? (scan-array 1 k strkeys))))
12625+
(let [new-keys (aclone strkeys)
12626+
new-strobj (obj-clone strobj strkeys)]
1260612627
(.splice new-keys (scan-array 1 k new-keys) 1)
1260712628
(js-delete new-strobj k)
1260812629
(ObjMap. meta new-keys new-strobj nil))
@@ -12643,6 +12664,8 @@ reduces them without incurring seq initialization"
1264312664
(-pr-writer [coll writer opts]
1264412665
(print-map coll pr-writer writer opts)))
1264512666

12667+
(es6-iterable ObjMap)
12668+
1264612669
(set! (. ObjMap -EMPTY) (ObjMap. nil (array) (js-obj) empty-ordered-hash))
1264712670

1264812671
(set! (. ObjMap -fromObject) (fn [ks obj] (ObjMap. nil ks obj nil)))
@@ -12678,6 +12701,21 @@ reduces them without incurring seq initialization"
1267812701
Object
1267912702
(toString [coll]
1268012703
(pr-str* coll))
12704+
(keys [coll]
12705+
(es6-iterator (map #(-key %) (-seq coll))))
12706+
(entries [coll]
12707+
(es6-entries-iterator (-seq coll)))
12708+
(values [coll]
12709+
(es6-iterator (map #(-val %) (-key coll))))
12710+
(has [coll k]
12711+
(contains? coll k))
12712+
(get [coll k not-found]
12713+
(-lookup coll k not-found))
12714+
(forEach [coll f]
12715+
(let [xs (-seq coll)]
12716+
(when-not (nil? xs)
12717+
(.forEach (.-arr xs)
12718+
#(f (-val %) (-key %))))))
1268112719

1268212720
IWithMeta
1268312721
(-with-meta [coll meta] (HashMap. meta count hashobj __hash))
@@ -12813,6 +12851,8 @@ reduces them without incurring seq initialization"
1281312851
(-pr-writer [coll writer opts]
1281412852
(print-map coll pr-writer writer opts)))
1281512853

12854+
(es6-iterable HashMap)
12855+
1281612856
(set! (. HashMap -EMPTY) (HashMap. nil 0 (js-obj) empty-unordered-hash))
1281712857

1281812858
(set! (. HashMap -fromArrays) (fn [ks vs]
@@ -12835,6 +12875,19 @@ reduces them without incurring seq initialization"
1283512875
Object
1283612876
(toString [coll]
1283712877
(pr-str* coll))
12878+
(keys [coll]
12879+
(es6-iterator (-seq coll)))
12880+
(entries [coll]
12881+
(es6-set-entries-iterator (-seq coll)))
12882+
(values [coll]
12883+
(es6-iterator (-seq coll)))
12884+
(has [coll k]
12885+
(contains? coll k))
12886+
(forEach [coll f]
12887+
(let [xs (-seq hash-map)]
12888+
(when (some? xs)
12889+
(.forEach (.-arr xs)
12890+
#(f (-val %) (-key %))))))
1283812891

1283912892
IWithMeta
1284012893
(-with-meta [coll meta] (Set. meta hash-map __hash))
@@ -12911,6 +12964,8 @@ reduces them without incurring seq initialization"
1291112964
IPrintWithWriter
1291212965
(-pr-writer [coll writer opts] (pr-sequential-writer writer pr-writer "#{" " " "}" opts coll)))
1291312966

12967+
(es6-iterable Set)
12968+
1291412969
(set! (. Set -EMPTY) (Set. nil (. HashMap -EMPTY) empty-unordered-hash))
1291512970

1291612971
(defn simple-set

src/test/cljs/cljs/interop_test.cljs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414
(testing "Object equiv method"
1515
(is (.equiv :foo :foo))
1616
(is (.equiv 'foo 'foo))
17-
(is (.equiv {:foo 1 :bar 2} {:foo 1 :bar 2}))
17+
;; .equiv is not a standard thing, primarily for interop
18+
;; in transit-js, probably not a concern for lite-mode users
19+
(when-not LITE_MODE
20+
(is (.equiv {:foo 1 :bar 2} {:foo 1 :bar 2})))
1821
(is (.equiv [1 2 3] [1 2 3]))
1922
(is (.equiv '(1 2 3) '(1 2 3)))
2023
(is (.equiv (map inc [1 2 3]) (map inc [1 2 3])))
21-
(is (.equiv #{:cat :dog :bird} #{:cat :dog :bird}))
22-
))
24+
;; .equiv is not a standard thing, primarily for interop
25+
;; in transit-js, probably not a concern for lite-mode users
26+
(when-not LITE_MODE
27+
(is (.equiv #{:cat :dog :bird} #{:cat :dog :bird})))))
2328

2429
(deftest test-es6-interfaces
2530
(testing "ES6 collection interfaces"
@@ -67,4 +72,4 @@
6772
(is (#{:cat :bird :dog} (.-value (.next iter))))
6873
(is (#{:cat :bird :dog} (.-value (.next iter))))
6974
(is (.-done (.next iter)))))
70-
))
75+
))

src/test/cljs/lite_test_runner.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
'cljs.new-new-test
8080
'cljs.seqs-test
8181
#_'cljs.hashing-test
82-
#_'cljs.interop-test ;; ES6 stuff
82+
'cljs.interop-test ;; ES6 stuff
8383
#_'cljs.iterator-test
8484
'cljs.reader-test
8585
'cljs.binding-test

0 commit comments

Comments
 (0)