   about   lua_replace(L,LUA_ENVIRONINDEX)
  I'm confused by the statement   lua_replace(L,LUA_ENVIRONINDEX)what does it mean?
  It replaces the "ENVIRONINDEX" with the value at the top of the lua stack, popping that value in the process.The ENVIRONINDEX is a special table representing the "environment" of the C function:Quote:The environment of the running C function is always at pseudo-index LUA_ENVIRONINDEX.The "environment" is where non-local variables live. It can be modified by using setfenv():Quote:All global variables live as fields in ordinary Lua tables, called environment tables or simply environments (see §2.9). Each function has its own reference to an environment, so that all global variables in this function will refer to this environment table. When a function is created, it inherits the environment from the function that created it. To get the environment table of a Lua function, you call getfenv. To replace it, you call setfenv. (You can only manipulate the environment of C functions through the debug library; (see §5.9).)A common way of using the environment is to create a new one, and load a file into it (in Lua, loading a file is like running a special function). This way the file cannot alter global state, it gets captured into the environment created for it. Using this you can ensure different scripts cannot spoil each others data. You will need to work out your own method for cross environment communication.If you are new to Lua, you probably don't need to worry about this just yet.lua_replace()LUA_ENVIRONINDEX;  The current alpha for Lua 5.2 has removed the pseudo index and it's associated functions.Quote:Pseudoindex LUA_ENVIRONINDEX and functions lua_getfenv/lua_setfenv were removed. C functions do not have environments any more. Use an upvalue with a shared table if you need to keep shared state among several C functions. (You may use luaL_setfuncs to open a C library with all functions sharing a common upvalue.)http://www.lua.org/work/doc/manual.html#8   ;ok thanks guys, but i'm still confused by the codes segement from gpg6 as followingvoid init (const char* tname, const char* bname,   const luaL_reg *flist, int (*destroy) (lua_State*)) { lua_newtable(L);  // create table for uniquenesslua_pushstring(L,"v");lua_setfield(L,-2,"__mode");// set as weak-value tablelua_pushvalue(L,-1);   // duplicate tablelua_setmetatable(L,-2);// set itself as metatablelua_replace(L,LUA_ENVIRONINDEX); // set table as env tableluaL_register(L,tname,flist);    // create libtable }so,if i'm correct lua_replace(L,LUA_ENVIRONINDEX) set the table at top stack as env table ,when calling luaL_register(L,tname,flist),the functions in table tname use the special table as envoriment table.if i'm wrong,please correct me! 