Fix #1456: allow dyn vars to be set in socket REPL (#1461)

This commit is contained in:
Michiel Borkent 2023-01-04 21:23:29 +01:00 committed by GitHub
parent 3aca505790
commit b9308eddcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 40 deletions

View file

@ -9,7 +9,7 @@ A preview of the next release can be installed from
## Unreleased ## Unreleased
... - [#1456](https://github.com/babashka/babashka/issues/1456): allow `*warn-on-reflection*` and `*unchecked-math*` to be set in socket REPL and nREPL ([@axks](https://github.com/axks))
## 1.0.169 (2023-01-03) ## 1.0.169 (2023-01-03)

View file

@ -2,6 +2,7 @@
{:no-doc true} {:no-doc true}
(:require (:require
[babashka.impl.clojure.main :as m] [babashka.impl.clojure.main :as m]
[babashka.impl.clojure.core :as core-extras]
[clojure.java.io :as io] [clojure.java.io :as io]
[clojure.string :as str] [clojure.string :as str]
[clojure.tools.reader.reader-types :as r] [clojure.tools.reader.reader-types :as r]
@ -50,41 +51,43 @@
([sci-ctx] (repl sci-ctx nil)) ([sci-ctx] (repl sci-ctx nil))
([sci-ctx {:keys [:init :read :eval :need-prompt :prompt :flush :print :caught]}] ([sci-ctx {:keys [:init :read :eval :need-prompt :prompt :flush :print :caught]}]
(let [in @sci/in] (let [in @sci/in]
(m/repl (sci/binding [core-extras/warn-on-reflection @core-extras/warn-on-reflection
:init (or init core-extras/unchecked-math @core-extras/unchecked-math]
(fn [] (m/repl
(sci/with-bindings {sci/out @sci/err} :init (or init
(sio/println "Babashka" (fn []
(str "v" (str/trim (slurp (io/resource "BABASHKA_VERSION")))) (sci/with-bindings {sci/out @sci/err}
"REPL.") (sio/println "Babashka"
(sio/println "Use :repl/quit or :repl/exit to quit the REPL.") (str "v" (str/trim (slurp (io/resource "BABASHKA_VERSION"))))
(sio/println "Clojure rocks, Bash reaches.") "REPL.")
(sio/println)) (sio/println "Use :repl/quit or :repl/exit to quit the REPL.")
(eval-form sci-ctx `(apply require (quote ~m/repl-requires))))) (sio/println "Clojure rocks, Bash reaches.")
:read (or read (sio/println))
(fn [_request-prompt request-exit] (eval-form sci-ctx `(apply require (quote ~m/repl-requires)))))
(if (nil? (r/peek-char in)) :read (or read
request-exit (fn [_request-prompt request-exit]
(let [v (parser/parse-next sci-ctx in)] (if (nil? (r/peek-char in))
(skip-if-eol in) request-exit
(if (or (identical? :repl/quit v) (let [v (parser/parse-next sci-ctx in)]
(identical? :repl/exit v)) (skip-if-eol in)
request-exit (if (or (identical? :repl/quit v)
v))))) (identical? :repl/exit v))
:eval (or eval request-exit
(fn [expr] v)))))
(sci/with-bindings {sci/file "<repl>" :eval (or eval
sci/*1 *1 (fn [expr]
sci/*2 *2 (sci/with-bindings {sci/file "<repl>"
sci/*3 *3 sci/*1 *1
sci/*e *e} sci/*2 *2
(let [ret (eval-form sci-ctx expr)] sci/*3 *3
ret)))) sci/*e *e}
:need-prompt (or need-prompt (fn [] true)) (let [ret (eval-form sci-ctx expr)]
:prompt (or prompt #(sio/printf "%s=> " (utils/current-ns-name))) ret))))
:flush (or flush sio/flush) :need-prompt (or need-prompt (fn [] true))
:print (or print sio/prn) :prompt (or prompt #(sio/printf "%s=> " (utils/current-ns-name)))
:caught (or caught repl-caught))))) :flush (or flush sio/flush)
:print (or print sio/prn)
:caught (or caught repl-caught))))))
(defn start-repl! (defn start-repl!
([sci-ctx] (start-repl! sci-ctx nil)) ([sci-ctx] (start-repl! sci-ctx nil))

View file

@ -1,8 +1,8 @@
(ns babashka.impl.socket-repl-test (ns babashka.impl.socket-repl-test
(:require (:require
[babashka.impl.common :as common]
[babashka.impl.server :refer [clojure-core-server-namespace]] [babashka.impl.server :refer [clojure-core-server-namespace]]
[babashka.impl.socket-repl :refer [start-repl! stop-repl!]] [babashka.impl.socket-repl :refer [start-repl! stop-repl!]]
[babashka.main :as main]
[babashka.process :as p] [babashka.process :as p]
[babashka.test-utils :as tu] [babashka.test-utils :as tu]
[babashka.wait :as w] [babashka.wait :as w]
@ -15,7 +15,7 @@
(set! *warn-on-reflection* true) (set! *warn-on-reflection* true)
(defn socket-command [expr expected] (defn socket-command [expr expected & [log?]]
(with-open [socket (java.net.Socket. "127.0.0.1" 1666) (with-open [socket (java.net.Socket. "127.0.0.1" 1666)
reader (io/reader socket) reader (io/reader socket)
sw (java.io.StringWriter.) sw (java.io.StringWriter.)
@ -25,7 +25,8 @@
(loop [] (loop []
(when-let [l (try (.readLine ^java.io.BufferedReader reader) (when-let [l (try (.readLine ^java.io.BufferedReader reader)
(catch java.net.SocketException _ nil))] (catch java.net.SocketException _ nil))]
;; (prn :l l) (when log?
(println "===" l))
(binding [*out* sw] (binding [*out* sw]
(println l)) (println l))
(let [s (str sw)] (let [s (str sw)]
@ -47,7 +48,7 @@
(when exec? (when exec?
(try (try
(if tu/jvm? (if tu/jvm?
(let [ctx (init {:namespaces {'clojure.core.server clojure-core-server-namespace} (let [ctx (init {:namespaces main/namespaces
:features #{:bb}})] :features #{:bb}})]
(ctx-store/reset-ctx! ctx) (ctx-store/reset-ctx! ctx)
(start-repl! "0.0.0.0:1666" ctx)) (start-repl! "0.0.0.0:1666" ctx))
@ -66,6 +67,8 @@
(is (socket-command "1\n*1" "1"))) (is (socket-command "1\n*1" "1")))
(testing "*ns*" (testing "*ns*"
(is (socket-command "(ns foo.bar) (ns-name *ns*)" "foo.bar"))) (is (socket-command "(ns foo.bar) (ns-name *ns*)" "foo.bar")))
(testing "set dyn vars"
(is (socket-command "[(set! *warn-on-reflection* true) (set! *unchecked-math* true)]" "[true true]")))
(finally (finally
(if tu/jvm? (if tu/jvm?
(do (stop-repl!) (do (stop-repl!)