Skip to content

Commit

Permalink
[babashka#420] Add support for clojure.core/find-var
Browse files Browse the repository at this point in the history
  • Loading branch information
RickMoynihan committed Sep 22, 2020
1 parent fba06e9 commit 67616a6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/sci/impl/namespaces.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
{:no-doc true}
(:refer-clojure :exclude [ex-message ex-cause eval read
read-string require
use load-string])
use load-string
find-var])
(:require
#?(:clj [clojure.edn :as edn]
:cljs [cljs.reader :as edn])
Expand Down Expand Up @@ -514,6 +515,19 @@
:cljs js/Error)
(str "Not a qualified symbol: " sym))))))

(defn sci-find-var [sci-ctx sym]
(if (qualified-symbol? sym)
(let [nsname (-> sym namespace symbol)
sym' (-> sym name symbol)]
(if-let [namespace (-> sci-ctx :env deref :namespaces (get nsname))]
(get namespace sym')
(throw (new #?(:clj IllegalArgumentException
:cljs js/Error)
(str "No such namespace: " nsname)))))
(throw (new #?(:clj IllegalArgumentException
:cljs js/Error)
(str "Not a qualified symbol: " sym)))))

;;;; End require + resolve

;;;; Binding vars
Expand Down Expand Up @@ -772,6 +786,7 @@
'ex-message (copy-core-var ex-message)
'ex-cause (copy-core-var ex-cause)
'find-ns (with-meta sci-find-ns {:sci.impl/op :needs-ctx})
'find-var (with-meta sci-find-var {:sci.impl/op :needs-ctx})
'first (copy-core-var first)
'float? (copy-core-var float?)
'floats (copy-core-var floats)
Expand Down
10 changes: 10 additions & 0 deletions test/sci/namespaces_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@
(is (eval* "(defn bar []) (ns-unmap *ns* 'bar) (nil? (resolve 'bar))"))
(is (eval* "(defn- baz []) (ns-unmap *ns* 'baz) (nil? (resolve 'baz))")))

(deftest find-var-test
(is (eval* "(= #'clojure.core/map (find-var 'clojure.core/map))"))
(is (eval* "(nil? (find-var 'clojure.core/no-such-symbol))"))
(is (thrown-with-msg? #?(:clj clojure.lang.ExceptionInfo :cljs js/Error)
#"No such namespace: no.such.namespace"
(eval* "(find-var 'no.such.namespace/var)")))
(is (thrown-with-msg? #?(:clj clojure.lang.ExceptionInfo :cljs js/Error)
#"Not a qualified symbol: no-namespace"
(eval* "(find-var 'no-namespace)"))))

(deftest find-ns-test
(is (true? (eval* "(ns foo) (some? (find-ns 'foo))")))
(is (nil? (eval* "(find-ns 'foo)"))))
Expand Down

0 comments on commit 67616a6

Please sign in to comment.