From: Michael Tremer Date: Thu, 22 Feb 2024 14:14:41 +0000 (+0000) Subject: lua: database: Add __close method X-Git-Tag: 0.9.18~161 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3b06229b16aca6250ec22972c711ff73ff3e72e3;p=location%2Flibloc.git lua: database: Add __close method Signed-off-by: Michael Tremer --- diff --git a/src/lua/database.c b/src/lua/database.c index ee86dda..f5306f9 100644 --- a/src/lua/database.c +++ b/src/lua/database.c @@ -71,13 +71,24 @@ static int Database_open(lua_State* L) { return 1; } -static int Database_gc(lua_State* L) { - Database* self = luaL_checkdatabase(L, 0); +static int Database_close(lua_State* L) { + Database* self = luaL_checkdatabase(L, 1); - if (self->db) + // Free database + if (self->db) { loc_database_unref(self->db); + self->db = NULL; + } + + return 0; +} + +static int Database_gc(lua_State* L) { + Database* self = luaL_checkdatabase(L, 1); + // Free the object free(self); + return 0; } @@ -105,6 +116,7 @@ static int Database_lookup(lua_State* L) { static const struct luaL_Reg database_functions[] = { { "open", Database_open }, { "lookup", Database_lookup }, + { "__close", Database_close }, { "__gc", Database_gc }, { NULL, NULL }, };