Single pump for both lua and mdb outputs

This commit is contained in:
U-ENGINEERO\joppe.blondel
2025-11-20 12:36:55 +01:00
parent fa0c8d5a2c
commit d654e1776a
3 changed files with 79 additions and 37 deletions

View File

@ -79,6 +79,16 @@ function debugger.on_state(state)
debugger.state = state
end
function debugger.on_break(file, line, reason)
debugger.log(string.format("breakpoint hit: %s:%d", file, line))
-- Temporary continue at unknown breaks
if file=='?' and line==-1 then
if debugger.debugrunner.caps.can_continue then
debugger.debugrunner:continue()
end
end
end
function debugger.run(target, name, r, view)
if debugger.debugrunner then
if debugger.state == "paused" then
@ -101,10 +111,7 @@ function debugger.run(target, name, r, view)
on_stderr = debugger.on_stderr,
on_exit = debugger.on_exit,
on_state = debugger.on_state,
on_break = function(file, line, reason)
debugger.log(string.format("breakpoint hit: %s:%d", file, line))
end,
on_break = debugger.on_break,
})
-- And run
debugger.debugrunner:run(target, name)

View File

@ -15,12 +15,29 @@ local function get_plugin_root()
return dirname(here) -- …/jpdebug
end
-- Check if string starts with a substring
local function starts_with(str, word)
return str:sub(1, #word) == word
end
-- ------------------- Runner API --------------------------------
---@class luadebug: runner
local luadebug = debugger.runner:new({
name = "luadebug",
proc = nil ---@type process|nil
proc = nil, ---@type process|nil
caps = {
can_pause = false,
can_continue = true,
can_step_in = true,
can_step_over = true,
can_step_out = true,
can_breakpoints = true,
has_stack = true,
has_locals = true,
can_eval = false,
},
})
function luadebug:run(target, name)
@ -48,24 +65,13 @@ function luadebug:run(target, name)
self.on_state('connecting')
-- output pump
core.add_thread(function()
while true do
core.redraw = true
coroutine.yield(0.016) -- 60FPS
local sout = self.proc:read_stdout()
local serr = self.proc:read_stderr()
if sout == nil or serr == nil then
-- Make sure to read stderr for the last time
if serr and serr~="" then self.on_stderr(serr) end
local exitcode = self.proc:wait(process.WAIT_INFINITE)
self.on_exit(exitcode)
break
-- Start output pump
self:pumps()
end
if sout and sout~="" then self.on_stdout(sout) end
if serr and serr~="" then self.on_stderr(serr) end
end
end)
function luadebug:continue()
self.mdb:write('run\n')
self.on_state('running')
end
function luadebug:wait(time)
@ -171,28 +177,57 @@ function luadebug:spawn_mdb(lua)
end
self.mdb = proc
end
self.mdb:write('run\n')
-- output pump
-- ---------------- Pumps ---------------------------------------
function luadebug:pumps()
core.add_thread(function()
while true do
core.redraw = true
coroutine.yield(0.016) -- 60FPS
local sout = self.mdb:read_stdout()
local serr = self.mdb:read_stderr()
-- LUA PROGRAM OUTPUT
if true then
local sout = self.proc:read_stdout()
local serr = self.proc:read_stderr()
if sout == nil or serr == nil then
-- Make sure to read stderr for the last time
if serr and serr~="" then self.on_stderr(serr) end
local exitcode = self.mdb:wait(process.WAIT_INFINITE)
local exitcode = self.proc:wait(process.WAIT_INFINITE)
self.on_exit(exitcode)
break
end
if sout and sout~="" then self.on_stdout('mdb> '..sout) end
if serr and serr~="" then self.on_stderr('mdb> '..serr) end
if sout and sout~="" then self.on_stdout(sout) end
if serr and serr~="" then self.on_stderr(serr) end
end
-- MDB OUTPUT
if true then
local sout = self.mdb:read_stdout()
local serr = self.mdb:read_stderr()
if sout == nil or serr == nil then
-- Make sure to read stderr for the last time
if serr and serr~="" then self.on_stderr(serr) end
local exitcode = self.proc:wait(process.WAIT_INFINITE)
self.on_exit(exitcode)
break
end
if sout and sout~="" then self.on_stdout("mdb> "..sout) end
if serr and serr~="" then self.on_stderr("mdb> "..serr) end
if sout and sout~="" then
-- Check output
if starts_with(sout, "Paused") then
self.on_state('paused')
self.on_break('?', -1, 'paused')
end
end
end
end
end)
end
return luadebug
return luadebug