]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
lua: database: Add __close method
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 22 Feb 2024 14:14:41 +0000 (14:14 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 22 Feb 2024 14:14:41 +0000 (14:14 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/lua/database.c

index ee86ddad97e4bb29267765cc75bac52b971b0c99..f5306f984c6d8d45451a078a92f25de5d1f564c2 100644 (file)
@@ -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 },
 };