                         "<cljs repl>"
                         '(set! *print-fn* (fn [x] (.print js/out x))))))
 
-(defrecord RhinoEnv []
+(defrecord RhinoEnv [loaded-libs]
   repl/IJavaScriptEnv
   (-setup [this]
     (rhino-setup this))

           [org.mozilla.javascript Context ScriptableObject]))

(def current-repl-env (atom nil))
(def loaded-libs (atom #{}))

;;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(rule);}"))

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

        (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 repl-env) conj rule))))

(defn load-javascript [repl-env ns url]
  (let [missing (remove #(contains? @(:loaded-libs repl-env) %) 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 repl-env) (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 repl-env) conj "cljs.core")
    (repl/evaluate-form repl-env
                        env
                        "<cljs repl>"
