summaryrefslogtreecommitdiff
path: root/src/ld-lua.c
blob: 1e1dd3069a44bc0a2a36c71f540b8163a319031a (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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
 * ld-lua.c
 *
 * This file is a part of logdiag.
 * Copyright Přemysl Janouch 2010. All rights reserved.
 *
 * See the file LICENSE for licensing information.
 *
 */

#include <gtk/gtk.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#include "config.h"

#include "ld-symbol.h"
#include "ld-symbol-category.h"
#include "ld-library.h"

#include "ld-lua.h"
#include "ld-lua-symbol.h"

#include "ld-lua-private.h"
#include "ld-lua-symbol-private.h"


/**
 * SECTION:ld-lua
 * @short_description: Lua symbol engine.
 * @see_also: #LdLuaSymbol
 *
 * #LdLua is a symbol engine that uses Lua scripts to manage symbols.
 */

/* How does the application call the function for rendering?
 *   registry.logdiag_symbols
 *     -> table indexed by pointers to LdLuaSymbol objects
 *   registry.logdiag_symbols.object.render(cr)
 *     -> rendering function
 */

/*
 * LdLuaPrivate:
 * @L: Lua state.
 *
 * The library contains the real function for rendering.
 */
struct _LdLuaPrivate
{
	lua_State *L;
};

G_DEFINE_TYPE (LdLua, ld_lua, G_TYPE_OBJECT);

static void ld_lua_finalize (GObject *gobject);

static void *ld_lua_alloc (void *ud, void *ptr, size_t osize, size_t nsize);


/*
 * LdLuaData:
 * @self: A reference to self.
 * @category: A reference to parent category of the currently processed file.
 *
 * Full user data to be stored in Lua registry.
 */
typedef struct
{
	LdLua *self;
	LdSymbolCategory *category;
}
LdLuaData;

#define LD_LUA_LIBRARY_NAME "logdiag"
#define LD_LUA_DATA_INDEX LD_LUA_LIBRARY_NAME "_data"
#define LD_LUA_SYMBOLS_INDEX LD_LUA_LIBRARY_NAME "_symbols"


typedef struct
{
	LdLuaSymbol *symbol;
	cairo_t *cr;
}
LdLuaDrawData;

static int ld_lua_private_draw_cb (lua_State *L);
static int ld_lua_private_unregister_cb (lua_State *L);


static int ld_lua_logdiag_register (lua_State *L);

static luaL_Reg ld_lua_logdiag_lib[] =
{
	{"register", ld_lua_logdiag_register},
	{NULL, NULL}
};


static void push_cairo_object (lua_State *L, cairo_t *cr);

static int ld_lua_cairo_move_to (lua_State *L);
static int ld_lua_cairo_line_to (lua_State *L);
static int ld_lua_cairo_stroke (lua_State *L);
static int ld_lua_cairo_stroke_preserve (lua_State *L);
static int ld_lua_cairo_fill (lua_State *L);
static int ld_lua_cairo_fill_preserve (lua_State *L);

static luaL_Reg ld_lua_cairo_table[] =
{
	{"move_to", ld_lua_cairo_move_to},
	{"line_to", ld_lua_cairo_line_to},
	{"stroke", ld_lua_cairo_stroke},
	{"stroke_preserve", ld_lua_cairo_stroke_preserve},
	{"fill", ld_lua_cairo_fill},
	{"fill_preserve", ld_lua_cairo_fill_preserve},
	{NULL, NULL}
};


/* ===== Generic =========================================================== */

static void
ld_lua_class_init (LdLuaClass *klass)
{
	GObjectClass *object_class;

	object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = ld_lua_finalize;

	g_type_class_add_private (klass, sizeof (LdLuaPrivate));
}

static void
ld_lua_init (LdLua *self)
{
	lua_State *L;
	LdLuaData *ud;

	self->priv = G_TYPE_INSTANCE_GET_PRIVATE
		(self, LD_TYPE_LUA, LdLuaPrivate);

	L = self->priv->L = lua_newstate (ld_lua_alloc, NULL);
	g_return_if_fail (L != NULL);

	/* TODO: lua_atpanic () */

	/* Load some safe libraries. */
	lua_pushcfunction (L, luaopen_string);
	lua_call (L, 0, 0);

	lua_pushcfunction (L, luaopen_table);
	lua_call (L, 0, 0);

	lua_pushcfunction (L, luaopen_math);
	lua_call (L, 0, 0);

	/* Load the application library. */
	luaL_register (L, LD_LUA_LIBRARY_NAME, ld_lua_logdiag_lib);

	/* Store user data to the registry. */
	ud = lua_newuserdata (L, sizeof (LdLuaData));
	ud->self = self;
	ud->category = NULL;

	lua_setfield (L, LUA_REGISTRYINDEX, LD_LUA_DATA_INDEX);

	/* Create an empty symbols table. */
	lua_newtable (L);
	lua_setfield (L, LUA_REGISTRYINDEX, LD_LUA_SYMBOLS_INDEX);
}

static void
ld_lua_finalize (GObject *gobject)
{
	LdLua *self;

	self = LD_LUA (gobject);
	lua_close (self->priv->L);

	/* Chain up to the parent class. */
	G_OBJECT_CLASS (ld_lua_parent_class)->finalize (gobject);
}

/**
 * ld_lua_new:
 *
 * Create an instance of #LdLua.
 */
LdLua *
ld_lua_new (void)
{
	return g_object_new (LD_TYPE_LUA, NULL);
}

static void *
ld_lua_alloc (void *ud, void *ptr, size_t osize, size_t nsize)
{
	if (!nsize)
	{
		g_free (ptr);
		return NULL;
	}
	else
		return g_try_realloc (ptr, nsize);
}

/**
 * ld_lua_check_file:
 * @self: An #LdLua object.
 * @filename: The file to be checked.
 *
 * Check if the given filename can be loaded by #LdLua.
 */
gboolean
ld_lua_check_file (LdLua *self, const gchar *filename)
{
	g_return_val_if_fail (LD_IS_LUA (self), FALSE);
	return g_str_has_suffix (filename, ".lua")
		&& g_file_test (filename, G_FILE_TEST_IS_REGULAR);
}

/**
 * ld_lua_load_file_to_category:
 * @self: An #LdLua object.
 * @filename: The file to be loaded.
 * @category: An #LdSymbolCategory object.
 *
 * Loads a file and appends contained symbols into the category.
 *
 * Returns: TRUE if no error has occured, FALSE otherwise.
 */
gboolean
ld_lua_load_file_to_category (LdLua *self, const gchar *filename,
	LdSymbolCategory *category)
{
	gint retval;
	LdLuaData *ud;

	g_return_val_if_fail (LD_IS_LUA (self), FALSE);
	g_return_val_if_fail (filename != NULL, FALSE);
	g_return_val_if_fail (LD_IS_SYMBOL_CATEGORY (category), FALSE);

	/* XXX: If something from the following fails, Lua will call exit(). */
	lua_getfield (self->priv->L, LUA_REGISTRYINDEX, LD_LUA_DATA_INDEX);
	ud = lua_touserdata (self->priv->L, -1);
	lua_pop (self->priv->L, 1);
	g_return_val_if_fail (ud != NULL, FALSE);

	ud->category = category;

	retval = luaL_loadfile (self->priv->L, filename);
	if (retval)
		goto ld_lua_lftc_fail;

	retval = lua_pcall (self->priv->L, 0, 0, 0);
	if (retval)
		goto ld_lua_lftc_fail;

	ud->category = NULL;
	return TRUE;

ld_lua_lftc_fail:
	g_warning ("Lua error: %s", lua_tostring (self->priv->L, -1));
	lua_remove (self->priv->L, -1);

	ud->category = NULL;
	return FALSE;
}

/* ===== LdLuaSymbol callbacks ============================================= */

/**
 * ld_lua_private_draw:
 * @self: An #LdLua object.
 * @symbol: A symbol to be drawn.
 * @cr: A Cairo context to be drawn onto.
 *
 * Draw a symbol onto a Cairo context.
 */
void
ld_lua_private_draw (LdLua *self, LdLuaSymbol *symbol, cairo_t *cr)
{
	LdLuaDrawData data;

	g_return_if_fail (LD_IS_LUA (self));
	g_return_if_fail (LD_IS_LUA_SYMBOL (symbol));

	data.symbol = symbol;
	data.cr = cr;

	if (lua_cpcall (self->priv->L, ld_lua_private_draw_cb, &data))
	{
		g_warning ("Lua error: %s", lua_tostring (self->priv->L, -1));
		lua_remove (self->priv->L, -1);
	}
}

static int
ld_lua_private_draw_cb (lua_State *L)
{
	LdLuaDrawData *data;

	data = lua_touserdata (L, -1);

	/* Retrieve the function for rendering from the registry. */
	lua_getfield (L, LUA_REGISTRYINDEX, LD_LUA_SYMBOLS_INDEX);
	lua_pushlightuserdata (L, data->symbol);
	lua_gettable (L, -2);

	luaL_checktype (L, -1, LUA_TTABLE);
	lua_getfield (L, -1, "render");
	luaL_checktype (L, -1, LUA_TFUNCTION);

	/* Call the function do draw the symbol. */
	push_cairo_object (L, data->cr);
	lua_pcall (L, 1, 0, 0);
}

/**
 * ld_lua_private_unregister:
 * @self: An #LdLua object.
 * @symbol: A symbol to be unregistered.
 *
 * Unregister a symbol from the internal Lua state.
 */
void
ld_lua_private_unregister (LdLua *self, LdLuaSymbol *symbol)
{
	g_return_if_fail (LD_IS_LUA (self));
	g_return_if_fail (LD_IS_LUA_SYMBOL (symbol));

	if (lua_cpcall (self->priv->L, ld_lua_private_unregister_cb, symbol))
	{
		g_warning ("Lua error: %s", lua_tostring (self->priv->L, -1));
		lua_remove (self->priv->L, -1);
	}
}

static int
ld_lua_private_unregister_cb (lua_State *L)
{
	/* Set the entry in the symbol table to nil. */
	lua_getfield (L, LUA_REGISTRYINDEX, LD_LUA_SYMBOLS_INDEX);
	lua_insert (L, -2);
	lua_pushnil (L);
	lua_settable (L, -3);
	return 0;
}

/* ===== Application library =============================================== */

static int
ld_lua_logdiag_register (lua_State *L)
{
	LdLuaData *ud;
	LdLuaSymbol *symbol;
	const gchar *name;

	lua_getfield (L, LUA_REGISTRYINDEX, LD_LUA_DATA_INDEX);
	ud = lua_touserdata (L, -1);
	lua_pop (L, 1);
	g_return_val_if_fail (ud != NULL, 0);

	/* TODO: Create a symbol using the given parameters:
	 * 1. name
	 * 2. names (table) -> use g_get_language_names ()
	 * 3. area (table)
	 * 4. terminals (table)
	 * 5. render function
	 */

	/* Check and retrieve arguments. */
	name = lua_tostring (L, 1);
	if (!name)
		luaL_error (L, "register: bad or missing argument #%d", 1);
	if (!lua_isfunction (L, 5))
		luaL_error (L, "register: bad or missing argument #%d", 5);

	/* Create a symbol object. */
	symbol = g_object_new (LD_TYPE_LUA_SYMBOL, NULL);
	symbol->priv->lua = ud->self;
	g_object_ref (ud->self);

	ld_symbol_set_name (LD_SYMBOL (symbol), name);

	/* Create an entry in the symbol table. */
	lua_getfield (L, LUA_REGISTRYINDEX, LD_LUA_SYMBOLS_INDEX);
	lua_pushlightuserdata (L, symbol);

	lua_newtable (L);
	lua_pushvalue (L, 5);
	lua_setfield (L, -2, "render");

	lua_settable (L, -3);

	/* Insert the symbol into the category. */
	/* TODO: Don't just add blindly, also check for name collisions. */
	ld_symbol_category_insert_child (ud->category, G_OBJECT (symbol), -1);
	g_object_unref (symbol);

	return 0;
}

/* ===== Cairo ============================================================= */

static void
push_cairo_object (lua_State *L, cairo_t *cr)
{
	luaL_Reg *fn;

	/* Create a table. */
	lua_newtable (L);

	/* Add methods. */
	/* XXX: The light user data pointer gets invalid after the end of
	 *      "render" function invocation. If the script stores the "cr" object
	 *      in some global variable and then tries to reuse it the next time,
	 *      the application will go SIGSEGV.
	 */
	for (fn = ld_lua_cairo_table; fn->name; fn++)
	{
		lua_pushlightuserdata (L, cr);
		lua_pushcclosure (L, fn->func, 1);
		lua_setfield (L, -2, fn->name);
	}
}

/* TODO: More functions. Possibly put it into another file
 *       and generate it automatically.
 */
static int
ld_lua_cairo_move_to (lua_State *L)
{
	cairo_t *cr;
	lua_Number x, y;

	cr = lua_touserdata (L, lua_upvalueindex (1));

	x = luaL_checknumber (L, 1);
	y = luaL_checknumber (L, 2);

	cairo_move_to (cr, x, y);
	return 0;
}

static int
ld_lua_cairo_line_to (lua_State *L)
{
	cairo_t *cr;
	lua_Number x, y;

	cr = lua_touserdata (L, lua_upvalueindex (1));
	x = luaL_checknumber (L, 1);
	y = luaL_checknumber (L, 2);
	cairo_line_to (cr, x, y);
	return 0;
}

static int
ld_lua_cairo_stroke (lua_State *L)
{
	cairo_t *cr;

	cr = lua_touserdata (L, lua_upvalueindex (1));
	cairo_stroke (cr);
	return 0;
}

static int
ld_lua_cairo_stroke_preserve (lua_State *L)
{
	cairo_t *cr;

	cr = lua_touserdata (L, lua_upvalueindex (1));
	cairo_stroke_preserve (cr);
	return 0;
}

static int
ld_lua_cairo_fill (lua_State *L)
{
	cairo_t *cr;

	cr = lua_touserdata (L, lua_upvalueindex (1));
	cairo_fill (cr);
	return 0;
}

static int
ld_lua_cairo_fill_preserve (lua_State *L)
{
	cairo_t *cr;

	cr = lua_touserdata (L, lua_upvalueindex (1));
	cairo_fill_preserve (cr);
	return 0;
}