       (tear-down repl-env)
       (throw t))))
 
-(defrecord JscEnv [response-promise bonjour-name webdav-mount-point socket options]
+(defrecord JscEnv [response-promise bonjour-name webdav-mount-point socket torn-down reconnect-fn options]
   repl/IReplEnvOptions
   (-repl-options [this]
     {:require-foreign true})

  (-load [repl-env provides url]
    (load-javascript repl-env provides url))
  (-tear-down [repl-env]
    (tear-down repl-env)))

(defn repl-env* [options]
  (JscEnv. (atom nil) (atom nil) (atom nil) (atom nil) options))

(defn repl-env
  [& {:as options}]

  (let [{:keys [out]} @(:socket repl-env)
        response-promise (promise)]
    (reset! (:response-promise repl-env) response-promise)
    (try
      (write out js)
      (let [response @response-promise]
        (if (= :eof response)
          (do
            (reconnect-if repl-env)
            {:status :error
             :value  "Connection closed."})
          (let [result (json/read-str response
                         :key-fn keyword)]
            (merge
              {:status (keyword (:status result))
               :value  (:value result)}
              (when-let [raw-stacktrace (:stacktrace result)]
                {:stacktrace raw-stacktrace})))))
      (catch Exception _
        (reconnect-if repl-env)))))

(defn load-javascript
  "Load a Closure JavaScript file into the JSC REPL process."
