Switch to use ctx-store for global context

This commit is contained in:
Michiel Borkent 2022-11-19 11:00:35 +01:00
parent 495ded8a50
commit b458d79b29
14 changed files with 35 additions and 32 deletions

View file

@ -60,7 +60,7 @@
(selmer.parser/render-template template context-map)))
(defn sci-ns-resolve [ns fqs]
(sci/eval-form @ctx (list 'clojure.core/ns-resolve ns (list 'quote fqs))))
(sci/eval-form (ctx) (list 'clojure.core/ns-resolve ns (list 'quote fqs))))
(defn force! [x]
(if (instance? clojure.lang.IDeref x) @x x))

View file

@ -40,8 +40,8 @@
#_(assert (<= (count ans)) (pr-str ans))
(let [xn (xml/uri-symbol n)
al (symbol (clj-ns-name a))]
(sci/eval-form @ctx `(create-ns (quote ~xn)))
(sci/eval-form @ctx `(alias (quote ~al) (quote ~xn)))
(sci/eval-form (ctx) `(create-ns (quote ~xn)))
(sci/eval-form (ctx) `(alias (quote ~al) (quote ~xn)))
(recur rst)))))
(def xml-namespace

View file

@ -167,7 +167,7 @@
'default-data-readers (copy-core-var default-data-readers)
'xml-seq (copy-core-var xml-seq)
'read+string (new-var 'read+string (fn [& args]
(apply read+string @common/ctx args)))
(apply read+string (common/ctx) args)))
'*command-line-args* command-line-args
'*warn-on-reflection* warn-on-reflection
'*compile-files* compile-files

View file

@ -408,7 +408,7 @@
{:added "1.1"}
[x]
(if (symbol? x)
(when-let [v (second (resolve/lookup @ctx x false))]
(when-let [v (second (resolve/lookup (ctx) x false))]
(when-let [value (if (instance? sci.lang.Var v)
(get-possibly-unbound-var v)
v)]
@ -826,7 +826,7 @@
Because the intent is to run a single test, there is no check for the namespace test-ns-hook."
{:added "1.11"}
[test-symbol]
(let [test-var (sci/resolve @ctx test-symbol)]
(let [test-var (sci/resolve (ctx) test-symbol)]
(cond
(nil? test-var)
(sci/binding [sci/out sci/err]

View file

@ -1,10 +1,11 @@
(ns babashka.impl.common
(:require
[clojure.java.io :as io]
[clojure.string :as str]))
[clojure.string :as str]
[sci.ctx-store :as ctx-store]))
;; placeholder for ctx
(def ctx (volatile! nil))
(defn ctx [] (ctx-store/get-ctx))
(def bb-edn (volatile! nil))
(def debug (volatile! false))
(def version (str/trim (slurp (io/resource "BABASHKA_VERSION"))))

View file

@ -15,7 +15,7 @@
:describe {"versions" {"babashka" common/version}}
:thread-bind [babashka.impl.clojure.core/warn-on-reflection]}
opts)]
(server/start-server! @common/ctx opts))))
(server/start-server! (common/ctx) opts))))
(def nrepl-server-namespace
(let [ns-sci (sci/create-ns 'babashka.nrepl.server)]

View file

@ -34,7 +34,7 @@
Namespace
(datafy [n]
(with-meta {:name (sci-ns-name n)
:publics (->> n (sci-ns-publics @ctx) sortmap)
:imports (->> n (sci-ns-imports @ctx) sortmap)
:interns (->> n (sci-ns-interns @ctx) sortmap)}
:publics (->> n (sci-ns-publics (ctx)) sortmap)
:imports (->> n (sci-ns-imports (ctx)) sortmap)
:interns (->> n (sci-ns-interns (ctx)) sortmap)}
(meta n))))

View file

@ -1,13 +1,14 @@
(ns babashka.impl.pods
{:no-doc true}
(:refer-clojure :exclude [read])
(:require [babashka.impl.common :refer [ctx bb-edn]]
[babashka.pods.sci :as pods]
[sci.core :as sci]
[clojure.java.io :as io]))
(:require
[babashka.impl.common :refer [bb-edn ctx]]
[babashka.pods.sci :as pods]
[clojure.java.io :as io]
[sci.core :as sci]))
(defn load-pod [& args]
(apply pods/load-pod @ctx args))
(apply pods/load-pod (ctx) args))
(defn load-pods-metadata [pods-map opts]
(reduce-kv

View file

@ -7,15 +7,15 @@
(def sns (sci/create-ns 'clojure.core.server nil))
(def prepl (fn [& args]
(apply server/prepl @common/ctx args)))
(apply server/prepl (common/ctx) args)))
(def io-prepl
(fn [& args]
(apply server/io-prepl @common/ctx args)))
(apply server/io-prepl (common/ctx) args)))
(def start-server
(fn [& args]
(apply server/start-server @common/ctx args)))
(apply server/start-server (common/ctx) args)))
(def clojure-core-server-namespace
{'repl (sci/copy-var socket-repl/repl sns)

View file

@ -11,7 +11,7 @@
;; this is mapped to clojure.core.server/repl in babashka.main
(def repl (fn []
(repl/repl @common/ctx)))
(repl/repl (common/ctx))))
(defn parse-opts [opts]
(let [opts (str/trim opts)

View file

@ -455,15 +455,15 @@
([task {:keys [:parallel]
:or {parallel (:parallel (current-task))}}]
(let [[[expr]] (assemble-task task parallel)]
(sci/eval-string* @ctx expr))))
(sci/eval-string* (ctx) expr))))
(defn exec
([sym]
(let [snippet (cli/exec-fn-snippet sym)]
(sci/eval-string* @ctx snippet)))
(sci/eval-string* (ctx) snippet)))
([sym extra-opts]
(let [snippet (cli/exec-fn-snippet sym extra-opts)]
(sci/eval-string* @ctx snippet))))
(sci/eval-string* (ctx) snippet))))
(def tasks-namespace
{'shell (sci/copy-var shell sci-ns)

View file

@ -5,7 +5,7 @@
(defn contextualize [f]
(fn [& args]
(apply f @ctx args)))
(apply f (ctx) args)))
(def tns t/tns)

View file

@ -58,6 +58,7 @@
[hf.depstar.uberjar :as uberjar]
[sci.addons :as addons]
[sci.core :as sci]
[sci.ctx-store :as ctx-store]
[sci.impl.copy-vars :as sci-copy-vars]
[sci.impl.io :as sio]
[sci.impl.namespaces :as sci-namespaces]
@ -303,7 +304,7 @@ Use bb run --help to show this help output.
s (slurp f)]
(sci/with-bindings {sci/ns @sci/ns
sci/file (.getAbsolutePath f)}
(sci/eval-string* @common/ctx s))))
(sci/eval-string* (common/ctx) s))))
(defn start-socket-repl! [address ctx]
(socket-repl/start-repl! address ctx))
@ -373,7 +374,7 @@ Use bb run --help to show this help output.
'repl (sci/new-var 'repl
(fn [& opts]
(let [opts (apply hash-map opts)]
(repl/start-repl! @common/ctx opts))) {:ns clojure-main-ns})
(repl/start-repl! (common/ctx) opts))) {:ns clojure-main-ns})
'with-bindings (sci/copy-var clojure-main/with-bindings clojure-main-ns)}
'clojure.test t/clojure-test-namespace
'clojure.math math-namespace
@ -847,7 +848,7 @@ Use bb run --help to show this help output.
(when-let [res (cp/source-for-namespace loader namespace nil)]
(if uberscript
(do (swap! uberscript-sources conj (:source res))
(uberscript/uberscript {:ctx @common/ctx
(uberscript/uberscript {:ctx (common/ctx)
:expressions [(:source res)]})
{})
res)))
@ -882,7 +883,7 @@ Use bb run --help to show this help output.
:proxy-fn proxy-fn}
opts (addons/future opts)
sci-ctx (sci/init opts)
_ (vreset! common/ctx sci-ctx)
_ (ctx-store/reset-ctx! sci-ctx)
_ (when-let [pods (:pods @common/bb-edn)]
(when-let [pod-metadata (pods/load-pods-metadata
pods {:download-only (download-only?)})]

View file

@ -48,7 +48,7 @@
(if tu/jvm?
(let [ctx (init {:namespaces {'clojure.core.server clojure-core-server-namespace}
:features #{:bb}})]
(vreset! common/ctx ctx)
(vreset! common/ctx (fn [] ctx))
(start-repl! "0.0.0.0:1666" ctx))
(do (vreset! server-process
(p/process ["./bb" "socket-repl" "localhost:1666"]))
@ -81,7 +81,7 @@
:env (atom {})
:namespaces {'clojure.core.server clojure-core-server-namespace}
:features #{:bb}})]
(vreset! common/ctx ctx)
(vreset! common/ctx (fn [] ctx))
(start-repl! "{:address \"localhost\" :accept clojure.core.server/repl :port 1666}"
ctx))
(do (vreset! server-process
@ -105,7 +105,7 @@
:env (atom {})
:namespaces {'clojure.core.server clojure-core-server-namespace}
:features #{:bb}})]
(vreset! common/ctx ctx)
(vreset! common/ctx (fn [] ctx))
(start-repl! "{:address \"localhost\" :accept clojure.core.server/io-prepl :port 1666}"
ctx))
(do (vreset! server-process