[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: In praise of globals
- From: Miles Bader <miles@...>
- Date: Thu, 18 Apr 2013 10:51:35 +0900
Steve Litt <slitt@troubleshooters.com> writes:
> That didn't work out quite as well as I'd hoped. It works just fine
> with a global table named t, but when I change it to _G, it doesn't
> work. Here's the program:
...
> You don't see the metatable side-effects when working with _G.
Actual accesses to global variables in Lua don't go via _G -- _G just
exists for user convenience, it's not actually part of Lua's internal
mechanism -- so assigning to _G won't affect them. Global variable
access uses the environment pointer (_ENV in 5.2 or the "fenv" in
5.1).
You can either (1) set the metatable of the original _G directly (and
use rawget/rawset to access the actual values without infinite
looping), or (2) use a more appropriate mechanism to change the
environment pointer to point to your proxy (set _ENV in 5.2, or use
setfenv in 5.1).
-miles
--
Omochiroi!
- References:
- In praise of globals, Dirk Laurie
- Re: In praise of globals, Thomas Jericke
- Re: In praise of globals, Dirk Laurie
- Re: In praise of globals, Thomas Jericke
- Re: In praise of globals, Dirk Laurie
- Re: In praise of globals, Thomas Jericke
- Re: In praise of globals, Dirk Laurie
- Re: In praise of globals, Steve Litt
- Re: In praise of globals, Dirk Laurie
- Re: In praise of globals, Steve Litt