Skip to content

Commit 4f86b74

Browse files
committed
[#379] Fix false dynamic binding
1 parent 2e4d6e0 commit 4f86b74

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

src/sci/impl/vars.cljc

+20-21
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@
6464
#?(:clj (.get dvals)
6565
:cljs @dvals))
6666

67+
(deftype TBox #?(:clj [thread ^:volatile-mutable val]
68+
:cljs [thread ^:mutable val])
69+
t/IBox
70+
(setVal [this v]
71+
(set! val v))
72+
(getVal [this] val))
73+
6774
(defn clone-thread-binding-frame ^Frame []
6875
(let [^Frame f #?(:clj (.get dvals)
6976
:cljs @dvals)]
@@ -73,13 +80,6 @@
7380
#?(:clj (.set dvals frame)
7481
:cljs (reset! dvals frame)))
7582

76-
(deftype TBox #?(:clj [thread ^:volatile-mutable val]
77-
:cljs [thread ^:mutable val])
78-
t/IBox
79-
(setVal [this v]
80-
(set! val v))
81-
(getVal [this] val))
82-
8383
(declare var?)
8484

8585
(defn dynamic-var? [v]
@@ -259,25 +259,24 @@
259259
(not (instance? SciUnbound root)))
260260
t/IBox
261261
(setVal [this v]
262-
(let [b (get-thread-binding this)]
263-
(if (some? b)
264-
#?(:clj
265-
(let [t (.-thread b)]
266-
(if (not (identical? t (Thread/currentThread)))
267-
(throw (new IllegalStateException
268-
(str "Can't change/establish root binding of " this " with set")))
269-
(t/setVal b v)))
270-
:cljs (t/setVal b v))
271-
(throw (new #?(:clj IllegalStateException :cljs js/Error)
272-
(str "Can't change/establish root binding of " this " with set"))))))
262+
(if-let [b (get-thread-binding this)]
263+
#?(:clj
264+
(let [t (.-thread b)]
265+
(if (not (identical? t (Thread/currentThread)))
266+
(throw (new IllegalStateException
267+
(format "Can't set!: %s from non-binding thread" sym)))
268+
(t/setVal b v)))
269+
:cljs (t/setVal b v))
270+
(throw (new #?(:clj IllegalStateException :cljs js/Error)
271+
(str "Can't change/establish root binding of " this " with set")))))
273272
(getVal [this] root)
274273
#?(:clj clojure.lang.IDeref :cljs IDeref)
275274
(#?(:clj deref
276275
:cljs -deref) [this]
277276
(if thread-bound
278-
(or (when-let [tbox (get-thread-binding this)]
279-
(t/getVal tbox))
280-
root)
277+
(if-let [tbox (get-thread-binding this)]
278+
(t/getVal tbox)
279+
root)
281280
root))
282281
Object
283282
(toString [_]

test/sci/vars_test.cljc

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
#_#_:sci.impl/built-in true})]
3636
(is (= [11 10] (sci/eval-string
3737
"[(binding [*x* (fn [] 11)] (*x*)) (*x*)]"
38-
{:bindings {'*x* x}}))))))
38+
{:bindings {'*x* x}})))))
39+
(testing "dynamic binding of false works"
40+
(is (false? (sci/eval-string
41+
"(def ^:dynamic x nil) (binding [x false] x)")))))
3942

4043
(deftest redefine-var-test
4144
(is (= 11 (eval* "

0 commit comments

Comments
 (0)