Permalink
Browse files

Don't use push- and pop-thread-bindings directly; fixes NREPL-16

  • Loading branch information...
1 parent 07786ca commit 9af6e2bc287e52b6d585fac4c45b0a2ec976fbb6 @cemerick cemerick committed Apr 16, 2012
Showing with 41 additions and 34 deletions.
  1. +41 −34 src/main/clojure/clojure/tools/nrepl/middleware/interruptible_eval.clj
@@ -30,40 +30,47 @@
It is assumed that `bindings` already contains useful/appropriate entries
for all vars indicated by `clojure.main/with-bindings`."
[bindings {:keys [code ns transport] :as msg}]
- (let [bindings (atom (merge bindings (when ns {#'*ns* (-> ns symbol find-ns)})))]
- (try
- (clojure.main/repl
- :init (fn [] (push-thread-bindings @bindings))
- :read (if (string? code)
- (let [reader (LineNumberingPushbackReader. (StringReader. code))]
- #(read reader false %2))
- (let [q (java.util.concurrent.ArrayBlockingQueue. (count code) false code)]
- #(or (.poll q 0 TimeUnit/MILLISECONDS) %2)))
- :prompt (fn [])
- :need-prompt (constantly false)
- ; TODO pretty-print?
- :print (fn [v]
- (reset! bindings (assoc (get-thread-bindings)
- #'*3 *2
- #'*2 *1
- #'*1 v))
- (t/send transport (response-for msg
- {:value v
- :ns (-> *ns* ns-name str)})))
- ; TODO customizable exception prints
- :caught (fn [e]
- (let [root-ex (#'clojure.main/root-cause e)]
- (when-not (instance? ThreadDeath root-ex)
- (reset! bindings (assoc (get-thread-bindings) #'*e e))
- (t/send transport (response-for msg {:status :eval-error
- :ex (-> e class str)
- :root-ex (-> root-ex class str)}))
- (clojure.main/repl-caught e)))))
- @bindings
- (finally
- (pop-thread-bindings)
- (.flush ^Writer (@bindings #'*out*))
- (.flush ^Writer (@bindings #'*err*))))))
+ (let [bindings (atom (merge bindings (when ns {#'*ns* (-> ns symbol find-ns)})))
+ out (@bindings #'*out*)
+ err (@bindings #'*err*)]
+ (with-bindings @bindings
+ (try
+ (clojure.main/repl
+ ;; clojure.main/repl paves over certain vars even if they're already thread-bound
+ :init #(do (set! *compile-path* (@bindings #'*compile-path*))
+ (set! *1 (@bindings #'*1))
+ (set! *2 (@bindings #'*2))
+ (set! *3 (@bindings #'*3))
+ (set! *e (@bindings #'*e)))
+ :read (if (string? code)
+ (let [reader (LineNumberingPushbackReader. (StringReader. code))]
+ #(read reader false %2))
+ (let [q (java.util.concurrent.ArrayBlockingQueue. (count code) false code)]
+ #(or (.poll q 0 TimeUnit/MILLISECONDS) %2)))
+ :prompt (fn [])
+ :need-prompt (constantly false)
+ ; TODO pretty-print?
+ :print (fn [v]
+ (reset! bindings (assoc (get-thread-bindings)
+ #'*3 *2
+ #'*2 *1
+ #'*1 v))
+ (t/send transport (response-for msg
+ {:value v
+ :ns (-> *ns* ns-name str)})))
+ ; TODO customizable exception prints
+ :caught (fn [e]
+ (let [root-ex (#'clojure.main/root-cause e)]
+ (when-not (instance? ThreadDeath root-ex)
+ (reset! bindings (assoc (get-thread-bindings) #'*e e))
+ (t/send transport (response-for msg {:status :eval-error
+ :ex (-> e class str)
+ :root-ex (-> root-ex class str)}))
+ (clojure.main/repl-caught e)))))
+ @bindings
+ (finally
+ (.flush ^Writer out)
+ (.flush ^Writer err))))))
(defn- configure-thread-factory
"Returns a new ThreadFactory for the given session. This implementation

0 comments on commit 9af6e2b

Please sign in to comment.