reference to a local function

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

reference to a local function

aryajur
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
Reply | Threaded
Open this post in threaded view
|

Re: reference to a local function

Paul K-2
> 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 <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: reference to a local function

Jonathan Goble
In reply to this post by aryajur
On Sat, Mar 3, 2018 at 8:23 PM Milind Gupta <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

Re: reference to a local function

aryajur
Got it,

Thanks Paul, Jonathan.

Milind

On Sat, Mar 3, 2018 at 5:39 PM, Jonathan Goble <[hidden email]> wrote:
On Sat, Mar 3, 2018 at 8:23 PM Milind Gupta <[hidden email]> 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