summaryrefslogtreecommitdiff
path: root/src/honey_lua.c
blob: 1cbd4834e1902baea3eb228f5a837d12ac9a52a4 (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
#include "common.h"

void honey_lua_create_table(lua_State* L,
			    honey_lua_element* elements,
			    unsigned int n_elements)
{
    lua_createtable(L, 0, n_elements);

    for (int i=0; i<n_elements; i++) {
	honey_lua_push_element(L, elements[i]);
	lua_setfield(L, -2, elements[i].name);
    }
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

void honey_lua_push_element(lua_State* L, honey_lua_element element)
{
    switch(element.type) {
    case HONEY_INT:
	lua_pushinteger(L, element.data.integer);
	break;

    case HONEY_NUM:
	lua_pushnumber(L, element.data.number);
	break;

    case HONEY_STRING:
	lua_pushstring(L, element.data.string);
	break;

    case HONEY_FUNC:
	lua_pushcfunction(L, element.data.function);
	break;

    case HONEY_TABLE:
	honey_lua_create_table(L,
			       element.data.table.elements,
			       element.data.table.n_elements);
	break;

    default:
	// this should never happen
	break;
    }
}