added a test for memory management
[lqt.git] / common / lqt_common.cpp
blobd9afd237ff385168a5c348acacd143724a71820a
1 /*
2 * Copyright (c) 2007-2008 Mauro Iazzi
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "lqt_common.hpp"
28 #include <cstring>
30 #include <QDebug>
32 static void lqtL_getenumtable (lua_State *L) {
33 lua_getfield(L, LUA_REGISTRYINDEX, LQT_ENUMS);
34 if (lua_isnil(L, -1)) {
35 lua_pop(L, 1);
36 lua_newtable(L);
37 lua_pushvalue(L, -1);
38 lua_setfield(L, LUA_REGISTRYINDEX, LQT_ENUMS);
42 static void lqtL_getpointertable (lua_State *L) {
43 lua_getfield(L, LUA_REGISTRYINDEX, LQT_POINTERS); // (1) get storage for pointers
44 if (lua_isnil(L, -1)) { // (1) if there is not
45 lua_pop(L, 1); // (0) pop the nil value
46 lua_newtable(L); // (1) create a new one
47 lua_newtable(L); // (2) create an empty metatable
48 lua_pushstring(L, "v"); // (3) push the mode value: weak values are enough
49 lua_setfield(L, -2, "__mode"); // (2) set the __mode field
50 lua_setmetatable(L, -2); // (1) set it as the metatable
51 lua_pushvalue(L, -1); // (2) duplicate the new pointer table
52 lua_setfield(L, LUA_REGISTRYINDEX, LQT_POINTERS); // (1) put one copy as storage
56 static void lqtL_getreftable (lua_State *L) {
57 lua_getfield(L, LUA_REGISTRYINDEX, LQT_REFS); // (1) get storage for pointers
58 if (lua_isnil(L, -1)) { // (1) if there is not
59 lua_pop(L, 1); // (0) pop the nil value
60 lua_newtable(L); // (1) create a new one
61 lua_newtable(L); // (2) create an empty metatable
62 lua_pushstring(L, "v"); // (3) push the mode value: weak values are enough
63 lua_setfield(L, -2, "__mode"); // (2) set the __mode field
64 lua_setmetatable(L, -2); // (1) set it as the metatable
65 lua_pushvalue(L, -1); // (2) duplicate the new pointer table
66 lua_setfield(L, LUA_REGISTRYINDEX, LQT_REFS); // (1) put one copy as storage
70 void * lqtL_getref (lua_State *L, size_t sz, bool weak) {
71 void *ret = NULL;
72 lqtL_getreftable(L); // (1)
73 ret = lua_newuserdata(L, sz); // (2)
75 lua_newtable(L); // (3)
76 lua_getglobal(L, "DEBUG"); // (4)
77 lua_setfield(L, -2, "__gc"); // (3)
78 lua_setmetatable(L, -2); // (2)
80 if (weak) {
81 lua_rawseti(L, -2, 1+lua_objlen(L, -2)); // (1)
82 } else {
83 lua_pushinteger(L, 1+lua_objlen(L, -2)); // (3)
84 lua_settable(L, -3); // (1)
86 lua_pop(L, 1);
87 return ret;
90 bool * lqtL_toboolref (lua_State *L, int index) {
91 bool tmp = lua_toboolean(L, index);
92 bool *ret = (bool*)lqtL_getref(L, sizeof(bool), true);
93 *ret = tmp;
94 return ret;
97 int * lqtL_tointref (lua_State *L, int index) {
98 int tmp = lua_tointeger(L, index);
99 int *ret = (int*)lqtL_getref(L, sizeof(int), false);
100 *ret = tmp;
101 return ret;
104 void lqtL_pusharguments (lua_State *L, char **argv) {
105 int i = 0;
106 lua_newtable(L);
107 for (i=0;*argv /* fix the maximum number? */;argv++,i++) {
108 lua_pushstring(L, *argv);
109 lua_rawseti(L, -2, i+1);
111 return;
114 char ** lqtL_toarguments (lua_State *L, int index) {
115 char ** ret = (char**)lqtL_getref(L, sizeof(char*)*(lua_objlen(L, index)+1), false);
116 const char *str = NULL;
117 size_t strlen = 0;
118 int i = 0;
119 for (i=0;;i++) {
120 lua_rawgeti(L, index, i+1);
121 if (!lua_isstring(L, -1)) {
122 str = NULL; strlen = 0;
123 ret[i] = NULL;
124 lua_pop(L, 1);
125 break;
126 } else {
127 str = lua_tolstring(L, -1, &strlen);
128 ret[i] = (char*)lqtL_getref(L, sizeof(char)*(strlen+1), false);
129 strncpy(ret[i], str, strlen+1);
130 lua_pop(L, 1);
133 return ret;
136 static int lqtL_createenum (lua_State *L, lqt_Enum e[], const char *n) {
137 luaL_Reg empty[] = { { 0, 0 } };
138 lqt_Enum *l = e;
139 lqtL_getenumtable(L); // (1)
140 lua_newtable(L); // (2)
141 lua_pushvalue(L, -1); // (3)
142 lua_setfield(L, -3, n); // (2)
143 while ( (l->name!=0) ) { // (2)
144 lua_pushstring(L, l->name); // (3)
145 lua_pushinteger(L, l->value); // (4)
146 lua_settable(L, -3); // (2)
147 lua_pushinteger(L, l->value); // (3)
148 lua_pushstring(L, l->name); // (4)
149 lua_settable(L, -3); // (2)
150 l++; // (2)
152 lua_pop(L, 2); // (0)
153 l = e;
154 luaL_register(L, n, empty); // (1)
155 while ( (l->name!=0) ) { // (1)
156 lua_pushstring(L, l->name); // (2)
157 lua_pushinteger(L, l->value); // (3)
158 lua_settable(L, -3); // (1)
159 lua_pushinteger(L, l->value); // (2)
160 lua_pushstring(L, l->name); // (3)
161 lua_settable(L, -3); // (1)
162 l++; // (1)
164 lua_pop(L, 1); // (0)
165 return 0;
168 int lqtL_createenumlist (lua_State *L, lqt_Enumlist list[]) {
169 while (list->enums!=0 && list->name!=0) {
170 lqtL_createenum(L, list->enums, list->name); // (0)
171 list++;
173 return 0;
176 static int lqtL_gcfunc (lua_State *L) {
177 if (!lua_isuserdata(L, 1) && lua_islightuserdata(L, 1)) return 0;
178 lua_getfenv(L, 1); // (1)
179 if (!lua_istable(L, -1)) {
180 lua_pop(L, 1); // (0)
181 return 0;
183 lua_getfield(L, -1, "__gc"); // (2)
184 lua_remove(L, -2); // (1)
185 if (!lua_isfunction(L, -1)) {
186 lua_pop(L, 1); // (0)
187 return 0;
189 lua_pushvalue(L, 1); // (2)
190 if (lua_pcall(L, 1, 0, 0)) { // (-2;+1/+0)
191 // (1)
192 return lua_error(L);
194 return 0; // (+0)
197 static int lqtL_newindexfunc (lua_State *L) {
198 lua_settop(L, 3); // (=3)
199 if (!lua_isuserdata(L, 1) && lua_islightuserdata(L, 1)) return 0;
200 lua_getfenv(L, 1); // (+1)
201 if (!lua_istable(L, -1)) {
202 lua_pop(L, 1); // (+0)
203 return 0;
205 lua_remove(L, 1); // (+0)
206 lua_insert(L, 1); // (+0)
207 lua_rawset(L, 1); // (-2)
208 return 0;
210 static int lqtL_indexfunc (lua_State *L) {
211 int i = 1;
212 if (lua_isuserdata(L, 1) && !lua_islightuserdata(L, 1)) {
213 lua_getfenv(L, 1); // (1)
214 lua_pushvalue(L, 2); // (2)
215 lua_gettable(L, -2); // (2)
216 if (!lua_isnil(L, -1)) {
217 lua_remove(L, -2);
218 return 1;
220 lua_pop(L, 2); // (0)
222 lua_pushnil(L); // (+1)
223 while (!lua_isnone(L, lua_upvalueindex(i))) { // (+1)
224 lua_pop(L, 1); // (+0)
225 lua_pushvalue(L, 2); // (+1)
226 if (i==1) {
227 lua_rawget(L, lua_upvalueindex(i)); // (+1)
228 } else {
229 lua_gettable(L, lua_upvalueindex(i)); // (+1)
231 if (!lua_isnil(L, -1)) break;
232 i++;
234 return 1; // (+1)
237 static int lqtL_pushindexfunc (lua_State *L, const char *name, lqt_Base *bases) {
238 int upnum = 1;
239 luaL_newmetatable(L, name); // (1)
240 while (bases->basename!=NULL) {
241 luaL_newmetatable(L, bases->basename); // (upnum)
242 upnum++;
243 bases++;
245 lua_pushcclosure(L, lqtL_indexfunc, upnum); // (1)
246 return 1;
249 static int lqtL_ctor_helper(lua_State*L) {
250 lua_getfield(L, 1, "new");
251 lua_replace(L, 1);
252 lua_call(L, lua_gettop(L)-1, LUA_MULTRET);
253 return lua_gettop(L);
256 static int lqtL_local_ctor(lua_State*L) {
257 lua_pushvalue(L, lua_upvalueindex(1)); // (+1)
258 lua_getfield(L, -1, "new"); // (+2)
259 lua_insert(L, 1); // (+2)
260 lua_pop(L, 1); // (+1)
261 lua_call(L, lua_gettop(L)-1, LUA_MULTRET); // (X)
262 lua_getfield(L, 1, "delete"); // (X+1)
263 lua_setfield(L, 1, "__gc"); // (X)
264 return lua_gettop(L);
267 int lqtL_createclass (lua_State *L, const char *name, luaL_Reg *mt, lqt_Base *bases) {
268 int len = 0;
269 char *new_name = NULL;
270 lqt_Base *bi = bases;
271 luaL_newmetatable(L, name); // (1)
272 luaL_register(L, NULL, mt); // (1)
273 // setup offsets
274 lua_pushstring(L, name); // (2) FIXME: remove
275 lua_pushinteger(L, 0); // (3) FIXME: remove
276 lua_settable(L, -3); // (1) FIXME: remove
277 while (bi->basename!=NULL) {
278 lua_pushstring(L, bi->basename); // (2) FIXME: remove
279 lua_pushinteger(L, bi->offset); // (3) FIXME: remove
280 lua_settable(L, -3); // (1) FIXME: remove
281 bi++;
283 // set metafunctions
284 lqtL_pushindexfunc(L, name, bases); // (2)
285 lua_setfield(L, -2, "__index"); // (1)
286 lua_pushcfunction(L, lqtL_newindexfunc); // (2)
287 lua_setfield(L, -2, "__newindex"); // (1)
288 lua_pushcfunction(L, lqtL_gcfunc); // (2)
289 lua_setfield(L, -2, "__gc"); // (1)
291 // set it as its own metatable
292 lua_pushvalue(L, -1); // (2)
293 lua_setmetatable(L, -2); // (1)
294 lua_pop(L, 1); // (0)
295 len = strlen(name);
296 new_name = (char*)malloc(len*sizeof(char));
297 strncpy(new_name, name, len);
298 new_name[len-1] = '\0';
299 luaL_register(L, new_name, mt); // (1)
300 free(new_name);
301 new_name = NULL;
302 lua_newtable(L); // (2)
303 lua_pushcfunction(L, lqtL_ctor_helper); // (3)
304 lua_setfield(L, -2, "__call"); // (2)
305 lua_setmetatable(L, -2); // (1)
306 lua_pushvalue(L, -1); // (2)
307 lua_pushcclosure(L, lqtL_local_ctor, 1); // (2)
308 lua_setfield(L, -2, "new_local");
309 lua_pop(L, 1); // (0)
311 lua_pushlstring(L, name, strlen(name)-1); // (1)
312 lua_newtable(L); // (2)
313 luaL_newmetatable(L, name); // (3)
314 lua_setmetatable(L, -2); // (2)
315 // don't register again but use metatable
316 //luaL_register(L, NULL, mt); // (2)
317 lua_settable(L, LUA_GLOBALSINDEX); // (0)
319 return 0;
322 int lqtL_createclasses (lua_State *L, lqt_Class *list) {
323 while (list->name!=0) { // (0)
324 luaL_newmetatable(L, list->name); // (1)
325 luaL_register(L, NULL, list->mt); // (1)
326 lua_pushstring(L, list->name); // (2)
327 lua_pushboolean(L, 1); // (3)
328 lua_settable(L, -3); // (1)
329 lqtL_pushindexfunc(L, list->name, list->bases); // (2)
330 lua_setfield(L, -2, "__index"); // (1)
331 lua_pushcfunction(L, lqtL_newindexfunc); // (2)
332 lua_setfield(L, -2, "__newindex"); // (1)
333 lua_pushcfunction(L, lqtL_gcfunc); // (2)
334 lua_setfield(L, -2, "__gc"); // (1)
335 lua_pushvalue(L, -1); // (2)
336 lua_setmetatable(L, -2); // (1)
337 lua_pop(L, 1); // (0)
338 lua_pushlstring(L, list->name, strlen(list->name)-1); // (1)
339 lua_newtable(L); // (2)
340 luaL_register(L, NULL, list->mt); // (2)
341 lua_settable(L, LUA_GLOBALSINDEX); // (0)
342 list++;
344 return 0;
347 bool lqtL_isinteger (lua_State *L, int i) {
348 if (lua_type(L, i)==LUA_TNUMBER)
349 return lua_tointeger(L, i)==lua_tonumber(L, i);
350 else
351 return false;
353 bool lqtL_isnumber (lua_State *L, int i) {
354 return lua_type(L, i)==LUA_TNUMBER;
356 bool lqtL_isstring (lua_State *L, int i) {
357 return lua_type(L, i)==LUA_TSTRING;
359 bool lqtL_isboolean (lua_State *L, int i) {
360 return lua_type(L, i)==LUA_TBOOLEAN;
362 bool lqtL_missarg (lua_State *L, int index, int n) {
363 bool ret = true;
364 int i = 0;
365 for (i=index;i<index+n;i++) {
366 if (!lua_isnoneornil(L, i)) {
367 ret = false;
368 break;
371 return ret;
374 static void CS(lua_State *L) {
375 qDebug() << "++++++++++";
376 for (int i=1;i<=lua_gettop(L);i++) {
377 qDebug() << luaL_typename(L, i) << lua_touserdata(L, i);
379 qDebug() << "----------";
382 static void lqtL_ensurepointer (lua_State *L, const void *p) { // (+1)
383 lqtL_getpointertable(L); // (1)
384 lua_pushlightuserdata(L, const_cast<void*>(p)); // (2)
385 lua_gettable(L, -2); // (2)
386 if (lua_isnil(L, -1)) { // (2)
387 lua_pop(L, 1); // (1)
388 const void **pp = static_cast<const void**>(lua_newuserdata(L, sizeof(void*))); // (2)
389 *pp = p; // (2)
390 lua_newtable(L); // (3)
391 lua_setfenv(L, -2); // (2)
392 lua_pushlightuserdata(L, const_cast<void*>(p)); // (3)
393 lua_pushvalue(L, -2); // (4)
394 lua_settable(L, -4); // (2)
395 } else {
396 //const void **pp = static_cast<const void**>(lua_touserdata(L, -1)); // (2)
397 //if (pp!=NULL) *pp = p; // (2)
399 // (2)
400 lua_remove(L, -2); // (1)
403 void lqtL_register (lua_State *L, const void *p) { // (+0)
404 lqtL_ensurepointer(L, p);
405 lua_pop(L, 1);
408 void lqtL_unregister (lua_State *L, const void *p) {
409 lqtL_getpointertable(L); // (1)
410 lua_pushlightuserdata(L, const_cast<void*>(p)); // (2)
411 lua_gettable(L, -2); // (2)
412 if (lua_isuserdata(L, -1)) {
413 const void **pp = static_cast<const void**>(lua_touserdata(L, -1)); // (2)
414 *pp = 0;
416 lua_pop(L, 1); // (1)
417 lua_pushlightuserdata(L, const_cast<void*>(p)); // (2)
418 lua_pushnil(L); // (3)
419 lua_settable(L, -3); // (1)
420 lua_pop(L, 1); // (0)
423 void lqtL_pushudata (lua_State *L, const void *p, const char *name) {
424 bool already = false;
425 lqtL_ensurepointer(L, p); // (1)
426 if (lua_getmetatable(L, -1)) {
427 // (2)
428 lua_pop(L, 1); // (1)
429 lua_getfield(L, -1, name); // (2)
430 already = lua_toboolean(L, -1); // (2)
431 lua_pop(L, 1); // (1)
432 } else {
433 // (1)
435 if (!already) {
436 luaL_newmetatable(L, name); // (2)
437 lua_setmetatable(L, -2); // (1)
439 return;
442 void lqtL_passudata (lua_State *L, const void *p, const char *name) {
443 lqtL_pushudata(L, p, name);
444 // FIXME: these should be added, but it is not safe for now
445 lua_getfield(L, -1, "delete");
446 lua_setfield(L, -2, "__gc");
447 return;
450 void lqtL_copyudata (lua_State *L, const void *p, const char *name) {
451 luaL_newmetatable(L, name);
452 lua_pushstring(L, "new");
453 lua_rawget(L, -2);
454 if (lua_isnil(L, -1)) {
455 qDebug() << "cannot copy" << name;
456 lua_pop(L, 2);
457 lua_pushnil(L);
458 } else {
459 lua_remove(L, -2);
460 lqtL_pushudata(L, p, name);
461 if (lua_pcall(L, 1, 1, 0)) {
462 qDebug() << "error copying" << name;
463 lua_pop(L, 1);
464 lua_pushnil(L);
467 return;
470 void *lqtL_toudata (lua_State *L, int index, const char *name) {
471 void *ret = 0;
472 if (!lqtL_testudata(L, index, name)) return 0;
473 void **pp = static_cast<void**>(lua_touserdata(L, index));
474 ret = *pp;
475 lua_getfield(L, index, name);
476 ret = (void*)(lua_tointeger(L, -1) + (char*)ret);
477 lua_pop(L, 1);
478 return ret;
481 void lqtL_eraseudata (lua_State *L, int index, const char *name) {
482 void *ret = 0;
483 if (name!=NULL && !lqtL_testudata(L, index, name)) return;
484 void **pp = static_cast<void**>(lua_touserdata(L, index));
485 void *p = *pp;
486 *pp = 0;
487 lqtL_getpointertable(L); // (1)
488 lua_pushlightuserdata(L, p); // (2)
489 lua_pushnil(L); // (3)
490 lua_settable(L, -3); // (1)
491 lua_pop(L, 1);
492 return;
495 bool lqtL_testudata (lua_State *L, int index, const char *name) {
496 if (!lua_isuserdata(L, index) || lua_islightuserdata(L, index)) return false;
497 lua_getfield(L, index, name);
498 if (lua_isnil(L, -1)) {
499 lua_pop(L, 1);
500 return false;
502 lua_pop(L, 1);
503 return true;
506 void lqtL_pushenum (lua_State *L, int value, const char *name) {
507 lqtL_getenumtable(L);
508 lua_getfield(L, -1, name);
509 lua_remove(L, -2);
510 if (!lua_istable(L, -1)) {
511 lua_pop(L, 1);
512 lua_pushnil(L);
513 return;
515 lua_pushnumber(L, value);
516 lua_gettable(L, -2);
517 lua_remove(L, -2);
520 bool lqtL_isenum (lua_State *L, int index, const char *name) {
521 bool ret = false;
522 if (!lua_isstring(L, index)) return false;
523 lqtL_getenumtable(L);
524 lua_getfield(L, -1, name);
525 if (!lua_istable(L, -1)) {
526 lua_pop(L, 2);
527 return false;
529 lua_remove(L, -2);
530 lua_pushvalue(L, index);
531 lua_gettable(L, -2);
532 ret = !lua_isnil(L, -1);
533 lua_pop(L, 2);
534 return ret;
537 int lqtL_toenum (lua_State *L, int index, const char *name) {
538 int ret = -1;
539 // index = LQT_TOPOSITIVE(L, index);
540 lqtL_getenumtable(L); // (1)
541 lua_getfield(L, -1, name); // (2)
542 if (lua_isnil(L, -1)) {
543 lua_pop(L, 2); //(0)
544 return 0;
546 lua_pushvalue(L, index); // (3)
547 lua_gettable(L, -2); // (3)
548 if (lqtL_isinteger(L, -1)) {
549 ret = lua_tointeger(L, -1); // (3)
550 } else {
551 ret = lua_tointeger(L, index); // (3)
553 lua_pop(L, 3); // (0)
554 return ret;
557 int lqtL_getflags (lua_State *L, int index, const char *name) {
558 int ret = 0;
559 int eindex = 0;
560 int i = 1;
561 if (lqtL_isinteger(L, index)) return lua_tointeger(L, index);
562 if (!lua_istable(L, index)) return 0;
563 lqtL_getenumtable(L); // (1)
564 lua_getfield(L, -1, name); // (2)
565 if (!lua_istable(L, -1)) {
566 // (2)
567 lua_pop(L, 2);
568 return 0;
570 // (2)
571 lua_remove(L, -2); // (1)
572 eindex = lua_gettop(L);
573 for (i=1;;i++) { // (1)
574 lua_rawgeti(L, index, i); // (2)
575 if (lua_type(L, -1)!=LUA_TSTRING) {
576 lua_pop(L, 1); // (1)
577 break;
578 } else {
579 lua_gettable(L, eindex); // (2)
580 ret = ret | (int)lua_tointeger(L, -1);
581 lua_pop(L, 1); // (1)
584 // (1)
585 lua_pop(L, 1); // (0)
586 return ret;
589 void lqtL_pushflags (lua_State *L, int index, const char *name) {
590 // TODO
591 lua_pushnil(L);
592 return;
595 int lqtL_touintarray (lua_State *L) {
596 uint *p = NULL;
597 size_t i = 0;
598 size_t n, nb;
599 n = lua_objlen(L, -1);
600 nb = (n + 1) * sizeof(uint);
601 p = (uint*)lua_newuserdata(L, nb);
602 for (i=1;i<=n;i++) {
603 lua_rawgeti(L, -2, i);
604 p[i-1] = lua_tointeger(L, -1);
605 lua_pop(L, 1);
607 lua_remove(L, -2);
608 p[n] = 0;
609 return 1;