From b68339dc462f5c5880ae36adc9f04361dd693141 Mon Sep 17 00:00:00 2001 From: Bob Date: Wed, 29 May 2024 22:44:31 -0400 Subject: [PATCH] add `clojure.core.server/repl-read` --- CHANGELOG.md | 3 +++ src/babashka/impl/repl.clj | 21 ++++++++++++--------- src/babashka/impl/server.clj | 8 +++++++- test/babashka/impl/server_test.clj | 20 ++++++++++++++++++++ 4 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 test/babashka/impl/server_test.clj diff --git a/CHANGELOG.md b/CHANGELOG.md index 951f4142..585ef02b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ A preview of the next release can be installed from - Partially Fix [#1695](https://github.com/babashka/babashka/issues/1695): `--repl` arg handling should consume only one arg (itself) ([@bobisageek](https://github.com/bobisageek)) - Partially Fix [#1695](https://github.com/babashka/babashka/issues/1695): make `*command-line-args*` value available in the REPL ([@bobisageek](https://github.com/bobisageek)) - Fix [#1686](https://github.com/babashka/babashka/issues/1686): do not fetch dependencies/invoke java for `version`, `help`, and `describe` options ([@bobisageek](https://github.com/bobisageek)) +- [#1696](https://github.com/babashka/babashka/issues/1696): add `clojure.core/*source-path*` (unused) ([@bobisageek](https://github.com/bobisageek)) +- [#1696](https://github.com/babashka/babashka/issues/1696): add `clojure.main/with-read-known` ([@bobisageek](https://github.com/bobisageek)) +- [#1696](https://github.com/babashka/babashka/issues/1696): add `clojure.core.server/repl-read` ([@bobisageek](https://github.com/bobisageek)) ## 1.3.190 (2024-04-17) diff --git a/src/babashka/impl/repl.clj b/src/babashka/impl/repl.clj index 86fd58e6..289ba1ec 100644 --- a/src/babashka/impl/repl.clj +++ b/src/babashka/impl/repl.clj @@ -46,6 +46,16 @@ (when-not (= c \newline) (r/unread s c)))) +(defn repl-read [sci-ctx in-stream _request-prompt request-exit] + (if (nil? (r/peek-char in-stream)) + request-exit + (let [v (parser/parse-next sci-ctx in-stream)] + (skip-if-eol in-stream) + (if (or (identical? :repl/quit v) + (identical? :repl/exit v)) + request-exit + v)))) + (defn repl "REPL with predefined hooks for attachable socket server." ([sci-ctx] (repl sci-ctx nil)) @@ -68,15 +78,8 @@ (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))))) + (fn [request-prompt request-exit] + (repl-read sci-ctx in request-prompt request-exit))) :eval (or eval (fn [expr] (sci/with-bindings {sci/file "" diff --git a/src/babashka/impl/server.clj b/src/babashka/impl/server.clj index 78af54c7..a1e9fdd0 100644 --- a/src/babashka/impl/server.clj +++ b/src/babashka/impl/server.clj @@ -1,6 +1,7 @@ (ns babashka.impl.server (:require [babashka.impl.clojure.core.server :as server] [babashka.impl.common :as common] + [babashka.impl.repl :as repl] [babashka.impl.socket-repl :as socket-repl] [sci.core :as sci])) @@ -17,9 +18,14 @@ (fn [& args] (apply server/start-server (common/ctx) args))) +(def repl-read + (fn [& args] + (apply repl/repl-read (common/ctx) @sci/in args))) + (def clojure-core-server-namespace {'repl (sci/copy-var socket-repl/repl sns) 'prepl (sci/copy-var prepl sns) 'io-prepl (sci/copy-var io-prepl sns) 'start-server (sci/copy-var start-server sns) - 'stop-server (sci/copy-var server/stop-server sns)}) + 'stop-server (sci/copy-var server/stop-server sns) + 'repl-read (sci/copy-var repl-read sns)}) diff --git a/test/babashka/impl/server_test.clj b/test/babashka/impl/server_test.clj new file mode 100644 index 00000000..ebd677de --- /dev/null +++ b/test/babashka/impl/server_test.clj @@ -0,0 +1,20 @@ +(ns babashka.impl.server-test + (:require [babashka.test-utils :as tu] + [clojure.edn :as edn] + [clojure.test :as t :refer [deftest is testing]])) + +(def bb + (comp edn/read-string tu/bb)) + +(deftest repl-read-test + (testing "arbitrary values can be read" + (t/are [input result] + (= result (bb input "(let [request-exit (Object.)] + (loop [acc []] + (let [v (clojure.core.server/repl-read nil request-exit)] + (if (= v request-exit) + acc + (recur (conj acc v))))))")) + "abc" '[abc] + "123 456" [123 456] + "(nil ns/symbol (true))\n (+ 1 2 3)" '[(nil ns/symbol (true)) (+ 1 2 3)])))