Update sci context access/update

This commit is contained in:
mynomoto 2023-11-19 16:11:06 -03:00
parent 1699bbbe43
commit fd881dd4bd
2 changed files with 15 additions and 13 deletions

View file

@ -4,6 +4,7 @@
[goog.object :as gobject] [goog.object :as gobject]
[goog.string] [goog.string]
[sci.core :as sci] [sci.core :as sci]
[sci.ctx-store :as store]
[sci.impl.unrestrict] [sci.impl.unrestrict]
[scittle.impl.common :refer [cljns]] [scittle.impl.common :refer [cljns]]
[scittle.impl.error :as error] [scittle.impl.error :as error]
@ -45,12 +46,12 @@
'sci.core {'stacktrace sci/stacktrace 'sci.core {'stacktrace sci/stacktrace
'format-stacktrace sci/format-stacktrace}}) 'format-stacktrace sci/format-stacktrace}})
(def !sci-ctx (store/reset-ctx!
(atom (sci/init {:namespaces namespaces (sci/init {:namespaces namespaces
:classes {'js js/globalThis :classes {'js js/globalThis
:allow :all :allow :all
'Math js/Math} 'Math js/Math}
:ns-aliases {'clojure.pprint 'cljs.pprint}}))) :ns-aliases {'clojure.pprint 'cljs.pprint}}))
(def !last-ns (volatile! @sci/ns)) (def !last-ns (volatile! @sci/ns))
@ -58,21 +59,21 @@
(sci/binding [sci/ns @!last-ns] (sci/binding [sci/ns @!last-ns]
(let [rdr (sci/reader s)] (let [rdr (sci/reader s)]
(loop [res nil] (loop [res nil]
(let [form (sci/parse-next @!sci-ctx rdr)] (let [form (sci/parse-next (store/get-ctx) rdr)]
(if (= :sci.core/eof form) (if (= :sci.core/eof form)
(do (do
(vreset! !last-ns @sci/ns) (vreset! !last-ns @sci/ns)
res) res)
(recur (sci/eval-form @!sci-ctx form)))))))) (recur (sci/eval-form (store/get-ctx) form))))))))
(defn ^:export eval-string [s] (defn ^:export eval-string [s]
(try (-eval-string s) (try (-eval-string s)
(catch :default e (catch :default e
(error/error-handler e (:src @!sci-ctx)) (error/error-handler e (:src (store/get-ctx)))
(throw e)))) (throw e))))
(defn register-plugin! [_plug-in-name sci-opts] (defn register-plugin! [_plug-in-name sci-opts]
(swap! !sci-ctx sci/merge-opts sci-opts)) (store/swap-ctx! sci/merge-opts sci-opts))
(defn- eval-script-tags* [script-tags] (defn- eval-script-tags* [script-tags]
(when-let [tag (first script-tags)] (when-let [tag (first script-tags)]
@ -84,7 +85,7 @@
(let [response (gobject/get this "response")] (let [response (gobject/get this "response")]
(gobject/set tag "scittle_id" src) (gobject/set tag "scittle_id" src)
;; save source for error messages ;; save source for error messages
(swap! !sci-ctx assoc-in [:src src] response) (store/swap-ctx! assoc-in [:src src] response)
(sci/binding [sci/file src] (sci/binding [sci/file src]
(eval-string response))) (eval-string response)))
(eval-script-tags* (rest script-tags)))))] (eval-script-tags* (rest script-tags)))))]
@ -92,7 +93,7 @@
(if-let [text (not-empty (str/trim (gobject/get tag "textContent")))] (if-let [text (not-empty (str/trim (gobject/get tag "textContent")))]
(let [scittle-id (str (gensym "scittle-tag-"))] (let [scittle-id (str (gensym "scittle-tag-"))]
(gobject/set tag "scittle_id" scittle-id) (gobject/set tag "scittle_id" scittle-id)
(swap! !sci-ctx assoc-in [:src scittle-id] text) (store/swap-ctx! assoc-in [:src scittle-id] text)
(sci/binding [sci/file scittle-id] (sci/binding [sci/file scittle-id]
(eval-string text)) (eval-string text))
(eval-script-tags* (rest script-tags))) (eval-script-tags* (rest script-tags)))

View file

@ -1,9 +1,10 @@
(ns scittle.nrepl (ns scittle.nrepl
(:require (:require
[clojure.edn :as edn] [clojure.edn :as edn]
[sci.ctx-store :as store]
[sci.nrepl.completions :refer [completions]] [sci.nrepl.completions :refer [completions]]
[sci.nrepl.info :refer [info]] [sci.nrepl.info :refer [info]]
[scittle.core :refer [!last-ns eval-string !sci-ctx]])) [scittle.core :refer [!last-ns eval-string]]))
(defn nrepl-websocket [] (defn nrepl-websocket []
(.-ws_nrepl js/window)) (.-ws_nrepl js/window))
@ -29,7 +30,7 @@
:status ["error" "done"]}))))) :status ["error" "done"]})))))
(defn handle-nrepl-info [msg] (defn handle-nrepl-info [msg]
(let [info (info (assoc msg :ctx @!sci-ctx))] (let [info (info (assoc msg :ctx (store/get-ctx)))]
(nrepl-reply msg info))) (nrepl-reply msg info)))
(declare ops) (declare ops)
@ -61,13 +62,13 @@
:eldoc handle-nrepl-info :eldoc handle-nrepl-info
:lookup handle-nrepl-info :lookup handle-nrepl-info
:describe handle-describe :describe handle-describe
:complete (fn [msg] (let [completions (completions (assoc msg :ctx @!sci-ctx))] :complete (fn [msg] (let [completions (completions (assoc msg :ctx (store/get-ctx)))]
(nrepl-reply msg completions)))}) (nrepl-reply msg completions)))})
(defn handle-nrepl-message [msg] (defn handle-nrepl-message [msg]
(if-let [handler (ops (:op msg))] (if-let [handler (ops (:op msg))]
(handler msg) (handler msg)
(nrepl-reply (merge msg {:status ["error" "done"] :err "unkown-op"}) (assoc msg :ctx @!sci-ctx)))) (nrepl-reply (merge msg {:status ["error" "done"] :err "unkown-op"}) (assoc msg :ctx (store/get-ctx)))))
(defn ws-url [host port path] (defn ws-url [host port path]
(str "ws://" host ":" port "/" path)) (str "ws://" host ":" port "/" path))