100 lines
2.2 KiB
Lua
100 lines
2.2 KiB
Lua
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
|
|
end
|
|
|
|
---@class LDB
|
|
local LDB = {
|
|
name = "luadebug"
|
|
}
|
|
|
|
---@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)
|
|
if target.entry == nil then
|
|
core.error("[jpdebug][luadebug] target.entry is required")
|
|
return
|
|
end
|
|
local entry = target.entry
|
|
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)
|
|
|
|
-- 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
|
|
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)
|
|
end
|
|
|
|
-- Read the stdout, returns nil if process has stopped
|
|
---@param proc process Process object
|
|
function LDB:read_stdout(proc)
|
|
return proc:read_stdout()
|
|
end
|
|
|
|
-- Read the stderr
|
|
---@param proc process Process object
|
|
function LDB:read_stderr(proc)
|
|
return proc:read_stderr()
|
|
end
|
|
|
|
-- Kill the process
|
|
---@param proc process Process object
|
|
function LDB:kill(proc)
|
|
proc:kill()
|
|
end
|
|
|
|
-- Terminate the process
|
|
---@param proc process Process object
|
|
function LDB:terminate(proc)
|
|
proc:terminate()
|
|
end
|
|
|
|
return LDB
|