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
...
- [#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)

View file

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

View file

@ -1,8 +1,8 @@
(ns babashka.impl.socket-repl-test
(:require
[babashka.impl.common :as common]
[babashka.impl.server :refer [clojure-core-server-namespace]]
[babashka.impl.socket-repl :refer [start-repl! stop-repl!]]
[babashka.main :as main]
[babashka.process :as p]
[babashka.test-utils :as tu]
[babashka.wait :as w]
@ -15,7 +15,7 @@
(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)
reader (io/reader socket)
sw (java.io.StringWriter.)
@ -25,7 +25,8 @@
(loop []
(when-let [l (try (.readLine ^java.io.BufferedReader reader)
(catch java.net.SocketException _ nil))]
;; (prn :l l)
(when log?
(println "===" l))
(binding [*out* sw]
(println l))
(let [s (str sw)]
@ -47,7 +48,7 @@
(when exec?
(try
(if tu/jvm?
(let [ctx (init {:namespaces {'clojure.core.server clojure-core-server-namespace}
(let [ctx (init {:namespaces main/namespaces
:features #{:bb}})]
(ctx-store/reset-ctx! ctx)
(start-repl! "0.0.0.0:1666" ctx))
@ -66,6 +67,8 @@
(is (socket-command "1\n*1" "1")))
(testing "*ns*"
(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
(if tu/jvm?
(do (stop-repl!)