4 ms·
Hope this bit of code will be useful to you! You can use it to store the state of a function and restore it later. local function getupvalues(f) lo
by meric 11y ago
Hope this bit of code will be useful to you! You can use it to store the state of a function and restore it later.
local function getupvalues(f)
local state = {}
local i = 1
while true do
local n, v = debug.getupvalue(f, i)
if not n then break end
table.insert(state, v)
i = i + 1
end
return state
end
local function setupvalues(f, state)
for i, value in ipairs(state) do
debug.setupvalue(f, i, value)
end
return state
end
- versteegen 11y agoThanks. Though I was thinking of how to also save/restore the program counter. There is (or was, a few months ago) actually a bug in luajit that debug.setupvalue doesn't work on JIT compiled functions. From my reading of the source, it's not a major problem to fix it, and someone mentioned that they might do so.