Some code movement
This commit is contained in:
@ -14,7 +14,7 @@ config.plugins.jpdebug = {
|
|||||||
type = "luadebug",
|
type = "luadebug",
|
||||||
entry = "test.lua",
|
entry = "test.lua",
|
||||||
cwd = ".",
|
cwd = ".",
|
||||||
lua = {"C:\\msys64\\msys2_shell.cmd", "-defterm", "-here", "-no-start", "-ucrt64", "-shell", "bash", "-c", "lua"},
|
lua = {"lua"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
default_target = "luadebug"
|
default_target = "luadebug"
|
||||||
|
|||||||
4
init.lua
4
init.lua
@ -188,14 +188,16 @@ local function run_target(target, name)
|
|||||||
end
|
end
|
||||||
|
|
||||||
view:clear()
|
view:clear()
|
||||||
|
|
||||||
-- background pump (non-blocking I/O)
|
-- background pump (non-blocking I/O)
|
||||||
core.add_thread(function()
|
core.add_thread(function()
|
||||||
while true do
|
while true do
|
||||||
|
core.redraw = true
|
||||||
coroutine.yield(0.016) -- ~60fps
|
coroutine.yield(0.016) -- ~60fps
|
||||||
if running_proc == nil then
|
if running_proc == nil then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local out = running_proc:read_stdout()
|
local out = runner:read_stdout(running_proc)
|
||||||
if out == nil then
|
if out == nil then
|
||||||
-- stdout pipe closed: try drain stderr and break when both closed
|
-- stdout pipe closed: try drain stderr and break when both closed
|
||||||
local err = runner:read_stderr(running_proc)
|
local err = runner:read_stderr(running_proc)
|
||||||
|
|||||||
@ -1,23 +1,58 @@
|
|||||||
local core = require "core"
|
local core = require "core"
|
||||||
local process = require "process"
|
local process = require "process"
|
||||||
|
|
||||||
-- Dump any table to a string
|
-- tiny helpers
|
||||||
---@param o any
|
local function dirname(p) return p:match("^(.*)[/\\]") or "" end
|
||||||
---@param force bool|nil
|
local function join(a, b) return (a:sub(-1) == "/" and a or (a .. "/")) .. b end
|
||||||
---@return nil
|
|
||||||
---@diagnostic disable-next-line: unused-function
|
-- returns absolute path to the plugin root (…/jpdebug)
|
||||||
local function dump(o, force)
|
local function get_plugin_root()
|
||||||
force = force or false
|
-- debug.getinfo(1, "S").source gives "@/full/path/to/this/file.lua"
|
||||||
if type(o) == 'table' or force then
|
local src = debug.getinfo(1, "S").source
|
||||||
local s = '{ '
|
if src:sub(1, 1) == "@" then src = src:sub(2) end
|
||||||
for k,v in pairs(o) do
|
local here = dirname(src) -- …/jpdebug/runners
|
||||||
if type(k) ~= 'number' then k = '"'..k..'"' end
|
return dirname(here) -- …/jpdebug
|
||||||
s = s .. '['..k..'] = ' .. dump(v) .. ','
|
end
|
||||||
end
|
|
||||||
return s .. '} '
|
-- Main lua process spawner
|
||||||
else
|
local function spawn_lua_process(entry, lua, cwd, env)
|
||||||
return type(o)..": "..tostring(o)
|
local host = "localhost"
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
---@class LDB
|
---@class LDB
|
||||||
@ -35,65 +70,48 @@ function LDB:run(target, name, debuginfo)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
local entry = target.entry
|
local entry = target.entry
|
||||||
local lua = target.lua or {"lua"}
|
local lua = target.lua or "lua"
|
||||||
local cwd = target.cwd or "."
|
local cwd = target.cwd or "."
|
||||||
local env = target.env or {}
|
local env = target.env or {}
|
||||||
|
|
||||||
-- Build the inline lua launcher
|
-- TODO spawn the mobdebugger
|
||||||
local dbg_call = string.format([[
|
|
||||||
print("Here will the call to mobdebug be")
|
-- spawn the main lua process
|
||||||
]])
|
local proc = spawn_lua_process(entry, lua, cwd, env)
|
||||||
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
|
if proc == nil then
|
||||||
core.error("[jpdebug][luadebug] Failed to start "..entry)
|
core.error("[jpdebug][luadebug] Failed to start "..entry)
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
return proc
|
|
||||||
|
return {
|
||||||
|
luaproc=proc,
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Wait untill it ends, possibly with timeout
|
-- 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)
|
function LDB:wait(proc, time)
|
||||||
return proc:wait(time)
|
return proc.luaproc:wait(time)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Read the stdout, returns nil if process has stopped
|
-- Read the stdout, returns nil if process has stopped
|
||||||
---@param proc process Process object
|
|
||||||
function LDB:read_stdout(proc)
|
function LDB:read_stdout(proc)
|
||||||
return proc:read_stdout()
|
return proc.luaproc:read_stdout()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Read the stderr
|
-- Read the stderr
|
||||||
---@param proc process Process object
|
|
||||||
function LDB:read_stderr(proc)
|
function LDB:read_stderr(proc)
|
||||||
return proc:read_stderr()
|
return proc.luaproc:read_stderr()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Kill the process
|
-- Kill the process
|
||||||
---@param proc process Process object
|
|
||||||
function LDB:kill(proc)
|
function LDB:kill(proc)
|
||||||
proc:kill()
|
proc.luaproc:kill()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Terminate the process
|
-- Terminate the process
|
||||||
---@param proc process Process object
|
|
||||||
function LDB:terminate(proc)
|
function LDB:terminate(proc)
|
||||||
proc:terminate()
|
proc.luaproc:terminate()
|
||||||
end
|
end
|
||||||
|
|
||||||
return LDB
|
return LDB
|
||||||
|
|||||||
Reference in New Issue
Block a user