aboutsummaryrefslogtreecommitdiffstats
path: root/xlua.c
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2021-02-11 12:15:12 +0000
committerTomasz Kramkowski <tk@the-tk.com>2021-02-11 14:25:14 +0000
commitbb97e8aaa15217afe1f3fcdc93662ab03b8ae9d9 (patch)
tree64fa5b5ba419e72bc6acaddadbc51c73646295fa /xlua.c
downloadluiml-bb97e8aaa15217afe1f3fcdc93662ab03b8ae9d9.tar.gz
luiml-bb97e8aaa15217afe1f3fcdc93662ab03b8ae9d9.tar.xz
luiml-bb97e8aaa15217afe1f3fcdc93662ab03b8ae9d9.zip
init commit
Diffstat (limited to 'xlua.c')
-rw-r--r--xlua.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/xlua.c b/xlua.c
new file mode 100644
index 0000000..bb1b2de
--- /dev/null
+++ b/xlua.c
@@ -0,0 +1,56 @@
+#include <lauxlib.h>
+#include <lua.h>
+#include <stdarg.h>
+#include <stdbool.h>
+
+#include "xlua.h"
+
+void pushtable(lua_State *L, const struct key *keys, ...)
+{
+ va_list ap;
+ int narr = 0, nrec = 0;
+
+ for (const struct key *key = keys; key->type != KT_NULL; key++) {
+ switch (key->type) {
+ case KT_STRING: nrec++; break;
+ case KT_NUMBER: narr++; break;
+ default:
+ luaL_error(L, "pusharray failed: key: invalid key type");
+ }
+ }
+
+ lua_createtable(L, narr, nrec);
+
+ va_start(ap, keys);
+
+ for (const struct key *key = keys; key->type != KT_NULL; key++) {
+ switch (key->type) {
+ case KT_STRING: lua_pushstring(L, key->key.str); break;
+ case KT_NUMBER: lua_pushnumber(L, key->key.num); break;
+ }
+ switch (key->vtype) {
+ case VT_STRING:
+ lua_pushstring(L, va_arg(ap, const char *));
+ break;
+ case VT_NUMBER:
+ lua_pushnumber(L, va_arg(ap, lua_Number));
+ break;
+ case VT_BOOLEAN:
+ lua_pushboolean(L, va_arg(ap, int));
+ break;
+ default:
+ luaL_error(L, "pusharray failed: key: invalid value type");
+ }
+ lua_settable(L, -3);
+ }
+
+ va_end(ap);
+}
+
+void pushsetmember(lua_State *L, int index, const char *name, bool in_set)
+{
+ if (!in_set) return;
+ if (index < 0) index -= 1;
+ lua_pushboolean(L, 1);
+ lua_setfield(L, index, name);
+}