lua-users home
lua-l archive

Module finalization (along the lines of calling a function on exit)

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


Searching through the archives I found this post[1] that suggests a way to call
function on exit. How does it work in practice for a module. In other words if
I have code like this:
<C code>
static int z_foo(lua_State *L);
static int z_bar(lua_State *L);
static int z_initialize();
static int z_finalize();
static const struct luaL_reg functions[] = {
 {"foo", 	z_foo},
 {"bar", 	z_bar},
 {"__gc",		z_finalize}, /* <-- does not work */
 {NULL, NULL}
};
Z_API int luaopen_z (lua_State *L) 
{
 int rac = 0, cc = z_initialize();
 if ( cc != Z_OK ) {
 z_decode_and_raise_error(L, cc, "luaopen_z");
 }
 else {
 luaL_module(L, "z", functions, 0);
 rac = 1;
 }
 return rac;
}
</C code>
Where exactly would I set the __gc hook to invoke z_finalize() function call
when the module 'z' gets unloaded. In LuaSocket and as per Diego's post there
is an "__unload" method registered as part of the base libraries, which seems
to be nothing at run time; i.e it never gets called automatically.
Help! 
-- v.a
[1] http://lua-users.org/lists/lua-l/2004-06/msg00195.html

AltStyle によって変換されたページ (->オリジナル) /