Locals in Lua Environments

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

Locals in Lua Environments

Luís Santos
Hello, People

Under the risk of sounding stupid, just a curiosity: is there any reason I should not be able to access the function arguments when I access the function environment via getfenv() ?

My guess would be that this is some colateral effect of the paremeters being local.

I have no doubt that the argument variables should be local - so they won't collide with global environment. But when I am accessing my caller's environment from another function, the lack of access to his arguments looks kinda strange to me.

code:
function service(arg1, arg2)
    x = 1
    y = 2
    local z = 3
    doSomeReallyIntuitiveStuff('anyDebug')
end

function doSomeReallyIntuitiveStuff(msg)
    env = getfenv(2)
    local z = env.x + env.y  -- that´s kind of useful
    -- although env.z is not available (ok)
    -- env.arg1 and
env.arg2 might also be useful here
end


--
Luís Eduardo Jason Santos
Coordenador Técnico
IT Quality Systems
[hidden email]
[21]2242-7959 ramal 219


Reply | Threaded
Open this post in threaded view
|

Re: Locals in Lua Environments

Romulo Bahiense
Luís Santos wrote:
Hello, People

Hi,

Your problem is that local variables are not accessed via function's environment. You have to use the debug library to do so:

function foo( a, b )
    local z = a + b
    bar()
end

function bar( )
    print( debug.getlocal( 2, 1 ) ) --> a	4
    print( debug.getlocal( 2, 2 ) ) --> b	5
    print( debug.getlocal( 2, 3 ) ) --> z	9
    print( debug.getlocal( 2, 4 ) ) --> nil
end

foo( 4, 5 )



Please refer to Lua's documentation about debug.getlocal (and it's friends: debug.setlocal and debug.getinfo)

--rb



Reply | Threaded
Open this post in threaded view
|

Re: Locals in Lua Environments

Tim Hunter
In reply to this post by Luís Santos
Luís Santos wrote:
Hello, People

Under the risk of sounding stupid, just a curiosity: is there any reason I should not be able to access the function arguments when I access the function environment via getfenv() ?

My guess would be that this is some colateral effect of the paremeters being local.

I have no doubt that the argument variables should be local - so they won't collide with global environment. But when I am accessing my caller's environment from another function, the lack of access to his arguments looks kinda strange to me.
I'm using the debug library to do this. debug.getlocal(2,2) gets me the 2nd argument to the caller, for example.

Reply | Threaded
Open this post in threaded view
|

Re: Locals in Lua Environments

Stefan Brantschen

On 29 May 2007, at 00:21, Tim Hunter wrote:

Under the risk of sounding stupid, just a curiosity: is there any reason I should not be able to access the function arguments when I access the function environment via getfenv() ?

My guess would be that this is some colateral effect of the paremeters being local.

I have no doubt that the argument variables should be local - so they won't collide with global environment. But when I am accessing my caller's environment from another function, the lack of access to his arguments looks kinda strange to me.

I'm using the debug library to do this. debug.getlocal(2,2) gets me the 2nd argument to the caller, for example.

But note that this is not recommended as "usual programming tool", ie for anything else but debugging and such.

  => <http://www.lua.org/manual/5.1/manual.html#5.9>
  => PIL II chapter 23

With regards
-- Stefan



Reply | Threaded
Open this post in threaded view
|

Re: Locals in Lua Environments

Mildred Ki'Lya
In reply to this post by Luís Santos
Le Mon 28/05/2007 à 18:25 Luís Santos à écrit:
> I have no doubt that the argument variables should be local - so they 
> won't collide with global environment. But when I am accessing my 
> caller's environment from another function, the lack of access to his 
> arguments looks kinda strange to me.

You can't.

If the function is defined in the global environment, when you do
getfenv() it returns you exactly _G. Not a copy of _G
So having access to function arguments would mean make them global
(because they would be added to _G)

As it is said, use getupvalue for that. And if it's not for debugging,
then you should think your problem another way or use the C API. But I
still thing that having access to caller's environment should be used
only for debugging (or very special cases)

Mildred

-- 
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]


Reply | Threaded
Open this post in threaded view
|

RE: Locals in Lua Environments

Jerome Vuarand-2
Mildred wrote:
> If the function is defined in the global environment, when you do
> getfenv() it returns you exactly _G. Not a copy of _G So having
> access to function arguments would mean make them global (because
> they would be added to _G)  

Having access to function arguments could also mean that a function by
default has its own environment containing only arguments with both
__index and __newindex pointing to its parent environment (usually _G).
Neither solution is backward compatible though.