lua-users home
lua-l archive

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

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).

OK - now I get why I was getting so confused from the example at

There is a distinction between the environment of a function (what is using LUA_ENVIRONINDEX in the example), and the environment of a userdata (what is using lua_getfenv/lua_setfenv in the example). The function environment is set when the library is loaded (from Lua using require "queue"), but the userdata enviroment is set in queue_new(). Nice.