fennel-xl/localrepl.fnl
2022-12-03 23:26:07 -05:00

33 lines
1.3 KiB
Fennel

(local fennel (require :plugins.fennel-xl.fennel))
(local lume (require :plugins.fennel-xl.lume))
(local util (require :plugins.fennel-xl.util))
(local {: defmethod} (require :plugins.fennel-xl.multimethod))
(local {: submit : restart} (require :plugins.fennel-xl.replsession))
(local core (require :core))
(local localrepl (util.hot-table ...))
(fn localrepl.session-run [session]
(fennel.repl {:readChunk coroutine.yield
; todo: log errors?
:onValues #(pcall session.callback {:vals $1})
:onError #(pcall session.callback {:errType $1 :err $2 :luaSource $3 :traceback (fennel.traceback)})
:pp #$1
:env (lume.clone _G)}))
(defmethod restart :local (fn [session]
(set session.coro (coroutine.create localrepl.session-run))
(coroutine.resume session.coro session)
session))
(defmethod submit :local (fn [session chunk callback ?suppress-crash]
(assert (= session.callback nil))
(set session.callback callback)
(match (pcall coroutine.resume session.coro (.. chunk "\n"))
(false err) (do (when (not ?suppress-crash) (core.log (.. "REPL crashed: " err)))
(restart session)))
(assert (= session.callback callback))
(set session.callback nil)))
localrepl.hot