M xlua/utils.c +4 -60
@@ 1,5 1,5 @@
/*
- * Copyright (c) 2022 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2022-2023 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
*
* 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 @@ void xlua_pushlib_utils(lua_State *L)
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");
}
M xlua/xlua.c +57 -1
@@ 1,5 1,5 @@
/*
- * Copyright (c) 2020-2022 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2020-2023 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
*
* 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 @@ err:
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)
M xlua/xlua_impl.h +2 -1
@@ 1,5 1,5 @@
/*
- * Copyright (c) 2020-2022 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2020-2023 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
*
* 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);