From bb97e8aaa15217afe1f3fcdc93662ab03b8ae9d9 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Thu, 11 Feb 2021 12:15:12 +0000 Subject: init commit --- xlua.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 xlua.c (limited to 'xlua.c') 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 +#include +#include +#include + +#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); +} -- cgit v1.2.3-54-g00ecf