aboutsummaryrefslogtreecommitdiffstats
path: root/xlua.c
blob: bb1b2def9d8a0e44f3d121b0d2c7d01d2cfd3daf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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);
}