]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lua: Add a function to get a reference on a table in the stack
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 25 Feb 2020 09:20:04 +0000 (10:20 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Thu, 12 Aug 2021 06:57:07 +0000 (08:57 +0200)
The hlua_checktable() function may now be used to create and return a
reference on a table in stack, given its position. This function ensures it
is really a table and throws an exception if not.

This patch is mandatory to allow the support of the filters written in lua.

src/hlua.c

index 21786a1d1eb4ffc02f5610573811dcd2a52ab8f6..e7bb223b45b2a4bf0780907a4c7e28ca123c5f0e 100644 (file)
@@ -350,6 +350,20 @@ __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
        return luaL_ref(L, LUA_REGISTRYINDEX);
 }
 
+/* Used to check an Lua table type in the stack. It creates and
+ * returns a reference of the table. This function throws an
+ * error if the rgument is not a "table".
+ */
+__LJMP unsigned int hlua_checktable(lua_State *L, int argno)
+{
+       if (!lua_istable(L, argno)) {
+               const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno));
+               WILL_LJMP(luaL_argerror(L, argno, msg));
+       }
+       lua_pushvalue(L, argno);
+       return luaL_ref(L, LUA_REGISTRYINDEX);
+}
+
 /* Return the string that is of the top of the stack. */
 const char *hlua_get_top_error_string(lua_State *L)
 {