Skip to content

Commit

Permalink
[#430] bound?
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Oct 15, 2020
1 parent ab31be7 commit 8e06b82
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/sci/impl/namespaces.cljc
Expand Up @@ -334,7 +334,7 @@
(defn has-root-impl [sci-var]
(vars/hasRoot sci-var))

;;;; Namespaces
;;;; Namespaces / vars

(defn sci-ns-name [^sci.impl.vars.SciNamespace ns]
(vars/getName ns))
Expand Down Expand Up @@ -451,6 +451,13 @@
(swap! env assoc-in [:namespaces ns-name var-sym] new-var)
new-var)))))

(defn sci-bound?
[sci-var]
;; see https://github.com/clojure/clojure/blob/cbb3fdf787a00d3c1443794b97ed7fe4bef8e888/src/jvm/clojure/lang/Var.java#L190
(or (vars/hasRoot sci-var)
(some? (vars/get-thread-binding sci-var))
false))

;;;; End namespaces

;;;; Eval and read-string
Expand Down Expand Up @@ -1072,7 +1079,7 @@
'-' (copy-core-var -')
'*' (copy-core-var *')
'boolean-array (copy-core-var boolean-array)
'bound? (copy-core-var bound?)
'bound? (copy-core-var sci-bound?)
'byte-array (copy-core-var byte-array)
'bigint (copy-core-var bigint)
'bytes? (copy-core-var bytes?)
Expand Down
7 changes: 7 additions & 0 deletions test/sci/core_test.cljc
Expand Up @@ -1072,6 +1072,13 @@
(is (= '(/ 100 (inc 9))
(eval* "(macroexpand '(->> 9 inc (/ 100)))")))))

(deftest bound-test
(is (false? (eval* "(def x) (bound? #'x)")))
(is (true? (eval* "(def x 1) (bound? #'x)")))
(is (false? (eval* "(def ^:dynamic x) (bound? #'x)")))
(is (true? (eval* "(def ^:dynamic x) (binding [x 1] (bound? #'x))")))
(is (false? (eval* "(def ^:dynamic x) (binding [x 1]) (bound? #'x)"))))

;;;; Scratch

(comment
Expand Down

0 comments on commit 8e06b82

Please sign in to comment.