Skip to content

Commit 078d59d

Browse files
authored
Use primitives in print-map (#258)
* lower print-map - lift pr-map-entry-helper, implement ISeqable - lift-ns uses array of MapEntry instead of actual map
1 parent e611bd0 commit 078d59d

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10520,6 +10520,14 @@ reduces them without incurring seq initialization"
1052010520
(implements? IMeta obj)
1052110521
(not (nil? (meta obj)))))
1052210522

10523+
(defn- pr-map-entry [k v]
10524+
(reify
10525+
IMapEntry
10526+
(-key [_] k)
10527+
(-val [_] v)
10528+
ISeqable
10529+
(-seq [_] (IndexedSeq. #js [k v] 0 nil))))
10530+
1052310531
(defn- pr-writer-impl
1052410532
[obj writer opts]
1052510533
(cond
@@ -10557,12 +10565,9 @@ reduces them without incurring seq initialization"
1055710565
(.map
1055810566
(js-keys obj)
1055910567
(fn [k]
10560-
(reify
10561-
IMapEntry
10562-
(-key [_]
10563-
(cond-> k (some? (.match k #"^[A-Za-z_\*\+\?!\-'][\w\*\+\?!\-']*$")) keyword))
10564-
(-val [_]
10565-
(unchecked-get obj k)))))
10568+
(pr-map-entry
10569+
(cond-> k (some? (.match k #"^[A-Za-z_\*\+\?!\-'][\w\*\+\?!\-']*$")) keyword)
10570+
(unchecked-get obj k))))
1056610571
pr-writer writer opts))
1056710572

1056810573
(array? obj)
@@ -10731,20 +10736,22 @@ reduces them without incurring seq initialization"
1073110736
(keyword nil (name named))))
1073210737

1073310738
(defn- lift-ns
10734-
"Returns [lifted-ns lifted-map] or nil if m can't be lifted."
10739+
"Returns #js [lifted-ns lifted-map] or nil if m can't be lifted."
1073510740
[m]
1073610741
(when *print-namespace-maps*
10737-
(loop [ns nil
10738-
[[k v :as entry] & entries] (seq m)
10739-
lm (empty m)]
10740-
(if entry
10741-
(when (or (keyword? k) (symbol? k))
10742-
(if ns
10743-
(when (= ns (namespace k))
10744-
(recur ns entries (assoc lm (strip-ns k) v)))
10745-
(when-let [new-ns (namespace k)]
10746-
(recur new-ns entries (assoc lm (strip-ns k) v)))))
10747-
[ns lm]))))
10742+
(let [lm #js []]
10743+
(loop [ns nil
10744+
[[k v :as entry] & entries] (seq m)]
10745+
(if entry
10746+
(when (or (keyword? k) (symbol? k))
10747+
(if ns
10748+
(when (= ns (namespace k))
10749+
(.push lm (pr-map-entry (strip-ns k) v))
10750+
(recur ns entries))
10751+
(when-let [new-ns (namespace k)]
10752+
(.push lm (pr-map-entry (strip-ns k) v))
10753+
(recur new-ns entries))))
10754+
#js [ns lm])))))
1074810755

1074910756
(defn print-prefix-map [prefix m print-one writer opts]
1075010757
(pr-sequential-writer
@@ -10757,10 +10764,11 @@ reduces them without incurring seq initialization"
1075710764
opts (seq m)))
1075810765

1075910766
(defn print-map [m print-one writer opts]
10760-
(let [[ns lift-map] (when (map? m)
10761-
(lift-ns m))]
10767+
(let [ns&lift-map (when (map? m)
10768+
(lift-ns m))
10769+
ns (some-> ns&lift-map (aget 0))]
1076210770
(if ns
10763-
(print-prefix-map (str "#:" ns) lift-map print-one writer opts)
10771+
(print-prefix-map (str "#:" ns) (aget ns&lift-map 1) print-one writer opts)
1076410772
(print-prefix-map nil m print-one writer opts))))
1076510773

1076610774
(extend-protocol IPrintWithWriter

0 commit comments

Comments
 (0)