Skip to content

Commit f410593

Browse files
committed
Expose clojure.core/pr-on
1 parent 82a4297 commit f410593

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/sci/impl/io.cljc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@
8282
(print-method x w))
8383
nil))
8484

85+
#?(:clj (defn core-pr-on
86+
"Prints x to w under the interpreter's print settings. Exposed as the
87+
private clojure.core/pr-on, which host code reaches through the var. The
88+
host's own pr-on binds nothing, because there these settings are already
89+
the ones in effect."
90+
[x w]
91+
(binding [*print-length* @print-length
92+
*print-level* @print-level
93+
*print-meta* @print-meta
94+
*print-namespace-maps* @print-namespace-maps
95+
*print-readably* @print-readably
96+
*print-dup* @print-dup-var]
97+
(pr-on x w))))
98+
8599
#?(:cljd (defn pr
86100
[& objs]
87101
(binding [*out* @out

src/sci/impl/namespaces.cljc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,7 @@
13661366
'newline (copy-var sci.impl.io/newline clojure-core-ns {:name 'newline})
13671367
'flush (copy-core-var sci.impl.io/flush)
13681368
'pr (copy-var sci.impl.io/pr clojure-core-ns {:name 'pr})
1369+
#?@(:clj ['pr-on (new-var 'pr-on sci.impl.io/core-pr-on clojure-core-ns {:private true})])
13691370
'prn (copy-core-var sci.impl.io/prn)
13701371
'print (copy-core-var sci.impl.io/print)
13711372
'println (copy-core-var sci.impl.io/println)

test/sci/io_test.cljc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(:require
33
[clojure.edn :as edn]
44
[clojure.string :as str]
5-
[clojure.test :as test :refer [deftest is]]
5+
[clojure.test :as test :refer [deftest is testing]]
66
[sci.core :as sci]
77
[sci.test-utils :as tu]))
88

@@ -37,6 +37,20 @@
3737
(is (= "\"hello\"" (sci/with-out-str (eval* "(pr \"hello\")"))))
3838
(is (= "\n" (sci/with-out-str (eval* "(newline)"))))))
3939

40+
#?(:cljd nil
41+
:clj
42+
(deftest pr-on-test
43+
(when-not tu/native?
44+
(testing "clojure.core/pr-on prints to the writer it is given"
45+
(let [w (java.io.StringWriter.)]
46+
(tu/eval* "(@#'clojure.core/pr-on (range 5) w)" {:namespaces {'user {'w w}}})
47+
(is (= "(0 1 2 3 4)" (str w)))))
48+
(testing "and under the interpreter's print settings"
49+
(let [w (java.io.StringWriter.)]
50+
(tu/eval* "(binding [*print-length* 3] (@#'clojure.core/pr-on (range 10) w))"
51+
{:namespaces {'user {'w w}}})
52+
(is (= "(0 1 2 ...)" (str w))))))))
53+
4054
(deftest print-length-test
4155
(when-not tu/native?
4256
(is (str/includes?

0 commit comments

Comments
 (0)