Skip to content

Commit 4b423a3

Browse files
committed
fork vars
1 parent 260ccb3 commit 4b423a3

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/sci/core.cljc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,23 @@
269269
"Forks a context (as produced with `init`) into a new context. Any new
270270
vars created in the new context won't be visible in the original
271271
context."
272-
[ctx]
273-
(update ctx :env (fn [env] (atom @env))))
272+
([ctx]
273+
(update ctx :env (fn [env] (atom @env))))
274+
([ctx {:keys [clone-vars]}]
275+
(update ctx :env (fn [env]
276+
(let [env @env
277+
env (if clone-vars
278+
(update env :namespaces
279+
(fn [namespaces]
280+
(update-vals namespaces (fn [namespace]
281+
(update-vals namespace
282+
(fn [var]
283+
(if (utils/var? var)
284+
(.clone ^sci.lang.Var var)
285+
var)))))))
286+
287+
env)]
288+
(atom env))))))
274289

275290
(defn eval-string*
276291
"Evaluates string `s` in the context of `ctx` (as produced with

src/sci/lang.cljc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,10 @@
249249
(apply @this a b c d e f g h i j k l m n o p q r s t rest))
250250
#?(:clj
251251
(applyTo [this args]
252-
(apply @this args))))
252+
(apply @this args)))
253+
#?@(:clj [Cloneable
254+
(clone [_this]
255+
(new Var root sym meta thread-bound needs-ctx watches))]))
253256

254257
#?(:clj
255258
;; Use public interface for print-method so it can be overriden in bb itself

0 commit comments

Comments
 (0)