[#348] nrepl: support multiple top level expressions
This commit is contained in:
parent
56a798135c
commit
e2bdd7eae2
2 changed files with 40 additions and 25 deletions
|
|
@ -3,8 +3,10 @@
|
||||||
(:refer-clojure :exclude [send future binding])
|
(:refer-clojure :exclude [send future binding])
|
||||||
(:require [babashka.impl.bencode.core :refer [write-bencode read-bencode]]
|
(:require [babashka.impl.bencode.core :refer [write-bencode read-bencode]]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
|
[clojure.tools.reader.reader-types :as r]
|
||||||
[sci.core :as sci]
|
[sci.core :as sci]
|
||||||
[sci.impl.interpreter :refer [eval-string*]]
|
[sci.impl.interpreter :refer [eval-string* eval-form]]
|
||||||
|
[sci.impl.parser :as p]
|
||||||
[sci.impl.utils :as sci-utils]
|
[sci.impl.utils :as sci-utils]
|
||||||
[sci.impl.vars :as vars])
|
[sci.impl.vars :as vars])
|
||||||
(:import [java.io StringWriter OutputStream InputStream PushbackInputStream EOFException BufferedOutputStream]
|
(:import [java.io StringWriter OutputStream InputStream PushbackInputStream EOFException BufferedOutputStream]
|
||||||
|
|
@ -38,30 +40,35 @@
|
||||||
|
|
||||||
(defn eval-msg [ctx o msg]
|
(defn eval-msg [ctx o msg]
|
||||||
(try
|
(try
|
||||||
(let [ns-str (get msg :ns)
|
(let [code-str (get msg :code)
|
||||||
sci-ns (when ns-str (sci-utils/namespace-object (:env ctx) (symbol ns-str) true nil))
|
reader (r/indexing-push-back-reader (r/string-push-back-reader code-str))
|
||||||
sw (StringWriter.)]
|
ns-str (get msg :ns)
|
||||||
(sci/with-bindings (cond-> {sci/out sw}
|
sci-ns (when ns-str (sci-utils/namespace-object (:env ctx) (symbol ns-str) true nil))]
|
||||||
|
(when @dev? (println "current ns" (vars/current-ns-name)))
|
||||||
|
(sci/with-bindings (cond-> {}
|
||||||
sci-ns (assoc vars/current-ns sci-ns))
|
sci-ns (assoc vars/current-ns sci-ns))
|
||||||
(when @dev? (println "current ns" (vars/current-ns-name)))
|
(loop []
|
||||||
(let [code-str (get msg :code)
|
(let [sw (StringWriter.)
|
||||||
value (if (str/blank? code-str)
|
form (p/parse-next ctx reader)
|
||||||
::nil
|
value (if (identical? :edamame.impl.parser/eof form) ::nil
|
||||||
(eval-string* ctx code-str))
|
(sci/with-bindings {sci/out sw}
|
||||||
out-str (not-empty (str sw))
|
(eval-form ctx form)))
|
||||||
env (:env ctx)]
|
out-str (not-empty (str sw))
|
||||||
(swap! env update-in [:namespaces 'clojure.core]
|
env (:env ctx)]
|
||||||
(fn [core]
|
(swap! env update-in [:namespaces 'clojure.core]
|
||||||
(assoc core
|
(fn [core]
|
||||||
'*1 value
|
(assoc core
|
||||||
'*2 (get core '*1)
|
'*1 value
|
||||||
'*3 (get core '*2))))
|
'*2 (get core '*1)
|
||||||
(when @dev? (println "out str:" out-str))
|
'*3 (get core '*2))))
|
||||||
(when out-str
|
(when @dev? (println "out str:" out-str))
|
||||||
(send o (response-for msg {"out" out-str})))
|
(when out-str
|
||||||
(send o (response-for msg (cond-> {"ns" (vars/current-ns-name)}
|
(send o (response-for msg {"out" out-str})))
|
||||||
(not (identical? value ::nil)) (assoc "value" (pr-str value)))))
|
(send o (response-for msg (cond-> {"ns" (vars/current-ns-name)}
|
||||||
(send o (response-for msg {"status" #{"done"}})))))
|
(not (identical? value ::nil)) (assoc "value" (pr-str value)))))
|
||||||
|
(when (not (identical? ::nil value))
|
||||||
|
(recur)))))
|
||||||
|
(send o (response-for msg {"status" #{"done"}})))
|
||||||
(catch Exception ex
|
(catch Exception ex
|
||||||
(swap! (:env ctx) update-in [:namespaces 'clojure.core]
|
(swap! (:env ctx) update-in [:namespaces 'clojure.core]
|
||||||
assoc '*e ex)
|
assoc '*e ex)
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,15 @@
|
||||||
"id" (new-id!)
|
"id" (new-id!)
|
||||||
"ns" "unicorn"})
|
"ns" "unicorn"})
|
||||||
(let [reply (read-reply in session @id)]
|
(let [reply (read-reply in session @id)]
|
||||||
(is (= "unicorn" (:value reply)))))))
|
(is (= "unicorn" (:value reply))))))
|
||||||
|
(testing "multiple top level expressions results in two value replies"
|
||||||
|
(bencode/write-bencode os {"op" "eval"
|
||||||
|
"code" "(+ 1 2 3) (+ 1 2 3)"
|
||||||
|
"session" session
|
||||||
|
"id" (new-id!)})
|
||||||
|
(let [reply-1 (read-reply in session @id)
|
||||||
|
reply-2 (read-reply in session @id)]
|
||||||
|
(is (= "6" (:value reply-1) (:value reply-2))))))
|
||||||
(testing "load-file"
|
(testing "load-file"
|
||||||
(bencode/write-bencode os {"op" "load-file" "file" "(ns foo) (defn foo [] :foo)" "session" session "id" (new-id!)})
|
(bencode/write-bencode os {"op" "load-file" "file" "(ns foo) (defn foo [] :foo)" "session" session "id" (new-id!)})
|
||||||
(read-reply in session @id)
|
(read-reply in session @id)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue