summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/util.c21
-rw-r--r--src/util/util.h17
2 files changed, 38 insertions, 0 deletions
diff --git a/src/util/util.c b/src/util/util.c
index 89e9d38..52e2a16 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -1,6 +1,27 @@
#include <lua.h>
#include <lauxlib.h>
#include <honeysuckle.h>
+#include "util.h"
+
+void create_table(lua_State *L, struct honey_tbl_t *tbl)
+{
+ lua_createtable(L, 0, 0);
+ int t = lua_gettop(L);
+
+ for (struct honey_tbl_t *pair = tbl; pair->key != NULL; pair += 1) {
+ if (pair->type == LUA_TNUMBER) {
+ lua_pushinteger(L, pair->value.integer);
+ lua_setfield(L, t, pair->key);
+ }
+ else if (pair->type == LUA_TFUNCTION) {
+ lua_pushcfunction(L, pair->value.function);
+ lua_setfield(L, t, pair->key);
+ }
+ else {
+ /* bad type, ignore */
+ }
+ }
+}
void append_table(lua_State *L, int tbl_a, int tbl_b)
{
diff --git a/src/util/util.h b/src/util/util.h
index 231bcc9..6092d11 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -3,7 +3,24 @@
#include <lua.h>
+
+struct honey_tbl_t {
+ const char *key;
+ int type;
+ union {
+ lua_Integer integer;
+ lua_CFunction function;
+ } value;
+};
+
+#define H_INT(k, v) { .key=k, .type=LUA_TNUMBER, .value.integer=v }
+#define H_ENUM(v) { .key=#v, .type=LUA_TNUMBER, .value.integer=v }
+#define H_FUNC(k, v) { .key=k, .type=LUA_TFUNCTION, .value.function=v }
+#define H_END { .key=NULL, .type=LUA_TNIL, .value.integer=0 }
+
+
void setup_util(lua_State *L, int honey_tbl);
+void create_table(lua_State *L, struct honey_tbl_t *tbl);
void append_table(lua_State *L, int tbl_a, int tbl_b);
#endif