lua-users home
lua-l archive

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


> I have done some reading and I guess my question is whether
> the following scheme will work?

>     /* get a C function on the stack from the global env somehow */
>     <some code> 
>     lua_getfenv(L, ,-1);

The environment of a C function is always the _current_ C
global environment (much like in Lua 4), so your code (after
loading the chunk) could possibly be replaced by:

    lua_pushvalue(L, LUA_GLOBALSINDEX);
    lua_setfenv(L, -2);
>     /* set the global env back to what it was b4 the lua_call */
>     lua_setfenv(L, -1);

The global (C) env _never changed_ in the first place, so this
is not necessary.  Remember that the lua_setfenv call only
affected the environment of the loaded chunk!