lua-users home
lua-l archive

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


2008/4/7, Abhinav Lele <abhinav.lele@gmail.com>:
> I registered a C function in Lua using
>  lua_pushstring("fn");
>  lua_pushcfunction(fn);
>  lua_settable(L,LUA_GLOBALSINDEX);
>
>  how i can do the reverse , i.e. remove the function from lua environment

Just set the variable to nil. The closure will eventually get collected.

lua_pushstring(L, "fn");
lua_pushnil(L);
lua_settable(L, LUA_GLOBALSINDEX);

Note that there are several functions in the Lua API to ease
manipulation of tables. The above could be rewritten in a shorter form
as :

lua_pushnil(L);
lua_setfield(L, LUA_GLOBALSINDEX, "fn");

or even shorter:

lua_pushnil(L);
lua_setglobal(L, "fn");