Skip to content

Use primitives in print-map #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions src/main/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10520,6 +10520,14 @@ reduces them without incurring seq initialization"
(implements? IMeta obj)
(not (nil? (meta obj)))))

(defn- pr-map-entry [k v]
(reify
IMapEntry
(-key [_] k)
(-val [_] v)
ISeqable
(-seq [_] (IndexedSeq. #js [k v] 0 nil))))

(defn- pr-writer-impl
[obj writer opts]
(cond
Expand Down Expand Up @@ -10557,12 +10565,9 @@ reduces them without incurring seq initialization"
(.map
(js-keys obj)
(fn [k]
(reify
IMapEntry
(-key [_]
(cond-> k (some? (.match k #"^[A-Za-z_\*\+\?!\-'][\w\*\+\?!\-']*$")) keyword))
(-val [_]
(unchecked-get obj k)))))
(pr-map-entry
(cond-> k (some? (.match k #"^[A-Za-z_\*\+\?!\-'][\w\*\+\?!\-']*$")) keyword)
(unchecked-get obj k))))
pr-writer writer opts))

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

(defn- lift-ns
"Returns [lifted-ns lifted-map] or nil if m can't be lifted."
"Returns #js [lifted-ns lifted-map] or nil if m can't be lifted."
[m]
(when *print-namespace-maps*
(loop [ns nil
[[k v :as entry] & entries] (seq m)
lm (empty m)]
(if entry
(when (or (keyword? k) (symbol? k))
(if ns
(when (= ns (namespace k))
(recur ns entries (assoc lm (strip-ns k) v)))
(when-let [new-ns (namespace k)]
(recur new-ns entries (assoc lm (strip-ns k) v)))))
[ns lm]))))
(let [lm #js []]
(loop [ns nil
[[k v :as entry] & entries] (seq m)]
(if entry
(when (or (keyword? k) (symbol? k))
(if ns
(when (= ns (namespace k))
(.push lm (pr-map-entry (strip-ns k) v))
(recur ns entries))
(when-let [new-ns (namespace k)]
(.push lm (pr-map-entry (strip-ns k) v))
(recur new-ns entries))))
#js [ns lm])))))

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

(defn print-map [m print-one writer opts]
(let [[ns lift-map] (when (map? m)
(lift-ns m))]
(let [ns&lift-map (when (map? m)
(lift-ns m))
ns (some-> ns&lift-map (aget 0))]
(if ns
(print-prefix-map (str "#:" ns) lift-map print-one writer opts)
(print-prefix-map (str "#:" ns) (aget ns&lift-map 1) print-one writer opts)
(print-prefix-map nil m print-one writer opts))))

(extend-protocol IPrintWithWriter
Expand Down
Loading