lua-users home
lua-l archive

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


Hello,

According to this:
http://www.lua.org/manual/5.2/manual.html#pdf-debug.getlocal

it seems that we can get index value to local variables.
(i've understood it's only legitimate for debugging purposes)
The explanations given in the link above are very obscure for me (and
reading the PiL didn't help).

I did a little sample program for testing, but i cannot manage to get
the index of the local variable declared in the «foo» function.


Here is my test code :


#!/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()


And the results:

1,1 = (for index) (1)
1,2 = (for limit) (3)
1,3 = (for step) (1)
1,4 = stack_level (1)
1,5 = (for index) (5)
3,1 = (*temporary) (2)
3,2 = (*temporary) (userdata: 0x7fffd4335b88)
3,3 = (*temporary) (function: 0x4046d0)

Where is «local_var_foo» ?

Could somebody explain to me how «debug.getlocal» works ?


Thank you.