        :value (.toString ex)
        :stacktrace (stacktrace ex)})))
 
-(defn goog-require [rule]
-  (when-not (contains? @loaded-libs rule)
+(defn goog-require [repl-env rule]
+  (when-not (contains? @(:loaded-libs repl-env) rule)
     (let [repl-env @current-repl-env
           path (string/replace (comp/munge rule) \. java.io.File/separatorChar)
           cljs-path (str path ".cljs")

        (if-let [res (io/resource js-path)]
          (-eval (io/reader res) repl-env js-path 1)
          (throw (Exception. (str "Cannot find " cljs-path " or " js-path " in classpath")))))
      (swap! loaded-libs conj rule))))

(defn load-javascript [repl-env ns url]
  (let [missing (remove #(contains? @loaded-libs %) ns)]
    (when (seq missing)
      (do (try 
            (-eval (io/reader url) repl-env (.toString url) 1)
            ;; TODO: don't show errors for goog/base.js line number 105
            (catch Throwable ex (println (.getMessage ex))))
          (swap! loaded-libs (partial apply conj) missing)))))

(defn rhino-setup [repl-env]
  (let [env (ana/empty-env)
        scope (:scope repl-env)]
    (repl/load-file repl-env "cljs/core.cljs")
    (swap! loaded-libs conj "cljs.core")
    (repl/evaluate-form repl-env
                        env
                        "<cljs repl>"

           [org.mozilla.javascript Context ScriptableObject]))

(def current-repl-env (atom nil))

;;todo - move to core.cljs, using js
(def ^String bootjs (str "goog.require = function(rule){"
                         "Packages.clojure.lang.RT[\"var\"](\"cljs.repl.rhino\",\"goog-require\")"
                         ".invoke(___repl_env, rule);}"))

(defprotocol IEval
  (-eval [this env filename line]))
