Permalink
Browse files

bring back toString on the core collections, include a new lower level

`pr-str*` which doesn't bring in all the other printing machinery
  • Loading branch information...
1 parent 4914421 commit 29c8597a2c818fb8fee6fb6d725e450ccffe9b71 @swannodette swannodette committed May 8, 2013
Showing with 116 additions and 16 deletions.
  1. +116 −16 src/cljs/cljs/core.cljs
@@ -27,6 +27,17 @@
"Set *print-fn* to f."
[f] (set! *print-fn* f))
+(def ^:dynamic *flush-on-newline* true)
+(def ^:dynamic *print-readably* true)
+(def ^:dynamic *print-meta* false)
+(def ^:dynamic *print-dup* false)
+
+(defn- pr-opts []
+ {:flush-on-newline *flush-on-newline*
+ :readably *print-readably*
+ :meta *print-meta*
+ :dup *print-dup*})
+
(def
^{:doc "bound in a repl thread to the most recent value printed"}
*1)
@@ -331,6 +342,23 @@
(-name [x])
(-namespace [x]))
+;; Printing support
+
+(deftype StringBufferWriter [sb]
+ IWriter
+ (-write [_ s] (.append sb s))
+ (-flush [_] nil))
+
+(defn pr-str*
+ "Support so that collections can implement toString without
+ loading all the printing machinery."
+ [^not-native obj]
+ (let [sb (gstring/StringBuffer.)
+ writer (StringBufferWriter. sb)]
+ (-pr-writer obj writer (pr-opts))
+ (-flush writer)
+ (str sb)))
+
;;;;;;;;;;;;;;;;;;; symbols ;;;;;;;;;;;;;;;
(declare list hash-combine hash Symbol)
@@ -621,6 +649,10 @@ reduces them without incurring seq initialization"
[x] (satisfies? IIndexed x))
(deftype IndexedSeq [arr i]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
ISeqable
(-seq [this] this)
@@ -696,6 +728,10 @@ reduces them without incurring seq initialization"
(declare with-meta)
(deftype RSeq [ci i meta]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IMeta
(-meta [coll] meta)
IWithMeta
@@ -1783,6 +1819,10 @@ reduces them without incurring seq initialization"
;;;;;;;;;;;;;;;; cons ;;;;;;;;;;;;;;;;
(deftype List [meta first rest count ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IList
IWithMeta
@@ -1829,6 +1869,10 @@ reduces them without incurring seq initialization"
(-count [coll] count))
(deftype EmptyList [meta]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IList
IWithMeta
@@ -1898,6 +1942,10 @@ reduces them without incurring seq initialization"
r))))
(deftype Cons [meta first rest ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IList
IWithMeta
@@ -1990,6 +2038,10 @@ reduces them without incurring seq initialization"
(.-x lazy-seq)))))
(deftype LazySeq [meta realized x ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IWithMeta
(-with-meta [coll meta] (LazySeq. meta realized x __hash))
@@ -2072,6 +2124,10 @@ reduces them without incurring seq initialization"
(ArrayChunk. arr off end)))
(deftype ChunkedCons [chunk more meta ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IWithMeta
(-with-meta [coll m]
(ChunkedCons. chunk more m __hash))
@@ -3059,6 +3115,10 @@ reduces them without incurring seq initialization"
pr-sequential-writer pr-writer chunked-seq)
(deftype PersistentVector [meta cnt shift root tail ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IWithMeta
(-with-meta [coll meta] (PersistentVector. meta cnt shift root tail __hash))
@@ -3222,6 +3282,10 @@ reduces them without incurring seq initialization"
(defn vector [& args] (vec args))
(deftype ChunkedSeq [vec node i off meta ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IWithMeta
(-with-meta [coll m]
(chunked-seq vec node i off m))
@@ -3294,6 +3358,10 @@ reduces them without incurring seq initialization"
(declare build-subvec)
(deftype Subvec [meta v start end ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IWithMeta
(-with-meta [coll meta] (build-subvec meta v start end __hash))
@@ -3574,6 +3642,10 @@ reduces them without incurring seq initialization"
;;; PersistentQueue ;;;
(deftype PersistentQueueSeq [meta front rear ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IWithMeta
(-with-meta [coll meta] (PersistentQueueSeq. meta front rear __hash))
@@ -3606,6 +3678,10 @@ reduces them without incurring seq initialization"
(-seq [coll] coll))
(deftype PersistentQueue [meta count front rear ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IWithMeta
(-with-meta [coll meta] (PersistentQueue. meta count front rear __hash))
@@ -3719,6 +3795,10 @@ reduces them without incurring seq initialization"
new-obj))
(deftype ObjMap [meta keys strobj update-count ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IWithMeta
(-with-meta [coll meta] (ObjMap. meta keys strobj update-count __hash))
@@ -3887,6 +3967,10 @@ reduces them without incurring seq initialization"
(declare TransientArrayMap)
(deftype PersistentArrayMap [meta cnt arr ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IWithMeta
(-with-meta [coll meta] (PersistentArrayMap. meta cnt arr __hash))
@@ -4583,6 +4667,10 @@ reduces them without incurring seq initialization"
(.inode-assoc! edit shift key2hash key2 val2 added-leaf?)))))))
(deftype NodeSeq [meta nodes i s ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IMeta
(-meta [coll] meta)
@@ -4640,6 +4728,10 @@ reduces them without incurring seq initialization"
(NodeSeq. nil nodes i s nil))))
(deftype ArrayNodeSeq [meta nodes i s ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IMeta
(-meta [coll] meta)
@@ -4689,6 +4781,10 @@ reduces them without incurring seq initialization"
(declare TransientHashMap)
(deftype PersistentHashMap [meta cnt root ^boolean has-nil? nil-val ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IWithMeta
(-with-meta [coll meta] (PersistentHashMap. meta cnt root has-nil? nil-val __hash))
@@ -4907,6 +5003,10 @@ reduces them without incurring seq initialization"
stack)))
(deftype PersistentTreeMapSeq [meta stack ^boolean ascending? cnt ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
ISeqable
(-seq [this] this)
@@ -5405,6 +5505,10 @@ reduces them without incurring seq initialization"
(deftype PersistentTreeMap [comp tree cnt meta ^:mutable __hash]
Object
+ (toString [coll]
+ (pr-str* coll))
+
+ Object
(entry-at [coll k]
(loop [t tree]
(if-not (nil? t)
@@ -5633,6 +5737,10 @@ reduces them without incurring seq initialization"
(declare TransientHashSet)
(deftype PersistentHashSet [meta hash-map ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IWithMeta
(-with-meta [coll meta] (PersistentHashSet. meta hash-map __hash))
@@ -5738,6 +5846,10 @@ reduces them without incurring seq initialization"
k)))
(deftype PersistentTreeSet [meta tree-map ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IWithMeta
(-with-meta [coll meta] (PersistentTreeSet. meta tree-map __hash))
@@ -5985,6 +6097,10 @@ reduces them without incurring seq initialization"
(if ((mk-bound-fn sc end-test end-key) e) s (next s))))))
(deftype Range [meta start end step ^:mutable __hash]
+ Object
+ (toString [coll]
+ (pr-str* coll))
+
IWithMeta
(-with-meta [rng meta] (Range. meta start end step __hash))
@@ -6249,11 +6365,6 @@ reduces them without incurring seq initialization"
(fn [match] (aget char-escapes match)))
\"))
-(deftype StringBufferWriter [sb]
- IWriter
- (-write [_ s] (.append sb s))
- (-flush [_] nil))
-
(defn- pr-writer
"Prefer this to pr-seq, because it makes the printing function
configurable, allowing efficient implementations such as appending
@@ -6368,17 +6479,6 @@ reduces them without incurring seq initialization"
(when (get opts :flush-on-newline)
(flush)))
-(def *flush-on-newline* true)
-(def *print-readably* true)
-(def *print-meta* false)
-(def *print-dup* false)
-
-(defn- pr-opts []
- {:flush-on-newline *flush-on-newline*
- :readably *print-readably*
- :meta *print-meta*
- :dup *print-dup*})
-
(defn pr-str
"pr to a string, returning it. Fundamental entrypoint to IPrintWithWriter."
[& objs]

0 comments on commit 29c8597

Please sign in to comment.