Skip to content

Commit afe2cf7

Browse files
Matthew Huebertswannodette
Matthew Huebert
authored andcommitted
CLJS-3287: selfhost: eval does not catch and return errors
in cljs.js, eval-str catches errors during analysis, compilation and evaluation, and returns a {:value … :error …} map. In contrast, the eval function doesn’t catch errors during the evaluation step. Attached patch + tests to make these behave consistently.
1 parent fdbd29e commit afe2cf7

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/main/cljs/cljs/js.cljs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,9 +837,15 @@
837837
(filter ana/dep-has-global-exports? (:deps ast))
838838
ns-name
839839
(:def-emits-var opts))
840-
(cb {:value (*eval-fn* {:source (.toString sb)})})))))
840+
(cb (try
841+
{:ns ns-name :value (*eval-fn* {:source (.toString sb)})}
842+
(catch :default cause
843+
(wrap-error (ana/error aenv "ERROR" cause)))))))))
841844
(let [src (with-out-str (comp/emit ast))]
842-
(cb {:value (*eval-fn* {:source src})})))))))))
845+
(cb (try
846+
{:value (*eval-fn* {:source src})}
847+
(catch :default cause
848+
(wrap-error (ana/error aenv "ERROR" cause)))))))))))))
843849

844850
(defn eval
845851
"Evaluate a single ClojureScript form. The parameters:

src/test/self/self_host/test.cljs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,28 @@
15571557
(ex-message (ex-cause (ex-cause error)))))
15581558
(inc! l)))))))))
15591559

1560+
(deftest test-cljs-3287
1561+
(async done
1562+
(let [st (cljs/empty-state)
1563+
l (latch 2 done)]
1564+
(cljs/eval-str st
1565+
"(throw (js/Error. \"eval error\"))"
1566+
nil
1567+
{:ns 'cljs.user
1568+
:target :nodejs
1569+
:eval node-eval}
1570+
(fn [{:keys [error]}]
1571+
(is (some? error))
1572+
(inc! l)))
1573+
(cljs/eval st
1574+
'(throw (js/Error. "eval error"))
1575+
{:ns 'cljs.user
1576+
:target :nodejs
1577+
:eval node-eval}
1578+
(fn [{:keys [error]}]
1579+
(is (some? error))
1580+
(inc! l))))))
1581+
15601582
(defn -main [& args]
15611583
(run-tests))
15621584

0 commit comments

Comments
 (0)