Skip to content

Commit

Permalink
[#584, #585] CLJS try/catch fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Jul 3, 2021
1 parent d3ef8dc commit f37e1ad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/sci/impl/analyzer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,12 @@
body (analyze ctx (cons 'do body-exprs))
catches (mapv (fn [c]
(let [[_ ex binding & body] c]
(if-let [clazz (interop/resolve-class ctx ex)]
(if-let [clazz #?(:clj (interop/resolve-class ctx ex)
:cljs (case ex
js/Error js/Error
js/Object js/Object
:default :default
(analyze ctx ex)))]
{:class clazz
:binding binding
:body (analyze (assoc-in ctx [:bindings binding] nil)
Expand Down
7 changes: 6 additions & 1 deletion src/sci/impl/evaluator.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@
[[_ r]
(reduce (fn [_ c]
(let [clazz (:class c)]
(when (instance? clazz e)
(when #?(:cljs
(or (kw-identical? :default clazz)
(if (instance? sci.impl.types/EvalFn clazz)
(instance? (eval ctx bindings clazz) e)
(instance? clazz e)))
:clj (instance? clazz e))
(reduced
[::try-result
(eval ctx
Expand Down
8 changes: 7 additions & 1 deletion test/sci/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,13 @@
(is (= 'hello (eval* "(try 'hello)"))))
(testing "babashka GH-220, try should accept nil in body"
(is (nil? (eval* "(try 1 2 nil)")))
(is (= 1 (eval* "(try 1 2 nil 1)")))))
(is (= 1 (eval* "(try 1 2 nil 1)"))))
#?(:cljs
(testing "allow all"
(let [ctx (sci/init {:classes {'js goog/global :allow :all}})]
(is (= 12 (sci/eval-string* ctx "(try (assoc 1 1 1) (catch :default e 12))")))
(is (= 12 (sci/eval-string* ctx "(try (assoc 1 1 1) (catch js/Object e 12))")))
(is (= 12 (sci/eval-string* ctx "(try (assoc 1 1 1) (catch (if (odd? 1) js/Object js/Error) e 12))")))))))

(deftest syntax-quote-test
(is (= '(clojure.core/list 10 10)
Expand Down

0 comments on commit f37e1ad

Please sign in to comment.