Moved luadebug to the new structure as well

This commit is contained in:
2025-10-26 13:01:40 +01:00
parent abedca2394
commit 92fd3cabf8
4 changed files with 175 additions and 87 deletions

View File

@ -1,5 +1,12 @@
local core = require "core"
local process = require "process"
local debugger = require "plugins.jpdebug.debugger"
---@class luadebug: runner
local luadebug = debugger.runner:new({
name = "luadebug",
proc = nil ---@type process|nil
})
-- tiny helpers
local function dirname(p) return p:match("^(.*)[/\\]") or "" end
@ -53,18 +60,44 @@ local function spawn_lua_process(entry, lua, cwd, env)
return proc
end
---@class LDB
local LDB = {
name = "luadebug"
}
local function spawn_mdb_process(lua)
local vendor_glob = join(get_plugin_root(), "vendor/?.lua")
local cmd = {}
if type(lua) == "table" then
for i=1, #lua do table.insert(cmd, lua[i]) end
else
table.insert(cmd, lua)
end
table.insert(cmd, "-e")
table.insert(cmd, string.format([[
package.path = %q .. ";" .. package.path
require("mobdebug").listen()
]], vendor_glob))
local proc = process.start(cmd, {
stdout = process.REDIRECT_PIPE,
stderr = process.REDIRECT_PIPE,
})
return proc
end
---@param target table Target table
---@param name string Name of the target to run
---@praam debuginfo table Debugging information
---@return process|nil
function LDB:run(target, name, debuginfo)
function luadebug:mdb_pump()
while true do
core.redraw = true
coroutine.yield(0.016) -- 60FPS
local sout = self.mdb:read_stdout()
local serr = self.mdb:read_stderr()
if sout == nil and serr == nil then
-- mdb exited
break
end
if sout and sout~="" then self.on_stdout("mdb>"..sout) end
if serr and serr~="" then self.on_stdout("mdb> ERROR: "..serr) end
end
end
function luadebug:run(target, name)
if target.entry == nil then
core.error("[jpdebug][luadebug] target.entry is required")
self.error("target.entry is required for "..name)
return
end
local entry = target.entry
@ -72,44 +105,70 @@ function LDB:run(target, name, debuginfo)
local cwd = target.cwd or "."
local env = target.env or {}
-- spwawn the mobdebug process
self.mdb = spawn_mdb_process(lua)
local dbg = self
core.add_thread(function()
luadebug.mdb_pump(dbg)
end)
-- spawn the main lua process
local proc = spawn_lua_process(entry, lua, cwd, env)
if proc == nil then
core.error("[jpdebug][luadebug] Failed to start "..entry)
return nil
self.error("Failed to start "..entry)
return
end
self.proc = proc
return {
luaproc=proc
}
-- 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 and serr == nil then
local exitcode = self.proc:wait(process.WAIT_INFINITE)
self.on_exit(exitcode)
break
end
if sout and sout~="" then self.on_stdout(sout) end
if serr and serr~="" then self.on_stderr(serr) end
end
end)
end
-- Wait untill it ends, possibly with timeout
function LDB:wait(proc, time)
return proc.luaproc:wait(time)
function luadebug:wait(time)
if not self.proc then return end
return self.proc:wait(time)
end
-- Read the stdout, returns nil if process has stopped
function LDB:read_stdout(proc)
local sl = proc.luaproc:read_stdout()
function luadebug:read_stdout()
if not self.proc then return end
local sl = self.proc:read_stdout()
return sl
end
-- Read the stderr
function LDB:read_stderr(proc)
local sl = proc.luaproc:read_stderr()
function luadebug:read_stderr()
if not self.proc then return end
local sl = self.proc:read_stderr()
return sl
end
-- Kill the process
function LDB:kill(proc)
proc.luaproc:kill()
function luadebug:kill()
if self.mdb then
-- Execute ctrl-C twice to exit mobdebug
self.mdb:kill()
self.mdb:kill()
end
if not self.proc then return end
self.proc:kill()
end
-- Terminate the process
function LDB:terminate(proc)
proc.luaproc:terminate()
function luadebug:terminate()
if not self.proc then return end
self.proc:terminate()
end
return LDB
return luadebug