mlua-rs/rlua

Read chunk-local data?

mrd0ll4r opened this issue · 2 comments

Hey,

title might be misleading, but I'm not sure how to better word it. This is the situation:

I have some Lua scripts for environment control (ventilation, lights, ...). The runtime around it is written in Rust. The scripts are loaded (with load and exec) once, and they all follow a pattern like this:

-- Variables
local running = false
local stop_at = 0
local pause_until = 0
local current_mode = 0
-- ...

function setup()
    -- Called once, after the program was loaded. Used to set up some things.
end

function handle_events(something)
    -- If there are events to be handled, this is called before `tick`
end

function tick(now)
    -- Run once every second or so, but regularly
end

This generally works, but I would like to inspect those local variables sometimes for debugging. I probably don't want to change them, and I don't need to look at them while something is executing (tick or handle_events). I tried to find them, but I couldn't. I checked globals() but I guess they're not global (which makes sense, in a way).

Is there a way to inspect these values? I suppose I could make them non-local, and that would work since every program has their own Lua so nothing should conflict... Is there another way?

Hi,
If you create the Lua instance with new_with_debug() you can use the Lua debug library.
I think using name, value = debug.getupvalue(setup, i) (iterate through values of i starting with 1 until you find the one you want) would do the trick.
In theory lua_getupvalue could be exposed in the rlua API (probably unsafe) but I don't think it can do anything that debug.getupvalue() can't do.

I see, thanks!