diff --git a/src/babashka/impl/clojure/core/server.clj b/src/babashka/impl/clojure/core/server.clj index 113934a0..3de2f97e 100644 --- a/src/babashka/impl/clojure/core/server.clj +++ b/src/babashka/impl/clojure/core/server.clj @@ -1,5 +1,5 @@ -;; Modified / stripped version of clojure.core.server for use with babashka on -;; GraalVM. +;; Modified / stripped version of clojure.core.server for use with babashka on +;; GraalVM. ;; ;; Copyright (c) Rich Hickey. All rights reserved. ;; The use and distribution terms for this software are covered by the diff --git a/src/babashka/impl/nrepl_server.clj b/src/babashka/impl/nrepl_server.clj index 7d44ca36..89a46ec7 100644 --- a/src/babashka/impl/nrepl_server.clj +++ b/src/babashka/impl/nrepl_server.clj @@ -41,21 +41,20 @@ (defn eval-msg [ctx o msg #_threads] (try (let [ns-str (get msg :ns) - sci-ns (if ns-str - (sci-utils/namespace-object (:env ctx) (symbol ns-str) nil false) - (sci-utils/namespace-object (:env ctx) 'user nil false))] - (sci/binding [vars/current-ns sci-ns - sci/print-length @sci/print-length] + sw (StringWriter.)] + (sci/with-bindings (cond-> {sci/out sw} + ns-str + (assoc vars/current-ns + (sci-utils/namespace-object (:env ctx) (symbol ns-str) nil false))) + (when @dev? (println "current ns" (vars/current-ns-name))) (let [session (get msg :session "none") id (get msg :id "unknown")] (when @dev? (println "Registering thread for" (str session "-" id))) ;; (swap! threads assoc [session id] (Thread/currentThread)) (let [code-str (get msg :code) - sw (StringWriter.) value (if (str/blank? code-str) ::nil - (sci/binding [sci/out sw - vars/current-ns @vars/current-ns] (eval-string* ctx code-str))) + (eval-string* ctx code-str)) out-str (not-empty (str sw)) env (:env ctx)] (swap! env update-in [:namespaces 'clojure.core] @@ -96,13 +95,10 @@ (defn complete [ctx o msg] (try (let [ns-str (get msg :ns) - sci-ns (if ns-str - (sci-utils/namespace-object (:env ctx) (symbol ns-str) nil false) - (sci-utils/namespace-object (:env ctx) 'user nil false))] - (sci/binding [vars/current-ns sci-ns] - (let [ - ;;ns-sym (symbol ns) - query (:symbol msg) + sci-ns (when ns-str + (sci-utils/namespace-object (:env ctx) (symbol ns-str) nil false))] + (sci/binding [vars/current-ns (or sci-ns @vars/current-ns)] + (let [query (:symbol msg) from-current-ns (fully-qualified-syms ctx (eval-string* ctx "(ns-name *ns*)")) from-current-ns (map (fn [sym] [(namespace sym) (name sym) :unqualified]) @@ -179,7 +175,7 @@ :describe (do (send os (response-for msg {"status" #{"done"} "aux" {} - "ops" (zipmap #{"clone", "describe", "eval"} + "ops" (zipmap #{"clone" "eval" "load-file" "complete" "describe"} (repeat {})) "versions" {} #_{"nrepl" {"major" "0" "minor" "4" diff --git a/test/babashka/impl/nrepl_server_test.clj b/test/babashka/impl/nrepl_server_test.clj index 84c81b54..4175de2b 100644 --- a/test/babashka/impl/nrepl_server_test.clj +++ b/test/babashka/impl/nrepl_server_test.clj @@ -52,7 +52,23 @@ id (:id msg) value (:value msg)] (is (= 1 id)) - (is (= value "6")))) + (is (= value "6"))) + (testing "creating a namespace and evaluating something in it" + (bencode/write-bencode os {"op" "eval" + "code" "(ns ns0) (defn foo [] :foo0) (ns ns1) (defn foo [] :foo1)" + "session" session + "id" (new-id!)}) + (read-reply in session @id) + (testing "not providing the ns key evaluates in the last defined namespace" + (bencode/write-bencode os {"op" "eval" "code" "(foo)" "session" session "id" (new-id!)}) + (is (= ":foo1" (:value (read-reply in session @id))))) + (testing "explicitly providing the ns key evaluates in that namespace" + (bencode/write-bencode os {"op" "eval" + "code" "(foo)" + "session" session + "id" (new-id!) + "ns" "ns0"}) + (is (= ":foo0" (:value (read-reply in session @id))))))) (testing "load-file" (bencode/write-bencode os {"op" "load-file" "file" "(ns foo) (defn foo [] :foo)" "session" session "id" (new-id!)}) (read-reply in session @id)