uci/lua: add explicit close() method
authorDirk Feytons <dirk.feytons@gmail.com>
Thu, 29 Sep 2016 15:39:57 +0000 (17:39 +0200)
committerJohn Crispin <john@phrozen.org>
Thu, 27 Oct 2016 10:37:23 +0000 (12:37 +0200)
This allows a caller to explicitly free the cursor as soon as it is
no longer needed instead of having to wait for the garbage collector.

Signed-off-by: Dirk Feytons <dirk.feytons@gmail.com>
lua/uci.c

index ddb9c5c..22055cb 100644 (file)
--- a/lua/uci.c
+++ b/lua/uci.c
@@ -933,8 +933,19 @@ uci_lua_list_configs(lua_State *L)
 static int
 uci_lua_gc(lua_State *L)
 {
 static int
 uci_lua_gc(lua_State *L)
 {
-       struct uci_context *ctx = find_context(L, NULL);
-       uci_free_context(ctx);
+       struct uci_context **ctx;
+
+       if (!lua_isuserdata(L, 1)) {
+               if (!global_ctx)
+                       return 0;
+               ctx = &global_ctx;
+       } else {
+               ctx = luaL_checkudata(L, 1, METANAME);
+               if (!*ctx)
+                       return 0;
+       }
+       uci_free_context(*ctx);
+       *ctx = NULL;
        return 0;
 }
 
        return 0;
 }
 
@@ -970,6 +981,7 @@ uci_lua_cursor(lua_State *L)
 
 static const luaL_Reg uci[] = {
        { "__gc", uci_lua_gc },
 
 static const luaL_Reg uci[] = {
        { "__gc", uci_lua_gc },
+       { "close", uci_lua_gc },
        { "cursor", uci_lua_cursor },
        { "load", uci_lua_load },
        { "unload", uci_lua_unload },
        { "cursor", uci_lua_cursor },
        { "load", uci_lua_load },
        { "unload", uci_lua_unload },