lua: fix invocation of foreach() with nil type and implicit self
[project/uci.git] / lua / uci.c
index 8ace746..1cb31a5 100644 (file)
--- a/lua/uci.c
+++ b/lua/uci.c
@@ -303,7 +303,7 @@ uci_lua_foreach(lua_State *L)
        ctx = find_context(L, &offset);
        package = luaL_checkstring(L, 1 + offset);
 
-       if (lua_isnil(L, 2))
+       if (lua_isnil(L, 2 + offset))
                type = NULL;
        else
                type = luaL_checkstring(L, 2 + offset);
@@ -994,8 +994,9 @@ luaopen_uci(lua_State *L)
 
        /* create module */
        lua_newtable(L);
+       lua_pushvalue(L, -1);
        luaL_setfuncs(L, uci, 0);
        lua_setglobal(L, MODNAME);
 
-       return 0;
+       return 1;
 }