Files
jpdebug/debugger.lua

74 lines
1.7 KiB
Lua

local core = require "core"
---@class runner
local runner = {
new = function(self, o) end, ---@meta
run = function(self, target, name) end, ---@meta
wait = function(sefl, time) end, ---@meta
kill = function(self) end, ---@meta
terminate = function(self) end, ---@meta
}
local debugger = {}
local debugwindow = nil ---@type JPDebugView
local debugrunner = nil ---@type runner|nil
function debugger.log(msg)
core.log("[jpdebug][debugger] %s", msg)
if debugwindow then debugwindow:push("meta", "debugger] "..msg) end
end
function debugger.error(msg)
core.error("[jpdebug][debugger]"..msg)
if debugwindow then debugwindow:push("meta", "debugger] ERROR: "..msg) end
end
function debugger.stdout(msg)
if debugwindow then debugwindow:push("stdout", msg) end
end
function debugger.stderr(msg)
if debugwindow then debugwindow:push("stderr", msg) end
end
function debugger.is_running()
return debugrunner~=nil
end
function debugger.run(target, name, r, view)
debugwindow = view
debugwindow:clear()
debugger.log(string.format("Running %s", name))
-- Create new runner object
debugrunner = r:new({
log = debugger.log,
error = debugger.error,
stdout = debugger.stdout,
stderr = debugger.stderr,
exited = debugger.exited,
})
-- And run
debugrunner:run(target, name)
end
function debugger.stop()
if debugrunner then
debugrunner:kill()
local exitcode = debugrunner:wait(1000)
-- TODO terminate if needed
debugger.log(string.format("... Stoped: %d", exitcode))
end
debugrunner = nil
end
function debugger.exited()
if debugrunner then
local exitcode = debugrunner:wait(process.WAIT_INFINITE)
debugger.log(string.format("exit: %d", exitcode))
end
debugrunner = nil
end
return debugger