app is now live, can communicate with driver

In the process I've also switched to a separate channel for run-time
errors. Now the driver won't struggle to get bindings out of the app
while it's throwing errors.
This commit is contained in:
Kartik K. Agaram 2022-11-27 14:27:04 -08:00
parent ce36bc1f43
commit fdf4722d2a
3 changed files with 29 additions and 54 deletions

16
app.lua
View File

@ -1,7 +1,7 @@
-- main entrypoint for LÖVE
--
-- Most apps can just use the default, but we need to override it to
-- install a test harness.
-- Most apps can just use the default (https://love2d.org/wiki/love.run), but
-- we need to override it to install a test harness.
--
-- A test harness needs to check what the 'real' code did.
-- To do this it needs to hook into primitive operations performed by code.
@ -23,7 +23,9 @@ function love.run()
love.timer.step()
local dt = 0
return function()
-- one iteration of the event loop
-- return nil to continue the event loop, non-nil to quit
App.run_frame = function()
if love.event then
love.event.pump()
for name, a,b,c,d,e,f in love.event.poll() do
@ -45,6 +47,14 @@ function love.run()
love.graphics.present()
love.timer.sleep(0.001)
-- returning nil continues the loop
end
-- protect against runtime errors
return function()
local status, result = xpcall(App.run_frame, live.handle_error)
return result
end
end

View File

@ -35,6 +35,7 @@ function live.initialize(arg)
Live.History = {} -- array of filename roots corresponding to each numeric prefix
Live.Manifest = {} -- mapping from roots to numeric prefixes as of version Live.Head
live.load_files_so_far()
Live.Previous_read = 0
if on.load then on.load() end
end
@ -148,7 +149,7 @@ end
-- look for a message from outside, and return nil if there's nothing
function live.receive()
local f = io.open(love.filesystem.getUserDirectory()..'/_love_akkartik_app')
local f = io.open(love.filesystem.getUserDirectory()..'/_love_akkartik_driver_app')
if f == nil then return nil end
local result = f:read('*a')
f:close()
@ -157,13 +158,13 @@ function live.receive()
print(result)
print(reset_terminal())
-- we can't unlink files, so just clear them
local clear = io.open(love.filesystem.getUserDirectory()..'/_love_akkartik_app', 'w')
local clear = io.open(love.filesystem.getUserDirectory()..'/_love_akkartik_driver_app', 'w')
clear:close()
return result
end
function live.send(msg)
local f = io.open(love.filesystem.getUserDirectory()..'/_love_akkartik_driver', 'w')
local f = io.open(love.filesystem.getUserDirectory()..'/_love_akkartik_app_driver', 'w')
if f == nil then return end
f:write(msg)
f:close()
@ -172,6 +173,16 @@ function live.send(msg)
print(reset_terminal())
end
function live.send_run_time_error(msg)
local f = io.open(love.filesystem.getUserDirectory()..'/_love_akkartik_app_driver_run_time_error', 'w')
if f == nil then return end
f:write(msg)
f:close()
print('=>'..app.color(0, --[[red]]1))
print(msg)
print(app.reset_terminal())
end
-- args:
-- format: 0 for normal, 1 for bold
-- color: 0-15
@ -263,48 +274,6 @@ end
-- ========= on error, pause the app and wait for messages
function live.run()
love.load(love.arg.parseGameArguments(arg), arg)
love.timer.step()
local dt = 0
return function()
local status, result = xpcall(live.try_run, live.handle_error)
return result
end
end
-- one iteration of the event loop
-- return nil to continue the event loop, non-nil to quit
-- from https://love2d.org/wiki/love.run
function live.try_run()
if love.event then
love.event.pump()
for name, a,b,c,d,e,f in love.event.poll() do
if name == 'quit' then
if not love.quit() then
return a or 0
end
end
love.handlers[name](a,b,c,d,e,f)
end
end
-- update
dt = love.timer.step()
love.update(dt)
-- draw before update to give it a chance to mutate state
love.graphics.origin()
love.graphics.clear(love.graphics.getBackgroundColor())
love.draw()
love.graphics.present()
love.timer.sleep(0.001)
-- returning nil continues the loop
end
-- return nil to continue the event loop, non-nil to quit
function live.handle_error(err)
local msg = tostring(err)
@ -319,7 +288,7 @@ function live.handle_error(err)
print('Look in the driver for options to investigate further.')
print("(You probably can't close the app window at this point. If you don't have the driver set up, you might need to force-quit.)")
-- send stack trace to driver and wait for a response
live.send('ERROR '..stack_trace)
live.send_run_time_error(stack_trace)
local buf
repeat
buf = live.receive()

View File

@ -73,11 +73,6 @@ end
function App.resize(w, h)
--? print(("Window resized to width: %d and height: %d."):format(w, h))
App.screen.width, App.screen.height = w, h
Text.redraw_all(Editor_state)
Editor_state.selection1 = {} -- no support for shift drag while we're resizing
Editor_state.right = App.screen.width-Margin_right
Editor_state.width = Editor_state.right-Editor_state.left
Text.tweak_screen_top_and_cursor(Editor_state, Editor_state.left, Editor_state.right)
Last_resize_time = Current_time
if on.resize then on.resize(w,h) end
end
@ -97,6 +92,7 @@ function App.update(dt)
return
end
Cursor_time = Cursor_time + dt
live.update(dt)
if on.update then on.update(dt) end
end