diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-02-11 12:15:12 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-02-11 14:25:14 +0000 |
commit | bb97e8aaa15217afe1f3fcdc93662ab03b8ae9d9 (patch) | |
tree | 64fa5b5ba419e72bc6acaddadbc51c73646295fa /xlua.c | |
download | luiml-bb97e8aaa15217afe1f3fcdc93662ab03b8ae9d9.tar.gz luiml-bb97e8aaa15217afe1f3fcdc93662ab03b8ae9d9.tar.xz luiml-bb97e8aaa15217afe1f3fcdc93662ab03b8ae9d9.zip |
init commit
Diffstat (limited to 'xlua.c')
-rw-r--r-- | xlua.c | 56 |
1 files changed, 56 insertions, 0 deletions
@@ -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); +} |