diff --git a/lib/frrscript.c b/lib/frrscript.c index acdd1df67b..dbae31b666 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -348,6 +348,9 @@ int frrscript_load(struct frrscript *fs, const char *function_name, /* Set up the Lua script */ lua_State *L = luaL_newstate(); + /* Load basic built-in Lua functions, e.g. ipairs, string, etc. */ + luaL_openlibs(L); + frrlua_export_logging(L); char script_name[MAXPATHLEN]; diff --git a/tests/lib/test_frrlua.c b/tests/lib/test_frrlua.c index 2760a273bd..4aa8c8a8c3 100644 --- a/tests/lib/test_frrlua.c +++ b/tests/lib/test_frrlua.c @@ -13,6 +13,8 @@ static void test_encode_decode(void) { lua_State *L = luaL_newstate(); + luaL_openlibs(L); + int a = 123; int b = a; @@ -99,6 +101,20 @@ static void test_encode_decode(void) lua_decode_sockunion(L, -1, &su_a); assert(sockunion_cmp(&su_a, &su_b) == 0); assert(lua_gettop(L) == 0); + + /* Test if built-in functions (string() in this case) are working */ + const char *result; + + lua_getglobal(L, "string"); + lua_getfield(L, -1, "upper"); + lua_pushstring(L, "testas"); + lua_pcall(L, 1, 1, 0); + + result = lua_tostring(L, -1); + assert(strmatch(result, "TESTAS")); + lua_pop(L, 1); + lua_close(L); + /* End of built-in functions test */ } int main(int argc, char **argv)