# HG changeset patch # User Josef 'Jeff' Sipek # Date 1685705659 14400 # Fri Jun 02 07:34:19 2023 -0400 # Node ID 2540a6c565c06a4db4d77083e96b2fb972624ed6 # Parent 73d660b2bae7ef3dd926de266cda52858601d48b xlua: allow use of lua script fragments in all hlog.* libs Signed-off-by: Josef 'Jeff' Sipek diff --git a/xlua/utils.c b/xlua/utils.c --- a/xlua/utils.c +++ b/xlua/utils.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Josef 'Jeff' Sipek + * Copyright (c) 2022-2023 Josef 'Jeff' Sipek * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,62 +22,6 @@ #include "xlua_impl.h" -/* add in scriptname key-values to table at the top of the stack */ -static void merge_lua_funcs(lua_State *L, const char *scriptname) -{ - const int orig_top = lua_gettop(L); - char error[128]; - int ret; - - ASSERT3U(lua_type(L, -1), ==, LUA_TTABLE); - - ret = xlua_load_script_internal(L, scriptname, error, sizeof(error)); - if (ret) - panic("Failed to load hlog.utils script fragment '%s': %s\n", - scriptname, error); - - switch (lua_type(L, -1)) { - case LUA_TTABLE: - /* ok, merge it */ - lua_pushnil(L); - while (lua_next(L, -2)) { - /* stack: utils table, this table, key, value */ - - if (!lua_isstring(L, -2) || - !lua_isfunction(L, -1)) - panic("Warning: hlog.utils fragment " - "'%s' table contains %s=%s pair\n", - scriptname, - lua_typename(L, lua_type(L, -2)), - lua_typename(L, lua_type(L, -1))); - - /* duplicate the key */ - lua_pushvalue(L, -2); - lua_insert(L, -2); - - /* stack: utils table, table, key, key, value */ - - /* set the k=v on the utils table */ - lua_settable(L, -5); - } - - /* pop the table */ - lua_pop(L, 1); - break; - case LUA_TNIL: - /* nothing, weird but ok */ - lua_pop(L, 1); - break; - default: - /* other types */ - panic("Unknown type %s gotten from hlog.utils fragment " - "'%s'\n", lua_typename(L, lua_type(L, -1)), - scriptname); - } - - ASSERT_LUA_STACK(L, orig_top); -} - static const luaL_Reg utils_funcs[] = { { NULL, NULL }, }; @@ -88,7 +32,7 @@ luaL_newlib(L, utils_funcs); /* set functions implemented in lua */ - merge_lua_funcs(L, "utils-math.lua"); - merge_lua_funcs(L, "utils-split.lua"); - merge_lua_funcs(L, "utils-table.lua"); + xlua_merge_lua_funcs(L, "utils-math.lua"); + xlua_merge_lua_funcs(L, "utils-split.lua"); + xlua_merge_lua_funcs(L, "utils-table.lua"); } diff --git a/xlua/xlua.c b/xlua/xlua.c --- a/xlua/xlua.c +++ b/xlua/xlua.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2022 Josef 'Jeff' Sipek + * Copyright (c) 2020-2023 Josef 'Jeff' Sipek * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -151,6 +151,62 @@ return ret; } +/* add in scriptname key-values to table at the top of the stack */ +void xlua_merge_lua_funcs(lua_State *L, const char *scriptname) +{ + const int orig_top = lua_gettop(L); + char error[128]; + int ret; + + ASSERT3U(lua_type(L, -1), ==, LUA_TTABLE); + + ret = xlua_load_script_internal(L, scriptname, error, sizeof(error)); + if (ret) + panic("Failed to load hlog.utils script fragment '%s': %s\n", + scriptname, error); + + switch (lua_type(L, -1)) { + case LUA_TTABLE: + /* ok, merge it */ + lua_pushnil(L); + while (lua_next(L, -2)) { + /* stack: utils table, this table, key, value */ + + if (!lua_isstring(L, -2) || + !lua_isfunction(L, -1)) + panic("Warning: hlog.utils fragment " + "'%s' table contains %s=%s pair\n", + scriptname, + lua_typename(L, lua_type(L, -2)), + lua_typename(L, lua_type(L, -1))); + + /* duplicate the key */ + lua_pushvalue(L, -2); + lua_insert(L, -2); + + /* stack: utils table, table, key, key, value */ + + /* set the k=v on the utils table */ + lua_settable(L, -5); + } + + /* pop the table */ + lua_pop(L, 1); + break; + case LUA_TNIL: + /* nothing, weird but ok */ + lua_pop(L, 1); + break; + default: + /* other types */ + panic("Unknown type %s gotten from hlog.utils fragment " + "'%s'\n", lua_typename(L, lua_type(L, -1)), + scriptname); + } + + ASSERT_LUA_STACK(L, orig_top); +} + static lua_State *__xlua_init(const char *name, const uint8_t *data, size_t size, int nresults, char *error, size_t errlen) diff --git a/xlua/xlua_impl.h b/xlua/xlua_impl.h --- a/xlua/xlua_impl.h +++ b/xlua/xlua_impl.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2022 Josef 'Jeff' Sipek + * Copyright (c) 2020-2023 Josef 'Jeff' Sipek * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -30,6 +30,7 @@ extern int xlua_load_script_internal(lua_State *L, const char *scriptname, char *error, size_t errlen); +extern void xlua_merge_lua_funcs(lua_State *L, const char *scriptname); /* push various lib tables */ extern void xlua_pushlib_adif(lua_State *L);