summaryrefslogtreecommitdiff
path: root/src/honey_lua.c
blob: 7730809bbd867ebb8ceb25507b13375eeb85358f (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include "common.h"

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * Lua binding helper function definitions
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

/* string must be able to hold at least 16 characters. */
static void type_to_string(char* string,
			   honey_lua_type type);

static bool check_argument(lua_State* L,
			   honey_lua_type type,
			   int index);

static void get_argument(lua_State* L,
			 void* destination,
			 honey_lua_type type,
			 int index);

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * String wrangling helpers
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

honey_result honey_format_string(char** string,
                                 char* format_string,
                                 ...)
{
    honey_result res;
    va_list args, args_;
    va_start(args, format_string);
    va_copy(args_, args);

    int string_size = vsnprintf(NULL, 0, format_string, args_);
    va_end(args_);
    *string = malloc((string_size+1) * sizeof(char));
    if (*string == NULL)
        res = HONEY_MEMORY_ALLOCATION_ERROR;
    else {
        vsnprintf(*string, string_size+1, format_string, args);
        res = HONEY_OK;
    }
    
    va_end(args);
    
    return res;
}

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

void honey_lua_throw_error(lua_State* L,
			   char* format_string,
			   ...)
{
    honey_result result;
    va_list args, args_;
    va_start(args,  format_string);
    va_copy(args_, args);

    int string_size = vsnprintf(NULL, 0, format_string, args_);
    va_end(args_);

    char* string = malloc((string_size + 1) * sizeof(char));
    if (string == NULL)
	lua_pushstring(L, "there was an error allocating memory for an error message");
    else {
	vsnprintf(string, string_size + 1, format_string, args);
	lua_pushstring(L, string);
	free(string);
    }
    lua_error(L);
}
    
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * Argument parsing functions
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

static void honey_lua_arg_error(lua_State* L,
                                honey_lua_type type,
                                int position)
{
    char expected_type[16];
    type_to_string(expected_type, type);
    
    char* error_message;
    honey_result result;
    char* got_type = (char*) lua_typename(L, lua_type(L, position));
    result = honey_format_string(&error_message,
                                 "bad argument in position %d: "
                                 "expected %s, but got %s instead.",
                                 position,
                                 expected_type,
                                 got_type);
    if (result != HONEY_OK) {
        lua_pushstring(L, "error allocating memory for error message :(");
    }
    else {
        lua_pushstring(L, error_message);
        free(error_message);
    }
    lua_error(L);
}

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

int honey_lua_parse_arguments(lua_State* L, int n, ...)
{
    va_list args;
    va_start(args, n);

    for (int i=1; i<=n; i++) {
        honey_lua_type type = va_arg(args, honey_lua_type);
	void* destination = va_arg(args, void*);
	if (!check_argument(L, type, i))
	    honey_lua_arg_error(L, type, i);

	get_argument(L, destination, type, i);
    }
            
    va_end(args);
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * Table creation functions
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

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_INTEGER:
        lua_pushinteger(L, element.data.integer);
        break;

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

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

    case HONEY_FUNCTION:
        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;

    case HONEY_NIL:
        lua_pushnil(L);
        break;

    case HONEY_USERDATA:
        /* cannot push userdata, push nil */
        lua_pushnil(L);
        break;

    case HONEY_LIGHTUSERDATA:
        lua_pushlightuserdata(L, element.data.pointer);
        break;

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

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

int honey_lua_traceback(lua_State* L)
{
    if (!lua_isstring(L, 1))
        /* 'message' is not a string, keep intact. */
        return 1;

    lua_getglobal(L, "debug");
    if (!lua_istable(L, -1)) {
        lua_pop(L, 1);
        return 1;
    }

    lua_getfield(L, -1, "traceback");
    if (!lua_isfunction(L, -1)) {
        lua_pop(L, 2);
        return 1;
    }

    lua_pushvalue(L, 1);
    lua_pushinteger(L, 2);
    lua_call(L, 2, 1);
    return 1;
}

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

int honey_lua_pcall(lua_State* L, int nargs, int nret)
{
    int traceback_pos = lua_gettop(L) - nargs;
    lua_pushcfunction(L, honey_lua_traceback);
    lua_insert(L, traceback_pos);

    int result = lua_pcall(L, nargs, nret, traceback_pos);
    lua_remove(L, traceback_pos);
    return result;
}
    
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

int honey_exit(lua_State* L)
{
    if (honey_window_info_ref == LUA_NOREF ||
        honey_window_info_ref == LUA_REFNIL) {
        lua_pushstring(L, "Window information is not set!");
        lua_error(L);
    }

    lua_rawgeti(L, LUA_REGISTRYINDEX, honey_window_info_ref);
    honey_window_information* info = lua_touserdata(L, -1);
    lua_pop(L, 1);

    glfwSetWindowShouldClose(info->window, true);
    return 0;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * Lua binding helper functions
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

/* string must be able to hold at least 16 characters. */
static void type_to_string(char* string,
			   honey_lua_type type)
{
    switch(type) {
    case HONEY_BOOLEAN:
	memcpy(string, "boolean", 8);
	break;

    case HONEY_INTEGER:
	memcpy(string, "integer", 8);
	break;

    case HONEY_NUMBER:
	memcpy(string, "number", 7);
	break;

    case HONEY_STRING:
	memcpy(string, "string", 7);
	break;

    case HONEY_FUNCTION:
	memcpy(string, "function", 9);
	break;

    case HONEY_TABLE:
	memcpy(string, "table", 6);
	break;

    case HONEY_NIL:
	memcpy(string, "nil", 4);
	break;

    case HONEY_USERDATA:
	memcpy(string, "userdata", 9);
	break;

    case HONEY_LIGHTUSERDATA:
	memcpy(string, "light userdata", 16);
	break;

    case HONEY_ANY:
	memcpy(string, "any", 4);
	break;

    default:
	break;
    }
}

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

static bool check_argument(lua_State* L,
			   honey_lua_type type,
			   int index)
{
    switch(type) {
    case HONEY_BOOLEAN:
	if (!lua_isboolean(L, index))
	    return false;
	break;

    case HONEY_INTEGER:
    case HONEY_NUMBER:
	if (!lua_isnumber(L, index))
	    return false;
	break;

    case HONEY_STRING:
	if (!lua_isstring(L, index))
	    return false;
	break;

    case HONEY_FUNCTION:
	if (!lua_isfunction(L, index))
	    return false;
	break;

    case HONEY_TABLE:
	if (!lua_istable(L, index))
	    return false;
	break;

    case HONEY_NIL:
	if (!lua_isnil(L, index))
	    return false;
	break;

    case HONEY_USERDATA:
	if (!lua_isuserdata(L, index))
	    return false;
	break;

    case HONEY_LIGHTUSERDATA:
	if (!lua_islightuserdata(L, index))
	    return false;
	break;

    default:
	break;
    }


    return true;
}

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

static void get_argument(lua_State* L,
			 void* destination,
			 honey_lua_type type,
			 int index)
{
    switch (type) {
    case HONEY_BOOLEAN:
	{
	    bool* result = destination;
	    *result = lua_toboolean(L, index);
	}
	break;

    case HONEY_INTEGER:
	{
	    int* result = destination;
	    *result = lua_tointeger(L, index);
	}
	break;

    case HONEY_NUMBER:
	{
	    float* result = destination;
	    *result = lua_tonumber(L, index);
	}
	break;

    case HONEY_STRING:
	{
	    char** result = destination;
	    *result = (char*) lua_tostring(L, index);
	}
	break;
            
    case HONEY_TABLE:
	break;

    case HONEY_FUNCTION:
	break;

    case HONEY_NIL:
	break;

    case HONEY_USERDATA:
	{
	    void** result = destination;
	    *result = lua_touserdata(L, index);
	}
	break;

    case HONEY_LIGHTUSERDATA:
	{
	    void** result = destination;
	    *result = lua_touserdata(L, index);
	}
	break;

    case HONEY_ANY:
	break;

    default:
	/* should never get here! */
	break;
    }
}

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