[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: __index overloading only for non-existing keys?
- From: Wim Couwenberg <w.couwenberg@...>
- Date: Thu, 10 Jul 2003 15:17:30 +0200
> I am trying to learn embedding Lua in C++ for a couple of days now and
> I've entered the world of operator overloading using metatables.
Join the club! ;-)
> In the following C++ code I try to overload the __index operator of a
> table 'variables' with a method printing a message. However, the message
> only appears if the key doesn't match any in the 'variables' table: Why
> isn't the indexHandler method called for every indexing operation?
That's right. That changed from Lua 4 to Lua 5. If you
want to trap all index access you can use what is known as
a "proxy": an empty table (that stays empty) while __index
references the "actual" table. By the way, __index will
always be used/called for userdata (if present.)
> Oh, and another question: what is the difference between the globals
> table and the registry table? And which should I use when?
Globals are accessible from both Lua and C, the registry is
only accessible from C. The general idea is to store
C-private data in the registry, to keep it hidden from the
scripting environment. The initial globals table of a new
lua state is the C globals table (i.e. the one at pseudo
index LUA_GLOBALSINDEX). That means that that table can end
up in closures as well (as their environment.) Apart from
that, they're just ordinary tables.
--
Wim