Skip to content

Commit 53201d8

Browse files
committed
restore reader, add EdnReader, read-edn, read-edn-string,docs
1 parent e060967 commit 53201d8

6 files changed

Lines changed: 857 additions & 212 deletions

File tree

src/clj/clojure/core.clj

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3391,11 +3391,10 @@
33913391
java.io.PushbackReader or some derivee. stream defaults to the
33923392
current value of *in*.
33933393
3394-
In the :default mode, governed by *read-eval*, reading of class objects,
3395-
vars, namespaces are allowed, as well as static methods and
3396-
constructors of derivees of the *read-whitelist*
3394+
Note that read can execute code (controlled by *read-eval*),
3395+
and as such should be used only with trusted sources.
33973396
3398-
See also - safe-read"
3397+
For data structure interop use read-edn"
33993398
{:added "1.0"
34003399
:static true}
34013400
([]
@@ -3405,14 +3404,24 @@
34053404
([stream eof-error? eof-value]
34063405
(read stream eof-error? eof-value false))
34073406
([stream eof-error? eof-value recursive?]
3408-
(. clojure.lang.LispReader (read stream (boolean eof-error?) eof-value recursive?))))
3407+
(. clojure.lang.LispReader (read stream (boolean eof-error?) eof-value recursive?))))
34093408

3410-
(defn safe-read
3411-
"Same as read, with *read-eval* bound to false"
3409+
(defn read-edn
3410+
"Reads the next object from stream, which must be an instance of
3411+
java.io.PushbackReader or some derivee. stream defaults to the
3412+
current value of *in*.
3413+
3414+
Reads data in the edn format (subset of Clojure data):
3415+
http://edn-format.org"
34123416
{:added "1.5"}
3413-
[& args]
3414-
(binding [*read-eval* false]
3415-
(apply read args)))
3417+
([]
3418+
(read *in*))
3419+
([stream]
3420+
(read stream true nil))
3421+
([stream eof-error? eof-value]
3422+
(read stream eof-error? eof-value false))
3423+
([stream eof-error? eof-value recursive?]
3424+
(. clojure.lang.EdnReader (read stream (boolean eof-error?) eof-value recursive?))))
34163425

34173426
(defn read-line
34183427
"Reads the next line from stream that is the current value of *in* ."
@@ -3426,23 +3435,21 @@
34263435
(defn read-string
34273436
"Reads one object from the string s.
34283437
3429-
In the :default mode, governed by *read-eval*, reading of class objects,
3430-
vars, namespaces are allowed, as well as static methods and
3431-
constructors of derivees of the *read-whitelist*
3432-
3433-
Returns nil when string is nil or empty.
3438+
Note that read-string can execute code (controlled by *read-eval*),
3439+
and as such should be used only with trusted sources.
34343440
3435-
See also - safe-read-string"
3441+
For data structure interop use read-edn-string"
34363442
{:added "1.0"
34373443
:static true}
3438-
[^String s] (when (pos? (count s)) (clojure.lang.RT/readString s)))
3444+
[s] (clojure.lang.RT/readString s))
3445+
3446+
(defn read-edn-string
3447+
"Reads one object from the string s. Returns nil when s is nil or empty.
34393448
3440-
(defn safe-read-string
3441-
"Same as read-string, with *read-eval* bound to false"
3449+
Reads data in the edn format (subset of Clojure data):
3450+
http://edn-format.org"
34423451
{:added "1.5"}
3443-
[s]
3444-
(binding [*read-eval* false]
3445-
(read-string s)))
3452+
[s] (when (pos? (count s)) (clojure.lang.EdnReader/readString s)))
34463453

34473454
(defn subvec
34483455
"Returns a persistent vector of the items in vector from
@@ -5899,29 +5906,13 @@
58995906
{:added "1.0"})
59005907

59015908
(add-doc-and-meta *read-eval*
5902-
"When set to logical false in the thread-local binding, #= reading is disabled.
5903-
Example:
5904-
(binding [*read-eval* false] (read-string \"#=(not allowed)\"))
5905-
5906-
Defaults to :default
5909+
"When set to logical false in the thread-local binding,
5910+
the eval reader (#=(...)) is disabled in read/load.
5911+
Example: (binding [*read-eval* false] (read-string \"#=(eval (def x 3))\"))
59075912
5908-
In :default mode, reading of class objects, vars, namespaces are allowed, as well as
5909-
static methods and constructors of derivees of the *read-whitelist*.
5910-
5911-
When set to true in the thread-local binding, the eval
5912-
reader (#=(...)) for arbitrary expressions is enabled in read/load.
5913-
"
5913+
Defaults to true"
59145914
{:added "1.0"})
59155915

5916-
(add-doc-and-meta *read-whitelist*
5917-
"In the reader's :default *read-eval* mode,
5918-
reading of static methods and constructors of derivees of the
5919-
*read-whitelist* is alowed.
5920-
5921-
Defaults to [java.util.Map, java.util.Collection, java.lang.Number]"
5922-
5923-
{:added "1.5"})
5924-
59255916
(defn future?
59265917
"Returns true if x is a future"
59275918
{:added "1.1"

src/clj/clojure/core_print.clj

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -309,23 +309,31 @@
309309
(defmethod print-dup clojure.lang.LazilyPersistentVector [o w] (print-method o w))
310310

311311
(def primitives-classnames
312-
{Float/TYPE "float"
313-
Integer/TYPE "int"
314-
Long/TYPE "long"
315-
Boolean/TYPE "boolean"
316-
Character/TYPE "char"
317-
Double/TYPE "double"
318-
Byte/TYPE "byte"
319-
Short/TYPE "short"})
312+
{Float/TYPE "Float/TYPE"
313+
Integer/TYPE "Integer/TYPE"
314+
Long/TYPE "Long/TYPE"
315+
Boolean/TYPE "Boolean/TYPE"
316+
Character/TYPE "Character/TYPE"
317+
Double/TYPE "Double/TYPE"
318+
Byte/TYPE "Byte/TYPE"
319+
Short/TYPE "Short/TYPE"})
320320

321321
(defmethod print-method Class [^Class c, ^Writer w]
322322
(.write w (.getName c)))
323323

324324
(defmethod print-dup Class [^Class c, ^Writer w]
325-
(let [^String cs (or (primitives-classnames c) (.getName c))]
326-
(.write w "#=\"")
327-
(.write w cs)
328-
(.write w "\"")))
325+
(cond
326+
(.isPrimitive c) (do
327+
(.write w "#=(identity ")
328+
(.write w ^String (primitives-classnames c))
329+
(.write w ")"))
330+
(.isArray c) (do
331+
(.write w "#=(java.lang.Class/forName \"")
332+
(.write w (.getName c))
333+
(.write w "\")"))
334+
:else (do
335+
(.write w "#=")
336+
(.write w (.getName c)))))
329337

330338
(defmethod print-method java.math.BigDecimal [b, ^Writer w]
331339
(.write w (str b))

src/jvm/clojure/lang/Compiler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7043,7 +7043,6 @@ public static Object load(Reader rdr, String sourcePath, String sourceName) {
70437043
LOCAL_ENV, null,
70447044
LOOP_LOCALS, null,
70457045
NEXT_LOCAL_NUM, 0,
7046-
RT.READEVAL, RT.T,
70477046
RT.CURRENT_NS, RT.CURRENT_NS.deref(),
70487047
LINE_BEFORE, pushbackReader.getLineNumber(),
70497048
COLUMN_BEFORE, pushbackReader.getColumnNumber(),

0 commit comments

Comments
 (0)