Skip to content

Commit 7ab25ce

Browse files
committed
feat: add active-* accessors for trace, splice
1 parent e04ca19 commit 7ab25ce

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/gen/dynamic.cljc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@
208208
(if-not (valid-trace-form? form)
209209
(throw (ex-info "Malformed trace expression." {:form form}))
210210
(let [[addr [gf & args]] (rest form)]
211-
`(dynamic.trace/*trace* ~addr ~gf ~(vec args))))
211+
`((dynamic.trace/active-trace) ~addr ~gf ~(vec args))))
212212

213213
(splice-form? form)
214214
(if-not (valid-splice-form? form)
215215
(throw (ex-info "Malformed splice expression." {:form form}))
216216
(let [[[gf & args]] (rest form)]
217-
`(dynamic.trace/*splice* ~gf ~(vec args))))
217+
`((dynamic.trace/active-splice) ~gf ~(vec args))))
218218

219219
:else
220220
form))

src/gen/dynamic/trace.cljc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@
2727
like `gf/simulate`, `gf/generate`, `trace/update`, etc."
2828
no-op)
2929

30+
(defn active-trace
31+
"Returns the currently-active tracing function, bound to [[*trace*]].
32+
33+
NOTE: Prefer `([[active-trace]])` to `[[*trace*]]`, as direct access to
34+
`[[*trace*]]` won't reflect new bindings when accessed inside of an SCI
35+
environment."
36+
[] *trace*)
37+
38+
(defn active-splice
39+
"Returns the currently-active tracing function, bound to [[*splice*]].
40+
41+
NOTE: Prefer `([[active-splice]])` to `[[*splice*]]`, as direct access to
42+
`[[*splice*]]` won't reflect new bindings when accessed inside of an SCI
43+
environment."
44+
[]
45+
*splice*)
46+
3047
(defmacro without-tracing
3148
[& body]
3249
`(binding [*trace* no-op

test/gen/dynamic/trace_test.cljc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
[gen.dynamic.trace :as dynamic.trace]
99
[gen.trace :as trace]))
1010

11+
(deftest binding-tests
12+
(letfn [(f [_] "hi!")]
13+
(binding [dynamic.trace/*trace* f
14+
dynamic.trace/*splice* f]
15+
(is (= f (dynamic.trace/active-trace))
16+
"active-trace reflects dynamic bindings")
17+
18+
(is (= f (dynamic.trace/active-splice))
19+
"active-splice reflects dynamic bindings"))))
20+
1121
(defn choice-trace
1222
[x]
1323
(reify trace/Choices

0 commit comments

Comments
 (0)