lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Paul K <paulclinger <at> yahoo.com> writes:

> 
> I came across this issue while investigating a bug in my debugger (MobDebug).
> 
> It looks like debug.setlocal does not correctly restore variables that
> are localized inside subblocks in a function.
> 

Paul,

You are storing the local variables by name, but their identifying key is 
numeric.

Here's how your code plays out:

function f()
    local a = 1
    do
        local a = 2
    end
end

t = {}
t['a'] = 1 -- finds local #1
t['a'] = 2 -- finds local #2, this overrides the value in the table, as I'm sure 
you understand

local #1 = t['a'] -- sets the top a to 2
local #2 = t['a'] -- sets the bottom a to 2

To solve this, simply store by numeric local ID, rather than name.

e.g:
local vars, i = {}, 1
while true do
    local name, value = debug.getlocal(func, i)
    vars[i] = value
    i = i+1
end
-- same idea for restore code

Hope this helps,
Declan.