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

index 5a6c68ada06a635029f0e6ca5c38cbdeffb5e52b..2a75f75dd922655997e14faba892c4bd2291a03d 100644 (file)
@@ -72,13 +72,24 @@ static int Network_new(lua_State* L) {
        return r;
 }
 
-static int Network_gc(lua_State* L) {
-       Network* self = luaL_checknetwork(L, 0);
+static int Network_close(lua_State* L) {
+       Network* self = luaL_checknetwork(L, 1);
 
-       if (self->network)
+       // Free the network
+       if (self->network) {
                loc_network_unref(self->network);
+               self->network = NULL;
+       }
+
+       return 0;
+}
 
+static int Network_gc(lua_State* L) {
+       Network* self = luaL_checknetwork(L, 1);
+
+       // Free the object
        free(self);
+
        return 0;
 }
 
@@ -139,6 +150,7 @@ static const struct luaL_Reg Network_functions[] = {
        { "get_asn", Network_get_asn },
        { "get_family", Network_get_family },
        { "get_country_code", Network_get_country_code },
+       { "__close", Network_close },
        { "__gc", Network_gc },
        { "__tostring", Network_tostring },
        { NULL, NULL },