2018-10-17 18:32:39 -07:00
|
|
|
(require "love.event")
|
|
|
|
|
|
|
|
;; This module exists in order to expose stdio over a channel so that it
|
|
|
|
;; can be used in a non-blocking way from another thread.
|
|
|
|
|
2021-09-08 19:41:20 -07:00
|
|
|
(fn prompt [cont?]
|
2022-04-12 09:46:28 -07:00
|
|
|
(io.write (if cont? ".." ">> ")) (io.flush) (.. (tostring (io.read)) "\n"))
|
2018-10-17 18:32:39 -07:00
|
|
|
|
2021-09-08 19:41:20 -07:00
|
|
|
;; This module is loaded twice: initially in the main thread where ... is nil,
|
|
|
|
;; and then again in a separate thread where ... contains the channel used to
|
|
|
|
;; communicate with the main thread.
|
2021-04-08 15:07:02 -07:00
|
|
|
|
2021-09-08 19:41:20 -07:00
|
|
|
(fn looper [event channel]
|
|
|
|
(match (channel:demand)
|
|
|
|
[:write vals] (do (io.write (table.concat vals "\t"))
|
|
|
|
(io.write "\n"))
|
|
|
|
[:read cont?] (love.event.push event (prompt cont?)))
|
|
|
|
(looper event channel))
|
|
|
|
|
|
|
|
(match ...
|
|
|
|
(event channel) (looper event channel))
|
2018-10-17 18:32:39 -07:00
|
|
|
|
|
|
|
{:start (fn start-repl []
|
2020-10-13 23:23:38 -07:00
|
|
|
(let [code (love.filesystem.read "lib/stdio.fnl")
|
2019-12-02 18:30:30 -08:00
|
|
|
luac (if code
|
|
|
|
(love.filesystem.newFileData
|
|
|
|
(fennel.compileString code) "io")
|
|
|
|
(love.filesystem.read "lib/stdio.lua"))
|
|
|
|
thread (love.thread.newThread luac)
|
|
|
|
io-channel (love.thread.newChannel)
|
|
|
|
coro (coroutine.create fennel.repl)
|
2021-09-08 19:41:20 -07:00
|
|
|
options {:readChunk (fn [{: stack-size}]
|
|
|
|
(io-channel:push [:read (< 0 stack-size)])
|
2020-10-13 23:23:38 -07:00
|
|
|
(coroutine.yield))
|
2021-09-08 19:41:20 -07:00
|
|
|
:onValues (fn [vals]
|
|
|
|
(io-channel:push [:write vals]))
|
|
|
|
:onError (fn [errtype err]
|
|
|
|
(io-channel:push [:write [err]]))
|
2020-04-01 21:42:57 -07:00
|
|
|
:moduleName "lib.fennel"}]
|
2018-10-17 18:32:39 -07:00
|
|
|
;; this thread will send "eval" events for us to consume:
|
2019-12-02 18:30:30 -08:00
|
|
|
(coroutine.resume coro options)
|
2021-09-08 19:41:20 -07:00
|
|
|
(thread:start "eval" io-channel)
|
2018-10-17 18:32:39 -07:00
|
|
|
(set love.handlers.eval
|
|
|
|
(fn [input]
|
2021-09-08 19:41:20 -07:00
|
|
|
(coroutine.resume coro input)))))}
|