lua-users home
lua-l archive

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


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!