From c66357b1d16ba0b39419a8b305b25b7ca45c19a7 Mon Sep 17 00:00:00 2001 From: Takahiro Yamashita Date: Sun, 13 Aug 2023 08:35:28 +0900 Subject: [PATCH 1/4] tests: internal: lua: add test for flb_lua_arraylength Signed-off-by: Takahiro Yamashita --- tests/internal/lua.c | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/internal/lua.c b/tests/internal/lua.c index d71b462fad2..79071ae1792 100644 --- a/tests/internal/lua.c +++ b/tests/internal/lua.c @@ -204,6 +204,77 @@ static void test_tompack() lua_close(l); } + +static void test_lua_arraylength() +{ + lua_State *l; + int i; + int len; + int size = 10; + + l = luaL_newstate(); + if (!TEST_CHECK(l != NULL)) { + TEST_MSG("luaL_newstate faild"); + return; + } + luaL_openlibs(l); + + /* create array. */ + lua_createtable(l, size, 0); + + for (i=1; i<=size; i++) { + lua_pushinteger(l, i); /* set an index of array */ + lua_pushinteger(l, 3+i); /* set a value */ + lua_settable(l, -3); /* points created table */ + } + + len = flb_lua_arraylength(l); + if (!TEST_CHECK(len == size)) { + TEST_MSG("size error. got=%d expect=%d", len, size); + } + lua_pop(l, 1); + + lua_close(l); +} + +static void test_lua_arraylength_for_array_contains_nil() +{ + lua_State *l; + int i; + int len; + int size = 10; + + l = luaL_newstate(); + if (!TEST_CHECK(l != NULL)) { + TEST_MSG("luaL_newstate faild"); + return; + } + luaL_openlibs(l); + + /* create array. */ + lua_createtable(l, size, 0); + + for (i=1; i<=size; i++) { + lua_pushinteger(l, i); /* set an index of array */ + if (i == 7) { + lua_pushnil(l); + } + else { + lua_pushinteger(l, 3+i); /* set a value */ + } + lua_settable(l, -3); /* points created table */ + } + + len = flb_lua_arraylength(l); + if (!TEST_CHECK(len == size)) { + TEST_MSG("size error. got=%d expect=%d", len, size); + } + lua_pop(l, 1); + + lua_close(l); +} + + TEST_LIST = { { "lua_is_valid_func" , test_is_valid_func}, { "lua_pushtimetable" , test_pushtimetable}, @@ -211,5 +282,7 @@ TEST_LIST = { { "lua_pushmpack" , test_pushmpack }, { "lua_tomsgpack" , test_tomsgpack }, { "lua_tompack" , test_tompack }, + { "lua_arraylength" , test_lua_arraylength }, + { "lua_arraylength_for_array_contains_nil", test_lua_arraylength_for_array_contains_nil}, { 0 } }; From 9de5f8a1ea3bd68e5c1f893cf918589e9c073c5c Mon Sep 17 00:00:00 2001 From: Takahiro Yamashita Date: Sun, 13 Aug 2023 09:51:13 +0900 Subject: [PATCH 2/4] lua: add flb_lua_dump_stack Signed-off-by: Takahiro Yamashita --- include/fluent-bit/flb_lua.h | 14 ++++++ src/flb_lua.c | 84 ++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/include/fluent-bit/flb_lua.h b/include/fluent-bit/flb_lua.h index 390a3bcba10..5b9dfa7d573 100644 --- a/include/fluent-bit/flb_lua.h +++ b/include/fluent-bit/flb_lua.h @@ -47,6 +47,19 @@ struct flb_lua_l2c_config { struct mk_list l2c_types; /* data types (lua -> C) */ }; +/* convert from negative index to positive index */ +static inline int flb_lua_absindex(lua_State *l , int index) +{ +#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM < 520 + if (index < 0) { + index = lua_gettop(l) + index + 1; + } +#else + index = lua_absindex(l, index); +#endif + return index; +} + int flb_lua_arraylength(lua_State *l); void flb_lua_pushtimetable(lua_State *l, struct flb_time *tm); int flb_lua_is_valid_func(lua_State *l, flb_sds_t func); @@ -60,5 +73,6 @@ void flb_lua_tompack(lua_State *l, mpack_writer_t *writer, int index, struct flb_lua_l2c_config *l2cc); +void flb_lua_dump_stack(FILE *out, lua_State *l); #endif diff --git a/src/flb_lua.c b/src/flb_lua.c index c6badec16bc..cd0988b7d86 100644 --- a/src/flb_lua.c +++ b/src/flb_lua.c @@ -584,3 +584,87 @@ void flb_lua_tomsgpack(lua_State *l, break; } } + +static void print_lua_value(FILE *out, lua_State *l, int index, int depth) +{ + int i; + int i_depth; + int type; + size_t len_s; + double val_d; + int64_t val_i; + int len_t; + + index = flb_lua_absindex(l, index); + + type = lua_type(l, index); + fprintf(out, "%s:", lua_typename(l, type)); + switch(type){ + case LUA_TSTRING: + fprintf(out, " %s\n", lua_tolstring(l,index, &len_s)); + break; + case LUA_TBOOLEAN: + fprintf(out, " %s\n", lua_toboolean(l, index) ? "true":"false"); + break; + case LUA_TNUMBER: + val_i = lua_tointeger(l, index); + val_d = lua_tonumber(l, index); + fprintf(out, " d=%lf i=%ld\n", val_d, val_i); + break; + case LUA_TTABLE: + len_t = flb_lua_arraylength(l); + fprintf(out, " size=%d ", len_t); + if (len_t > 0) { + fprintf(out, "array\n"); + for (i=1; i<=len_t; i++) { + for (i_depth=0; i_depth=1; i--) { + fprintf(out, "%03d: ", i); + print_lua_value(out, l, i, 2); + } + fprintf(out, "======\n"); +} From 9260b4eb005e83888cee3759368c63d1c6e3994b Mon Sep 17 00:00:00 2001 From: Takahiro Yamashita Date: Sun, 13 Aug 2023 10:10:57 +0900 Subject: [PATCH 3/4] lua: pass index to lua_arraylength Signed-off-by: Takahiro Yamashita --- include/fluent-bit/flb_lua.h | 2 +- src/flb_lua.c | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/include/fluent-bit/flb_lua.h b/include/fluent-bit/flb_lua.h index 5b9dfa7d573..b2787430284 100644 --- a/include/fluent-bit/flb_lua.h +++ b/include/fluent-bit/flb_lua.h @@ -60,7 +60,7 @@ static inline int flb_lua_absindex(lua_State *l , int index) return index; } -int flb_lua_arraylength(lua_State *l); +int flb_lua_arraylength(lua_State *l, int index); void flb_lua_pushtimetable(lua_State *l, struct flb_time *tm); int flb_lua_is_valid_func(lua_State *l, flb_sds_t func); int flb_lua_pushmpack(lua_State *l, mpack_reader_t *reader); diff --git a/src/flb_lua.c b/src/flb_lua.c index cd0988b7d86..a374752326c 100644 --- a/src/flb_lua.c +++ b/src/flb_lua.c @@ -209,18 +209,18 @@ static int lua_isinteger(lua_State *L, int index) * If we update luajit which is based Lua 5.2+, * this function should be removed. */ -static int lua_table_maxn(lua_State *l) +static int lua_table_maxn(lua_State *l, int index) { #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM < 520 int ret = -1; - if (lua_type(l, -1) != LUA_TTABLE) { + if (lua_type(l, index) != LUA_TTABLE) { return -1; } lua_getglobal(l, "table"); lua_getfield(l, -1, "maxn"); lua_remove(l, -2); /* remove table (lua_getglobal(L, "table")) */ - lua_pushvalue(l, -2); /* copy record to top of stack */ + lua_pushvalue(l, index); /* copy record to top of stack */ ret = lua_pcall(l, 1, 1, 0); if (ret < 0) { flb_error("[filter_lua] failed to exec table.maxn ret=%d", ret); @@ -239,24 +239,26 @@ static int lua_table_maxn(lua_State *l) return ret; #else - return (int)lua_rawlen(l, 1); + return (int)lua_rawlen(l, index); #endif } -int flb_lua_arraylength(lua_State *l) +int flb_lua_arraylength(lua_State *l, int index) { lua_Integer n; int count = 0; int max = 0; int ret = 0; - ret = lua_table_maxn(l); + index = flb_lua_absindex(l, index); + + ret = lua_table_maxn(l, index); if (ret > 0) { return ret; } lua_pushnil(l); - while (lua_next(l, -2) != 0) { + while (lua_next(l, index) != 0) { if (lua_type(l, -2) == LUA_TNUMBER) { n = lua_tonumber(l, -2); if (n > 0) { @@ -269,8 +271,9 @@ int flb_lua_arraylength(lua_State *l) lua_pop(l, 2); return -1; } - if (max != count) + if (max != count) { return -1; + } return max; } @@ -442,7 +445,7 @@ void flb_lua_tompack(lua_State *l, mpack_write_false(writer); break; case LUA_TTABLE: - len = flb_lua_arraylength(l); + len = flb_lua_arraylength(l, -1 + index); if (len > 0) { mpack_write_tag(writer, mpack_tag_array(len)); for (i = 1; i <= len; i++) { @@ -533,7 +536,7 @@ void flb_lua_tomsgpack(lua_State *l, msgpack_pack_false(pck); break; case LUA_TTABLE: - len = flb_lua_arraylength(l); + len = flb_lua_arraylength(l, -1 + index); if (len > 0) { msgpack_pack_array(pck, len); for (i = 1; i <= len; i++) { @@ -612,7 +615,7 @@ static void print_lua_value(FILE *out, lua_State *l, int index, int depth) fprintf(out, " d=%lf i=%ld\n", val_d, val_i); break; case LUA_TTABLE: - len_t = flb_lua_arraylength(l); + len_t = flb_lua_arraylength(l, index); fprintf(out, " size=%d ", len_t); if (len_t > 0) { fprintf(out, "array\n"); From 3c2c5004b1600128934c89a3721b6b666cd3ebee Mon Sep 17 00:00:00 2001 From: Takahiro Yamashita Date: Sun, 13 Aug 2023 10:11:13 +0900 Subject: [PATCH 4/4] tests: internal: lua: pass index to lua_arraylength Signed-off-by: Takahiro Yamashita --- tests/internal/lua.c | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/tests/internal/lua.c b/tests/internal/lua.c index 79071ae1792..122748fbacd 100644 --- a/tests/internal/lua.c +++ b/tests/internal/lua.c @@ -228,7 +228,43 @@ static void test_lua_arraylength() lua_settable(l, -3); /* points created table */ } - len = flb_lua_arraylength(l); + len = flb_lua_arraylength(l, -1); + if (!TEST_CHECK(len == size)) { + TEST_MSG("size error. got=%d expect=%d", len, size); + } + lua_pop(l, 1); + + lua_close(l); +} + +static void test_lua_arraylength_with_index() +{ + lua_State *l; + int i; + int len; + int size = 10; + + l = luaL_newstate(); + if (!TEST_CHECK(l != NULL)) { + TEST_MSG("luaL_newstate faild"); + return; + } + luaL_openlibs(l); + + /* create array. */ + lua_createtable(l, size, 0); + + for (i=1; i<=size; i++) { + lua_pushinteger(l, i); /* set an index of array */ + lua_pushinteger(l, 3+i); /* set a value */ + lua_settable(l, -3); /* points created table */ + } + + /* push 2 values */ + lua_pushinteger(l, 100); + lua_pushinteger(l, 101); + + len = flb_lua_arraylength(l, -3); /* points array. -1 points 101, -2 points 100 */ if (!TEST_CHECK(len == size)) { TEST_MSG("size error. got=%d expect=%d", len, size); } @@ -265,7 +301,7 @@ static void test_lua_arraylength_for_array_contains_nil() lua_settable(l, -3); /* points created table */ } - len = flb_lua_arraylength(l); + len = flb_lua_arraylength(l, -1); if (!TEST_CHECK(len == size)) { TEST_MSG("size error. got=%d expect=%d", len, size); } @@ -283,6 +319,7 @@ TEST_LIST = { { "lua_tomsgpack" , test_tomsgpack }, { "lua_tompack" , test_tompack }, { "lua_arraylength" , test_lua_arraylength }, + { "lua_arraylength_with_index" , test_lua_arraylength_with_index }, { "lua_arraylength_for_array_contains_nil", test_lua_arraylength_for_array_contains_nil}, { 0 } };