lua-users home
lua-l archive

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



On 29 Aug 2006, at 07:57, Jose Luis Hidalgo Valiño wrote:


El 29/08/2006, a las 2:58, Mildred escribió:


For example,
could lua_environindex be used to hold data locally( or private ) to
a C function that can be accessed in successive calls?

I don't know about LUA_ENVIRONINDEX but if you want to keep some
variables between the function call (like static variables in C) you
may find closures useful.

I'm not sure if closures are mutable or not (but it's name suggest they are not mutable), if you want a mutable variable, that can be accessed by a function, you should use one of these pseudo indices:
    - LUA_REGISTRY --> accessible by C code only
- LUA_GLOBALSINDEX ---> the thread environment, for global variables. - LUA_ENVIRONINDEX --> that's new to lua 5.1 and I'm not sure how to use it.

LUA_ENVIRONINDEX seems a way to hold data locally to a set of functions ( or only one ) but without loosing the possibility of access the full global environment (the thread environment) through LUA_GLOBALSINDEX.

In lua 5.1 lua_setfenv "changes" the behavior of LUA_ENVIRONINDEX or LUA_GLOBALSINDEX ? I'm not sure.

It depends whether you apply lua_setfenv to a Lua function or a C function. It has completely different meanings.

applied to a Lua function it changes where that Lua function finds global variables;

applied to a C function it changes its LUA_ENVIRONINDEX table. As if lua_replace(L, LUA_ENVIRONINDEX) was called. This has, of course, got nothing to do with global variables (or environment in the usual sense of the world).

Look at the source code for lua_setfenv, and lua_replace in lapi.c; it's instructive.

drj