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

@ -14,7 +14,7 @@ config.plugins.jpdebug = {
type = "luadebug",
entry = "test.lua",
cwd = ".",
lua = {"C:\\msys64\\msys2_shell.cmd", "-defterm", "-here", "-no-start", "-ucrt64", "-shell", "bash", "-c", "lua"},
lua = {"lua"},
},
},
default_target = "luadebug"

View File

@ -188,14 +188,16 @@ local function run_target(target, name)
end
view:clear()
-- background pump (non-blocking I/O)
core.add_thread(function()
while true do
core.redraw = true
coroutine.yield(0.016) -- ~60fps
if running_proc == nil then
return
end
local out = running_proc:read_stdout()
local out = runner:read_stdout(running_proc)
if out == nil then
-- stdout pipe closed: try drain stderr and break when both closed
local err = runner:read_stderr(running_proc)

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) .. ','
-- 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
return s .. '} '
-- 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
return type(o)..": "..tostring(o)
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

View File

@ -1,5 +1,5 @@
print("Starting loop: test 123")
for i = 1,10 do
for i = 1,5 do
print("i = ", i) -- we'll stop here
require("socket").sleep(1)
end