|
|
||
|
Am 25.07.2013 00:33 schröbte jseb:
#!/usr/bin/env lua
function foo()
local local_var_foo = true
end
function search_local()
for stack_level = 1,3 do
for loc = 1,5 do
name,value = debug.getlocal(stack_level,loc)
if (name) then
print(string.format(
"%d,%d = %s (%s)",stack_level,loc,name,value))
end
end
end
end
search_local()
Where is «local_var_foo» ?
A local variable is created when a "local" statement is executed, and destroyed when the block containing said "local" statement is left. You never call function `foo()`, so the `local local_var_foo` statement is never executed and the local variable never created.
Try this instead:
function foo()
local local_var_foo = true --> local created here (when foo is
called)
search_local()
end --> local destroyed here
foo()
If you want to examine locals without creating them you need to inspect
the bytecode of the compiled functions using the extra library lbci[1]
(also available via luarocks).
[1]: http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lbci
Could somebody explain to me how «debug.getlocal» works ? Thank you.
HTH, Philipp