lua-users home
lua-l archive

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

> Unlike __gettable, __index is only called when the table does not have
> that element. If you want to call __index (and __newindex) on *every*
> access to a table, you must make sure the table is empty. (For instance,
> we can store the elements in another table, and the empty talbe works as
> a proxy to the "real" table.)

Right, I understand that.

The problem is, after installing __index and __newindex calls for the global
table, if I execute:

    print( XYZ )

    XYZ = 1

    print( XYZ )

Then the __index is called for the first print, and I return a value.  Then
__newindex does not seem to be called for XYZ = 1, nor is __index called for
the second print.  I suppose it now exists in the table, as it was set to 1.
But if my __newindex code was called, then it would not have set the value
into the table, as it would have identified XYZ as a 'magic' global.

I could set up a proxy table on the globals I guess, but in theory I
shouldn't have to.  I cannot seem to get my __newindex to be called at all
for the global table.  Perhaps I'm doing something wrong... here's the setup
for it:

// Intercept global table access


lua_pushstring(L, "__index");

lua_pushcfunction(L, global_index);

lua_settable(L, -3);

lua_pushstring(L, "__newindex");

lua_pushcfunction(L, global_newindex);

lua_settable(L, -3);

lua_setmetatable(L, LUA_GLOBALSINDEX);


Love, Light and Peace,
- Peter Loveday
Director of Development, eyeon Software