[#899] (read-line) is buggy in REPL

This commit is contained in:
Michiel Borkent 2021-06-20 13:32:33 +02:00
parent 13773d0104
commit af754d013f
3 changed files with 13 additions and 1 deletions

View file

@ -17,6 +17,8 @@
babashka.impl.clojure.main
(:refer-clojure :exclude [with-bindings]))
(set! *warn-on-reflection* true)
(defn demunge
"Given a string representation of a fn class,
as in a stack trace element, returns a readable version."

View file

@ -38,11 +38,18 @@
(str ":" line ":" column))"]"))))
(sio/flush))))
(defn skip-if-eol
"Inspired by skip-if-eol from clojure.main."
[s]
(let [c (r/read-char s)]
(when-not (= c \newline)
(r/unread s c))))
(defn repl
"REPL with predefined hooks for attachable socket server."
([sci-ctx] (repl sci-ctx nil))
([sci-ctx {:keys [:init :read :eval :need-prompt :prompt :flush :print :caught]}]
(let [in (r/indexing-push-back-reader (r/push-back-reader @sci/in))]
(let [in @sci/in #_(r/indexing-push-back-reader (r/push-back-reader @sci/in))]
(m/repl
:init (or init
(fn []
@ -57,6 +64,7 @@
:read (or read
(fn [_request-prompt request-exit]
(let [v (parser/parse-next sci-ctx in)]
(skip-if-eol in)
(if (or (identical? :repl/quit v)
(identical? :repl/exit v)
(identical? parser/eof v))

View file

@ -47,6 +47,8 @@
(assert-repl "1\n(dec *1)(+ *2 *2)" "2")
(assert-repl "1\n(dec *1)(+ *2 *2)" "2")
(assert-repl "*command-line-args*" "[\"a\" \"b\" \"c\"]")
(assert-repl "(read-line)hello" "hello")
(assert-repl "(read-line)\nhello" "hello")
(assert-repl-error "(+ 1 nil)" "NullPointerException")
(assert-repl-error "(/ 1 0) (pst 1)" "Divide by zero\n\tclojure.lang.Numbers"))