2022-04-10 04:20:18 +00:00
|
|
|
(local util (require :lib.util))
|
|
|
|
(local fennel (require :lib.fennel))
|
2022-04-10 16:03:16 +00:00
|
|
|
(local core (require :core))
|
2022-04-10 04:20:18 +00:00
|
|
|
|
|
|
|
(local replsession (util.hot-table ...))
|
|
|
|
|
|
|
|
(set replsession.sessions {})
|
|
|
|
|
2022-04-10 16:03:16 +00:00
|
|
|
(fn replsession.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)}))
|
|
|
|
|
|
|
|
(fn replsession.restart-session [session]
|
|
|
|
(set session.coro (coroutine.create replsession.session-run))
|
|
|
|
(coroutine.resume session.coro session)
|
|
|
|
session)
|
|
|
|
|
|
|
|
(fn replsession.new-session [] (replsession.restart-session {:submit replsession.submit}))
|
2022-04-10 04:20:18 +00:00
|
|
|
|
|
|
|
(fn replsession.submit [session chunk callback]
|
|
|
|
(assert (= session.callback nil))
|
|
|
|
(set session.callback callback)
|
2022-04-10 16:03:16 +00:00
|
|
|
(match (pcall coroutine.resume session.coro (.. chunk "\n"))
|
|
|
|
(false err) (do (core.log (.. "REPL crashed: " err))
|
|
|
|
(replsession.restart-session session)))
|
2022-04-10 04:20:18 +00:00
|
|
|
(assert (= session.callback callback))
|
|
|
|
(set session.callback nil))
|
|
|
|
|
|
|
|
(fn replsession.activate [id] (set replsession.active-session-id id))
|
|
|
|
(fn replsession.session-id [?id] (or ?id replsession.active-session-id :REPL))
|
|
|
|
(fn replsession.session [?id]
|
|
|
|
(let [id (replsession.session-id ?id)
|
|
|
|
session (or (. replsession.sessions id) (replsession.new-session))]
|
|
|
|
(tset replsession.sessions id session)
|
|
|
|
session))
|
|
|
|
|
|
|
|
replsession.hot
|
|
|
|
|