lua-users home
lua-l archive

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


> 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