lua-users home
lua-l archive

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


Got it,

Thanks Paul, Jonathan.

Milind

On Sat, Mar 3, 2018 at 5:39 PM, Jonathan Goble <jcgoble3@gmail.com> wrote:
On Sat, Mar 3, 2018 at 8:23 PM Milind Gupta <milind.gupta@gmail.com> wrote:
Hi,
     I have the following code snippet:
---------------------------------------------------

local function add(a,b)
return a + b
end

print(_ENV["add"],_G["add"],add)

for k,v in pairs(_ENV) do
print(k,v)
end
print("-------------------")


print(debug.getupvalue(debug.getinfo(1).func,1))
------------------------------------------------------

I am trying to find out where "add" function is stored. Since it is defined local it was not there in _ENV and _G. I also cannot find it in the upvalues. Where is the function?

Thanks,
Milind

There's no way to dynamically look up locals via table syntax, because they aren't stored in a table. Locals are stored directly in the internal registers used by the VM. Without the debug library, the only way to access a local is by directly naming it in the code.

That said, debug.getlocal() does exist [1], but this does not allow lookup by name, only by the internal number. You would have to enumerate all locals to find the one you want.

[1] https://www.lua.org/manual/5.3/manual.html#pdf-debug.getlocal