# HG changeset patch # User Tero Marttila # Date 1238630191 -10800 # Node ID 477d1cb3d87cd86181946f87f32b88b0436bd5e8 # Parent 10ada0d1c7d7cfdcf9afb1cfa79b2924247e0e6a implement lua_module_unload and lua_module_gc to release references diff -r 10ada0d1c7d7 -r 477d1cb3d87c src/lua_objs.c --- a/src/lua_objs.c Thu Apr 02 02:55:59 2009 +0300 +++ b/src/lua_objs.c Thu Apr 02 02:56:31 2009 +0300 @@ -358,6 +358,8 @@ /** * Create a lua_module userdata from the given module and push it onto the stack, returning 1. + * + * The given module should be a reference of its own right. */ static int lua_module_create (lua_State *L, struct module *module) { @@ -372,6 +374,19 @@ } /** + * module_put() our module reference + */ +static int lua_module__gc (lua_State *L) +{ + struct lua_module *lua_module = lua_obj_get_obj(L, __func__, "spbot.module"); + + // put it + module_put(lua_module->module); + + return 0; +} + +/** * Parse and apply the given config option value as a raw value to the given module's option */ static int _lua_module_conf_raw_str (lua_State *L, struct lua_module *lua_module, struct nexus *nexus, @@ -456,8 +471,23 @@ return 0; } +static int lua_module_unload (lua_State *L) +{ + struct lua_module *lua_module = lua_obj_get_obj(L, __func__, "spbot.module"); + struct error_info err; + + // just unload it + if ((ERROR_CODE(&err) = module_unload(lua_module->module))) + return luaL_error(L, "module_unload: %s: %s", module_name(lua_module->module), error_msg(&err)); + + // ok + return 0; +} + static const struct luaL_Reg lua_module_methods[] = { + { "__gc", &lua_module__gc }, { "conf", &lua_module_conf }, + { "unload", &lua_module_unload }, { NULL, NULL } }; @@ -522,7 +552,7 @@ info.name = luaL_checkstring(L, 2); info.path = lua_isnoneornil(L, 3) ? NULL : luaL_checkstring(L, 3); - // load + // load and get a new reference if (module_load(lua_modules->modules, &module, &info, &err)) return luaL_error(L, "module_load: %s/%s: %s", info.name, info.path, error_msg(&err)); @@ -538,7 +568,7 @@ // the module name const char *name = luaL_checkstring(L, 2); - // look it up + // look it up, as a new reference if ((module = module_get(lua_modules->modules, name)) == NULL) return luaL_error(L, "module_get: %s: no such module", name);