From 22fd5867244ffe1f69a6e2e9eec99d70281c53b8 Mon Sep 17 00:00:00 2001 From: Rick Moynihan Date: Mon, 21 Sep 2020 22:10:57 +0100 Subject: [PATCH] [#420] Add support for clojure.core/find-var --- src/sci/impl/namespaces.cljc | 9 ++++++++- test/sci/namespaces_test.cljc | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/sci/impl/namespaces.cljc b/src/sci/impl/namespaces.cljc index c2881c4e..a2154bdd 100644 --- a/src/sci/impl/namespaces.cljc +++ b/src/sci/impl/namespaces.cljc @@ -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]) @@ -514,6 +515,11 @@ :cljs js/Error) (str "Not a qualified symbol: " sym)))))) +(defn sci-find-var [sci-ctx sym] + (let [namespace (-> sym namespace symbol) + sym' (-> sym name symbol)] + (-> sci-ctx :env deref :namespaces (get-in [namespace sym'])))) + ;;;; End require + resolve ;;;; Binding vars @@ -772,6 +778,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}) + #?@(:clj ['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) diff --git a/test/sci/namespaces_test.cljc b/test/sci/namespaces_test.cljc index cde6ba95..b0b980c2 100644 --- a/test/sci/namespaces_test.cljc +++ b/test/sci/namespaces_test.cljc @@ -70,6 +70,9 @@ (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))"))) + (deftest find-ns-test (is (true? (eval* "(ns foo) (some? (find-ns 'foo))"))) (is (nil? (eval* "(find-ns 'foo)"))))