lua-users home
lua-l archive

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


Brian Hook wrote:
> >You cannot get a table as in C. Instead of that you have to keep that
> >reference inside lua, in a safe place like the registry.
>
> I may be using the wrong nomenclature here, but basically I don't
> want to "Get" the table, only to query a value in that table (or a
> subtable).

Yeah, but you said you wanted to select a table, to be used as a root. So,
you have to keep a reference to that table somehow.

The registry is like any other table, the only difference is that it's used
to store things that belong to the application and that shouldn't be visible
from lua scripts. (It was already available in lua 4.0)

I was suggesting to use the registry to place a reference to your current
table, but you don't really have to, you can store it in the global table or
anywhere else. You can also use regular references (that now are part of the
aux api), but regular references do just that, place you object on the
registry table indexed by a number.

LUA_GLOBALSINDEX, and LUA_REGISTRYINDEX are just pseudo-indices. They work
like indices, and can be used with any function that takes an index. For
example, lua_gettable and lua_pushvalue.

A tipical usage of lua_pushvalue is:

lua_pushvalue(L, -1);

this takes the value at index -1 and pushes it on the stack, so it
duplicates the value on the top of the stack.

lua_pushvalue(L, LUA_GLOBALSINDEX);

takes the global table and pushes it on the stack.

lua_gettable has the same behaviour, so:

lua_gettable(L, LUA_GLOBALSINDEX);

pops the key from the stack, looks up the value of the table at the given
index, and pushes it on the stack. So, this just behaves as the 4.0
function:

lua_getglobals(L);

The new lua_getglobals of 5.0 is a very different thing, and I think it has
been renamed to lua_getfenv or something like that. This function pushes the
environment table of the function. (That's a new feature of lua 5.0)


Hoppe that makes sense,


Ignacio Castaño
castanyo@yahoo.es






___________________________________________________
Yahoo! Móviles
Personaliza tu móvil con tu logo y melodía favorito 
en http://moviles.yahoo.es