[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: reference to a local function
- From: Paul K <paul@...>
- Date: Sat, 3 Mar 2018 17:37:52 -0800
> 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?
It's not an upvalue, it's a regular local value, so you'd need to use
`debug.getlocal` instead (iterate over values and find the one with
the name you are looking for). Something like this should work:
i = 1
while true do
local name, value = debug.getlocal(1, i)
if not name then break end
print(name, value)
i = i + 1
end
Paul.
On Sat, Mar 3, 2018 at 5:22 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