From f6c031849f47ecad31364beef726e4aa92a20a47 Mon Sep 17 00:00:00 2001 From: Jojojoppe Date: Sat, 25 Oct 2025 18:25:50 +0200 Subject: [PATCH] Some code movement --- .lite_project.lua | 2 +- init.lua | 4 +- runners/luadebug.lua | 114 +++++++++++++++++++++++++------------------ test.lua | 2 +- 4 files changed, 71 insertions(+), 51 deletions(-) diff --git a/.lite_project.lua b/.lite_project.lua index 6b9083d..073027d 100644 --- a/.lite_project.lua +++ b/.lite_project.lua @@ -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" diff --git a/init.lua b/init.lua index db2c293..aa081d5 100644 --- a/init.lua +++ b/init.lua @@ -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) diff --git a/runners/luadebug.lua b/runners/luadebug.lua index 4572fd8..fd3ced3 100644 --- a/runners/luadebug.lua +++ b/runners/luadebug.lua @@ -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 diff --git a/test.lua b/test.lua index 0ab3dc9..696651b 100644 --- a/test.lua +++ b/test.lua @@ -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