Skip to content

Commit d2d2d08

Browse files
committed
make Node.js stdout handling mimic eval. Fixes printing of large
values at the REPL.
1 parent ae62411 commit d2d2d08

2 files changed

Lines changed: 28 additions & 28 deletions

File tree

src/main/clojure/cljs/repl/node.clj

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,15 @@
4343
(.write out (int 0)) ;; terminator
4444
(.flush out))
4545

46-
(defn read-response [^BufferedReader in]
46+
(defn ^String read-response [^BufferedReader in]
4747
(let [sb (StringBuilder.)]
4848
(loop [sb sb c (.read in)]
49-
(cond
50-
(= c 1) (let [ret (str sb)]
51-
(print ret)
52-
(recur (StringBuilder.) (.read in)))
53-
(= c 0) (str sb)
54-
:else (do
55-
(.append sb (char c))
56-
(recur sb (.read in)))))))
49+
(case c
50+
-1 (throw (IOException. "Stream closed"))
51+
0 (str sb)
52+
(do
53+
(.append sb (char c))
54+
(recur sb (.read in)))))))
5755

5856
(defn node-eval
5957
"Evaluate a JavaScript string in the Node REPL process."
@@ -93,23 +91,20 @@
9391
(defn- pipe [^Process proc in stream ios]
9492
;; we really do want system-default encoding here
9593
(with-open [^Reader in (-> in InputStreamReader. BufferedReader.)]
96-
(loop [buf (char-array (* 64 1024))]
97-
(when (alive? proc)
98-
(try
99-
(let [len (.read in buf)]
100-
(when-not (neg? len)
101-
(try
102-
(let [{:strs [repl data]} (json/read-str (String. buf))
103-
stream (or (.get ios repl) stream)]
104-
(.write stream data 0 (.length ^String data))
105-
(.flush stream))
106-
(catch Throwable _
107-
(.write stream buf 0 len)
108-
(.flush stream)))))
109-
(catch IOException e
110-
(when (and (alive? proc) (not (.contains (.getMessage e) "Stream closed")))
111-
(.printStackTrace e *err*))))
112-
(recur buf)))))
94+
(while (alive? proc)
95+
(try
96+
(let [res (read-response in)]
97+
(try
98+
(let [{:keys [repl content]} (json/read-str res :key-fn keyword)
99+
stream (or (.get ios repl) stream)]
100+
(.write stream content 0 (.length ^String content))
101+
(.flush stream))
102+
(catch Throwable _
103+
(.write stream res 0 (.length res))
104+
(.flush stream))))
105+
(catch IOException e
106+
(when (and (alive? proc) (not (.contains (.getMessage e) "Stream closed")))
107+
(.printStackTrace e *err*)))))))
113108

114109
(defn- build-process
115110
[opts repl-env input-src]

src/main/clojure/cljs/repl/node_repl.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@ var repl = null;
1818
process.stdout.write = (function(write) {
1919
return function(chunk, encoding, fd) {
2020
var args = Array.prototype.slice.call(arguments, 0);
21-
args[0] = JSON.stringify({repl: repl, data: chunk});
21+
args[0] = JSON.stringify({repl: repl, content: chunk});
2222
write.apply(process.stdout, args);
23+
write.call(process.stdout, "\0");
2324
};
2425
})(process.stdout.write);
2526

2627
process.stderr.write = (function(write) {
2728
return function(chunk, encoding, fd) {
2829
var args = Array.prototype.slice.call(arguments, 0);
29-
args[0] = JSON.stringify({repl: repl, data: chunk});
30+
args[0] = JSON.stringify({repl: repl, content: chunk});
3031
write.apply(process.stderr, args);
32+
write.call(process.stderr, "\0");
3133
};
3234
})(process.stderr.write);
3335

@@ -82,16 +84,19 @@ var server = net.createServer(function (socket) {
8284

8385
if(err) {
8486
socket.write(JSON.stringify({
87+
repl: repl,
8588
status: "exception",
8689
value: err.stack
8790
}));
8891
} else if(ret !== undefined && ret !== null) {
8992
socket.write(JSON.stringify({
93+
repl: repl,
9094
status: "success",
9195
value: ret.toString()
9296
}));
9397
} else {
9498
socket.write(JSON.stringify({
99+
repl: repl,
95100
status: "success",
96101
value: null
97102
}));

0 commit comments

Comments
 (0)