Some code movement

This commit is contained in:
2025-10-25 18:25:50 +02:00
parent 8f5d71a6b9
commit f6c031849f
4 changed files with 71 additions and 51 deletions

View File

@ -1,23 +1,58 @@
local core = require "core"
local process = require "process"
-- Dump any table to a string
---@param o any
---@param force bool|nil
---@return nil
---@diagnostic disable-next-line: unused-function
local function dump(o, force)
force = force or false
if type(o) == 'table' or force then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return type(o)..": "..tostring(o)
end
-- tiny helpers
local function dirname(p) return p:match("^(.*)[/\\]") or "" end
local function join(a, b) return (a:sub(-1) == "/" and a or (a .. "/")) .. b end
-- returns absolute path to the plugin root (…/jpdebug)
local function get_plugin_root()
-- debug.getinfo(1, "S").source gives "@/full/path/to/this/file.lua"
local src = debug.getinfo(1, "S").source
if src:sub(1, 1) == "@" then src = src:sub(2) end
local here = dirname(src) -- …/jpdebug/runners
return dirname(here) -- …/jpdebug
end
-- Main lua process spawner
local function spawn_lua_process(entry, lua, cwd, env)
local host = "localhost"
local port = 8172
local nowait = false
-- Resolve vendor/?.lua so "require('mobdebug')" finds the bundled file
local vendor_glob = join(get_plugin_root(), "vendor/?.lua")
-- Build the inline lua launcher
local dbg_call = string.format([[
local ok, m = pcall(require, "mobdebug"); if ok then
print("Connecting to "..%q..":"..tostring(%d))
local connected = m.start(%q, %d)
if not connected and %s then m.off() end
end
]], host, port, host, port, nowait and "true" or "false")
local launcher = string.format([[
package.path = %q .. ";" .. package.path
%s
dofile(%q)
]], vendor_glob, dbg_call, entry)
-- Spawn the process
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, launcher)
local proc = process.start(cmd, {
cwd = cwd,
env = env,
stdout = process.REDIRECT_PIPE,
stderr = process.REDIRECT_PIPE,
})
return proc
end
---@class LDB
@ -35,65 +70,48 @@ function LDB:run(target, name, debuginfo)
return
end
local entry = target.entry
local lua = target.lua or {"lua"}
local lua = target.lua or "lua"
local cwd = target.cwd or "."
local env = target.env or {}
-- Build the inline lua launcher
local dbg_call = string.format([[
print("Here will the call to mobdebug be")
]])
local launcher = string.format([[
%s
dofile(%q)
]], dbg_call, entry)
-- TODO spawn the mobdebugger
-- spawn the main lua process
local proc = spawn_lua_process(entry, lua, cwd, env)
-- Spawn the process
local cmd = lua
-- table.insert(lua, "-e")
table.insert(lua, entry)
local proc = process.start(cmd, {
cwd = cwd,
env = env,
stdout = process.REDIRECT_PIPE,
stderr = process.REDIRECT_PIPE,
})
if proc == nil then
core.error("[jpdebug][luadebug] Failed to start "..entry)
return nil
end
return proc
return {
luaproc=proc,
}
end
-- Wait untill it ends, possibly with timeout
---@param proc process Process object
---@param time any Time field, process.WAIT_INFINITE
function LDB:wait(proc, time)
return proc:wait(time)
return proc.luaproc:wait(time)
end
-- Read the stdout, returns nil if process has stopped
---@param proc process Process object
function LDB:read_stdout(proc)
return proc:read_stdout()
return proc.luaproc:read_stdout()
end
-- Read the stderr
---@param proc process Process object
function LDB:read_stderr(proc)
return proc:read_stderr()
return proc.luaproc:read_stderr()
end
-- Kill the process
---@param proc process Process object
function LDB:kill(proc)
proc:kill()
proc.luaproc:kill()
end
-- Terminate the process
---@param proc process Process object
function LDB:terminate(proc)
proc:terminate()
proc.luaproc:terminate()
end
return LDB