cljs.repl/evaluate-form

functionsince v0.0-927Edit
(evaluate-form repl-env env filename form)
(evaluate-form repl-env env filename form wrap)
(evaluate-form repl-env env filename form wrap opts)

Source docstring:
Evaluate a ClojureScript form in the JavaScript environment. Returns a
string which is the ClojureScript return value. This string may or may
not be readable by the Clojure reader.
Source code @ clojurescript:src/main/clojure/cljs/repl.cljc
(defn evaluate-form
  ([repl-env env filename form]
    (evaluate-form repl-env env filename form identity))
  ([repl-env env filename form wrap]
    (evaluate-form repl-env env filename form wrap *repl-opts*))
  ([repl-env env filename form wrap opts]
   (binding [ana/*cljs-file* filename]
     (let [env (merge env
                 {:root-source-info {:source-type :fragment
                                     :source-form form}
                  :repl-env repl-env})
           def-emits-var (:def-emits-var opts)
           backup-comp @env/*compiler*
           ->ast (fn [form]
                   (binding [ana/*analyze-deps* false]
                     (ana/analyze (assoc env :def-emits-var def-emits-var)
                       (wrap form) nil opts)))
           ast (->ast form)
           ast (if-not (#{:ns :ns*} (:op ast))
                 ast
                 (let [ijs (ana/parse-ns [form])]
                   (cljsc/handle-js-modules opts
                     (deps/dependency-order
                       (cljsc/add-dependency-sources [ijs] opts))
                     env/*compiler*)
                   (binding [ana/*check-alias-dupes* false]
                     (ana/no-warn (->ast form))))) ;; need new AST after we know what the modules are - David
           wrap-js
           ;; TODO: check opts as well - David
           (if (:source-map repl-env)
             (binding [comp/*source-map-data*
                       (atom {:source-map (sorted-map)
                              :gen-line 0})
                       comp/*source-map-data-gen-col* (AtomicLong.)]
               (let [js (comp/emit-str ast)
                     t (System/currentTimeMillis)]
                 (str js
                   "\n//# sourceURL=repl-" t ".js"
                   "\n//# sourceMappingURL=data:application/json;base64,"
                   (bytes-to-base64-str
                     (.getBytes
                       (sm/encode
                         {(str "repl-" t ".cljs")
                          (:source-map @comp/*source-map-data*)}
                         {:lines (+ (:gen-line @comp/*source-map-data*) 3)
                          :file (str "repl-" t ".js")
                          :sources-content
                          [(or (:source (meta form))
                             ;; handle strings / primitives without metadata
                             (with-out-str (pr form)))]})
                       "UTF-8")))))
             (comp/emit-str ast))]
       ;; NOTE: means macros which expand to ns aren't supported for now
       ;; when eval'ing individual forms at the REPL - David
       (when (#{:ns :ns*} (:op ast))
         (let [ast (try
                     (ana/no-warn (ana/analyze env form nil opts))
                     (catch #?(:clj Exception :cljs js/Error) e
                         (reset! env/*compiler* backup-comp)
                         (throw e)))
               sources (load-dependencies repl-env
                         (into (vals (:requires ast))
                               (distinct (vals (:uses ast))))
                         opts)]
           (load-cljs-loader repl-env sources opts)))
       (when *cljs-verbose*
         (err-out (println wrap-js)))
       (let [ret (-evaluate repl-env filename (:line (meta form)) wrap-js)]
         (case (:status ret)
           :error (throw
                    (ex-info (:value ret)
                      {:type :js-eval-error
                       :error ret
                       :repl-env repl-env
                       :form form}))
           :exception (throw
                        (ex-info (:value ret)
                          {:type :js-eval-exception
                           :error ret
                           :repl-env repl-env
                           :form form
                           :js wrap-js}))
           :success (:value ret)))))))