Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #31] load-file should return value of last expression #32

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions src/cemerick/piggieback.clj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@
(set! *ns* *original-clj-ns*)
(set! *original-clj-ns* nil))

(defn read-value
"Try to read return value as a Clojure expression"
[value]
(try
(read-string value)
(catch Exception _
(when (string? value)
(println value)))))

(defn cljs-eval
"Evaluates the expression [expr] (should already be read) using the
given ClojureScript REPL environment [repl-env] and a map of
Expand Down Expand Up @@ -114,11 +123,7 @@
expr
(#'cljsrepl/wrap-fn expr))]
(set-ns!)
(try
(read-string ret)
(catch Exception _
(when (string? ret)
(println ret))))))))
(read-value ret)))))
(when *original-clj-ns*
(set! ana/*cljs-ns* @escaping-ns)
(set! *ns* (create-ns @escaping-ns)))))))
Expand All @@ -130,10 +135,32 @@
'~expr
@#'*cljs-repl-options*)))

(defn wrap-form
[form]
(if (and (seq? form) (= 'ns (first form)))
form
(list 'cljs.core.pr-str form)))

(defn evaluate-load-form
"Cljs load-form with in-ns special-fn"
[repl-env env filename form wrap-form]
(if (and (seq? form) (= 'in-ns (first form)))
(apply (cljsrepl/default-special-fns 'in-ns) form)
(cljsrepl/evaluate-form repl-env env filename form wrap-form)))

(defn load-stream [repl-env filename res]
(let [env (ana/empty-env)]
(when-let [forms (seq (ana/forms-seq res filename))]
(doseq [form (butlast forms)]
(let [env (assoc env :ns (ana/get-namespace ana/*cljs-ns*))]
(evaluate-load-form repl-env env filename form identity)))
(let [env (assoc env :ns (ana/get-namespace ana/*cljs-ns*))]
(read-value (evaluate-load-form repl-env env filename (last forms) wrap-form))))))

(defn- load-file-contents
[repl-env code file-path file-name]
(binding [ana/*cljs-ns* 'cljs.user]
(cljs.repl/load-stream repl-env file-path (java.io.StringReader. code))))
(load-stream repl-env file-path (java.io.StringReader. code))))

(defn- load-file-code
[code file-path file-name]
Expand Down