[#81] fix socket repl issue with &env

This commit is contained in:
Michiel Borkent 2019-10-20 18:21:57 +02:00 committed by GitHub
parent 945b7685f1
commit 85aeaa354b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 11 deletions

2
sci

@ -1 +1 @@
Subproject commit 4fa8813270910d71a5fd7d590962fa1babbc19db
Subproject commit dcfea77c3172c78a6cb053ff51a6faae154b34dc

View file

@ -6,14 +6,14 @@
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.tools.reader.reader-types :as r]
[sci.core :refer [eval-string]]
[sci.impl.interpreter :refer [opts->ctx eval-edn-vals]]
[sci.impl.parser :as parser]))
(set! *warn-on-reflection* true)
(defn repl
"REPL with predefined hooks for attachable socket server."
[sci-opts]
[sci-ctx]
(let [in (r/indexing-push-back-reader (r/push-back-reader *in*))]
(m/repl
:init #(do (println "Babashka"
@ -32,13 +32,13 @@
v))
request-exit))
:eval (fn [expr]
(let [ret (eval-string (pr-str expr)
(update sci-opts
:bindings
merge {'*1 *1
'*2 *2
'*3 *3
'*e *e}))]
(let [ret (eval-edn-vals (update-in sci-ctx
[:namespaces 'clojure.core]
merge {'*1 *1
'*2 *2
'*3 *3
'*e *e})
[expr])]
ret))
:need-prompt (fn [] true))))
@ -49,12 +49,13 @@
[(first parts) (Integer. ^String (second parts))])
host+port (if-not host (str "localhost:" port)
host+port)
sci-ctx (opts->ctx sci-opts)
socket (server/start-server
{:address host
:port port
:name "bb"
:accept babashka.impl.socket-repl/repl
:args [sci-opts]})]
:args [sci-ctx]})]
(println "Babashka socket REPL started at" host+port)
socket))

View file

@ -48,6 +48,11 @@
(testing "*command-line-args*"
(is (str/includes? (socket-command '*command-line-args*)
"\"a\" \"b\" \"c\"")))
(testing "&env"
(socket-command '(defmacro bindings [] (mapv #(list 'quote %) (keys &env))))
(socket-command '(defn bar [x y z] (bindings)))
(is (str/includes? (socket-command '(bar 1 2 3))
"[x y z]")))
(finally
(if tu/jvm?
(stop-repl!)