summaryrefslogtreecommitdiffstats
path: root/src/lua/lua_rcl.c
blob: efedc0725c8e0edb9b8f67a641b29e2452c12dfb (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* Copyright (c) 2013, Vsevolod Stakhov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *       * Redistributions of source code must retain the above copyright
 *         notice, this list of conditions and the following disclaimer.
 *       * Redistributions in binary form must reproduce the above copyright
 *         notice, this list of conditions and the following disclaimer in the
 *         documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "lua_common.h"

/**
 * @file lua rcl bindings
 */

static gint lua_rcl_obj_push_array (lua_State *L, const ucl_object_t *obj);
static gint lua_rcl_obj_push_simple (lua_State *L, const ucl_object_t *obj, gboolean allow_array);
static ucl_object_t* lua_rcl_table_get (lua_State *L, gint idx);
static ucl_object_t* lua_rcl_elt_get (lua_State *L, gint idx);

/**
 * Push a single element of an object to lua
 * @param L
 * @param key
 * @param obj
 */
static void
lua_rcl_obj_push_elt (lua_State *L, const char *key, const ucl_object_t *obj)
{
	lua_pushstring (L, key);
	lua_rcl_obj_push (L, obj, TRUE);
	lua_settable (L, -3);
}

/**
 * Push a single object to lua
 * @param L
 * @param obj
 * @return
 */
static gint
lua_rcl_obj_push_obj (lua_State *L, const ucl_object_t *obj, gboolean allow_array)
{
	const ucl_object_t *cur;
	ucl_object_iter_t it = NULL;

	if (allow_array && obj->next != NULL) {
		/* Actually we need to push this as an array */
		return lua_rcl_obj_push_array (L, obj);
	}

	lua_newtable (L);
	while ((cur = ucl_iterate_object (obj, &it, true)) != NULL) {
		lua_rcl_obj_push_elt (L, ucl_object_key (cur), cur);
	}

	return 1;
}

/**
 * Push an array to lua as table indexed by integers
 * @param L
 * @param obj
 * @return
 */
static gint
lua_rcl_obj_push_array (lua_State *L, const ucl_object_t *obj)
{
	const ucl_object_t *cur;
	gint i = 1;

	lua_newtable (L);

	LL_FOREACH (obj, cur) {
		lua_rcl_obj_push (L, cur, FALSE);
		lua_rawseti (L, -2, i);
		i ++;
	}

	return 1;
}

/**
 * Push a simple object to lua depending on its actual type
 */
static gint
lua_rcl_obj_push_simple (lua_State *L, const ucl_object_t *obj, gboolean allow_array)
{
	if (allow_array && obj->next != NULL) {
		/* Actually we need to push this as an array */
		return lua_rcl_obj_push_array (L, obj);
	}

	switch (obj->type) {
	case UCL_BOOLEAN:
		lua_pushboolean (L, ucl_obj_toboolean (obj));
		break;
	case UCL_STRING:
		lua_pushstring (L, ucl_obj_tostring (obj));
		break;
	case UCL_INT:
#if LUA_VERSION_NUM >= 501
		lua_pushinteger (L, ucl_obj_toint (obj));
#else
		lua_pushnumber (L, ucl_obj_toint (obj));
#endif
		break;
	case UCL_FLOAT:
	case UCL_TIME:
		lua_pushnumber (L, ucl_obj_todouble (obj));
		break;
	default:
		lua_pushnil (L);
		break;
	}

	return 1;
}

/**
 * Push an object to lua
 * @param L lua state
 * @param obj object to push
 */
gint
lua_rcl_obj_push (lua_State *L, const ucl_object_t *obj, gboolean allow_array)
{
	switch (obj->type) {
	case UCL_OBJECT:
		return lua_rcl_obj_push_obj (L, obj, allow_array);
	case UCL_ARRAY:
		return lua_rcl_obj_push_array (L, obj->value.av);
	default:
		return lua_rcl_obj_push_simple (L, obj, allow_array);
	}
}

/**
 * Parse lua table into object top
 * @param L
 * @param top
 * @param idx
 */
static ucl_object_t *
lua_rcl_table_get (lua_State *L, gint idx)
{
	ucl_object_t *obj, *top = NULL;
	gsize keylen;
	const gchar *k;

	/* Table iterate */
	lua_pushvalue (L, idx);
	lua_pushnil (L);
	top = ucl_object_typed_new (UCL_OBJECT);
	while (lua_next (L, -2) != 0) {
		/* copy key to avoid modifications */
		lua_pushvalue (L, -2);
		k = lua_tolstring (L, -1, &keylen);
		obj = lua_rcl_elt_get (L, -2);
		ucl_object_insert_key (top, obj, k, keylen, true);
		lua_pop (L, 2);
	}
	lua_pop (L, 1);

	return top;
}

/**
 * Get a single element from lua to object obj
 * @param L
 * @param obj
 * @param idx
 */
static ucl_object_t *
lua_rcl_elt_get (lua_State *L, gint idx)
{
	gint type;
	ucl_object_t *obj;

	type = lua_type (L, idx);

	switch (type) {
	case LUA_TFUNCTION:
		lua_pushvalue (L, idx);
		obj = ucl_object_new ();
		obj->type = UCL_USERDATA;
		obj->value.ud = GINT_TO_POINTER (luaL_ref (L, LUA_REGISTRYINDEX));
		break;
	case LUA_TSTRING:
		obj = ucl_object_fromstring_common (lua_tostring (L, idx), 0, 0);
		break;
	case LUA_TNUMBER:
		obj = ucl_object_fromdouble (lua_tonumber (L, idx));
		break;
	case LUA_TBOOLEAN:
		obj = ucl_object_frombool (lua_toboolean (L, idx));
		break;
	case LUA_TTABLE:
		obj = lua_rcl_table_get (L, idx);
		break;
	}

	return obj;
}

/**
 * Extract rcl object from lua object
 * @param L
 * @return
 */
ucl_object_t *
lua_rcl_obj_get (lua_State *L, gint idx)
{
	ucl_object_t *obj;
	gint t;

	t = lua_type (L, idx);
	switch (t) {
	case LUA_TTABLE:
		/* We assume all tables as objects, not arrays */
		obj = lua_rcl_table_get (L, idx);
		break;
	default:
		obj = lua_rcl_elt_get (L, idx);
		break;
	}

	return obj;
}