Convert NULL pointer to nil. Fixes #17
[lqt/mk.git] / common / lqt_common.cpp
blob9c0d8db02a129a4ea6ddc0430748931214a06897
1 /*
2 * Copyright (c) 2007-2008 Mauro Iazzi
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 <iostream>
29 #include <cstdlib>
30 #include <cstring>
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) == 1;
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 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 (lqtL_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 if (!lua_isuserdata(L, 1) && lua_islightuserdata(L, 1)) return 0;
199 lua_getmetatable(L, 1);
200 lua_pushliteral(L, "__set");
201 lua_rawget(L, -2);
202 if (lua_istable(L, -1)) {
203 lua_pushvalue(L, 2);
204 lua_gettable(L, -2);
205 if (lua_isfunction(L, -1)) {
206 lua_CFunction setter = lua_tocfunction(L, -1);
207 if (!setter) return luaL_error(L, "Invalid setter %s", lua_tostring(L, 2));
208 return setter(L);
211 lua_settop(L, 3); // (=3)
212 lua_getfenv(L, 1); // (+1)
213 if (!lua_istable(L, -1)) {
214 lua_pop(L, 1); // (+0)
215 return 0;
217 lua_remove(L, 1); // (+0)
218 lua_insert(L, 1); // (+0)
219 lua_rawset(L, 1); // (-2)
220 return 0;
223 int lqtL_getoverload (lua_State *L, int index, const char *name) {
224 luaL_checkstack(L, 2, "no space to grow");
225 if (lua_isuserdata(L, index) && !lua_islightuserdata(L, index)) {
226 lua_getfenv(L, index); // (1)
227 lua_getfield(L, -1, name); // (2)
228 lua_remove(L, -2); // (1)
229 } else {
230 lua_pushnil(L); // (1)
232 return 1;
235 static int lqtL_indexfunc (lua_State *L) {
236 int i = 1;
237 if (lua_isuserdata(L, 1) && !lua_islightuserdata(L, 1)) {
238 lua_getmetatable(L, 1);
239 lua_pushliteral(L, "__get");
240 lua_rawget(L, -2);
241 if (lua_istable(L, -1)) {
242 lua_pushvalue(L, 2);
243 lua_gettable(L, -2);
244 if (lua_isfunction(L, -1)) {
245 lua_CFunction getter = lua_tocfunction(L, -1);
246 if (!getter) return luaL_error(L, "Invalid getter %s", lua_tostring(L, 2));
247 return getter(L);
250 lua_settop(L, 2);
251 lua_getfenv(L, 1); // (1)
252 lua_pushvalue(L, 2); // (2)
253 lua_gettable(L, -2); // (2)
254 if (!lua_isnil(L, -1)) {
255 lua_remove(L, -2);
256 return 1;
258 lua_pop(L, 2); // (0)
260 lua_pushnil(L); // (+1)
261 while (!lua_isnone(L, lua_upvalueindex(i))) { // (+1)
262 lua_pop(L, 1); // (+0)
263 lua_pushvalue(L, 2); // (+1)
264 if (i==1) {
265 lua_rawget(L, lua_upvalueindex(i)); // (+1)
266 } else {
267 lua_gettable(L, lua_upvalueindex(i)); // (+1)
269 if (!lua_isnil(L, -1)) break;
270 i++;
272 return 1; // (+1)
275 static int lqtL_pushindexfunc (lua_State *L, const char *name, lqt_Base *bases) {
276 int upnum = 1;
277 luaL_newmetatable(L, name); // (1)
278 while (bases->basename!=NULL) {
279 luaL_newmetatable(L, bases->basename); // (upnum)
280 upnum++;
281 bases++;
283 lua_pushcclosure(L, lqtL_indexfunc, upnum); // (1)
284 return 1;
287 static int lqtL_local_ctor(lua_State*L) {
288 lua_getfield(L, 1, "new"); // (+2)
289 lua_replace(L, 1); // (+2)
290 lua_call(L, lua_gettop(L)-1, LUA_MULTRET);// (X)
291 lua_getfield(L, 1, "delete"); // (X+1)
292 lua_setfield(L, 1, "__gc"); // (X)
293 return lua_gettop(L);
296 int lqtL_createclass (lua_State *L, const char *name, luaL_Reg *mt, luaL_Reg *getters, luaL_Reg *setters, lqt_Base *bases) {
297 int len = 0;
298 char *new_name = NULL;
299 lqt_Base *bi = bases;
300 luaL_newmetatable(L, name); // (1)
301 luaL_register(L, NULL, mt); // (1)
302 // setup offsets
303 lua_pushstring(L, name); // (2) FIXME: remove
304 lua_pushinteger(L, 0); // (3) FIXME: remove
305 lua_settable(L, -3); // (1) FIXME: remove
306 while (bi->basename!=NULL) {
307 lua_pushstring(L, bi->basename); // (2) FIXME: remove
308 lua_pushinteger(L, bi->offset); // (3) FIXME: remove
309 lua_settable(L, -3); // (1) FIXME: remove
310 bi++;
313 if (getters) {
314 lua_newtable(L);
315 luaL_register(L, NULL, getters);
316 lua_setfield(L, -2, "__get");
318 if (setters) {
319 lua_newtable(L);
320 luaL_register(L, NULL, setters);
321 lua_setfield(L, -2, "__set");
324 // set metafunctions
325 lqtL_pushindexfunc(L, name, bases); // (2)
326 lua_setfield(L, -2, "__index"); // (1)
327 lua_pushcfunction(L, lqtL_newindexfunc); // (2)
328 lua_setfield(L, -2, "__newindex"); // (1)
329 lua_pushcfunction(L, lqtL_gcfunc); // (2)
330 lua_setfield(L, -2, "__gc"); // (1)
331 lua_pushstring(L, name);
332 lua_setfield(L, -2, "__type");
334 // set it as its own metatable
335 lua_pushvalue(L, -1); // (2)
336 lua_setmetatable(L, -2); // (1)
337 lua_pop(L, 1); // (0)
338 len = strlen(name);
339 new_name = (char*)malloc(len*sizeof(char));
340 strncpy(new_name, name, len);
341 new_name[len-1] = '\0';
342 luaL_register(L, new_name, mt); // (1)
343 free(new_name);
344 new_name = NULL;
345 lua_newtable(L); // (2)
346 lua_pushcfunction(L, lqtL_local_ctor);
347 lua_setfield(L, -2, "__call"); // (2)
348 lua_setmetatable(L, -2); // (1)
349 lua_pop(L, 1); // (0)
351 lua_pushlstring(L, name, strlen(name)-1); // (1)
352 lua_newtable(L); // (2)
353 luaL_newmetatable(L, name); // (3)
354 lua_setmetatable(L, -2); // (2)
355 // don't register again but use metatable
356 //luaL_register(L, NULL, mt); // (2)
357 lua_settable(L, LUA_GLOBALSINDEX); // (0)
359 return 0;
362 bool lqtL_isinteger (lua_State *L, int i) {
363 if (lua_type(L, i)==LUA_TNUMBER) {
364 return lua_tointeger(L, i)==lua_tonumber(L, i);
365 } else {
366 return false;
369 bool lqtL_isnumber (lua_State *L, int i) {
370 return lua_type(L, i)==LUA_TNUMBER;
372 bool lqtL_isstring (lua_State *L, int i) {
373 return lua_type(L, i)==LUA_TSTRING;
375 bool lqtL_isboolean (lua_State *L, int i) {
376 return lua_type(L, i)==LUA_TBOOLEAN;
378 bool lqtL_missarg (lua_State *L, int index, int n) {
379 bool ret = true;
380 int i = 0;
381 for (i=index;i<index+n;i++) {
382 if (!lua_isnoneornil(L, i)) {
383 ret = false;
384 break;
387 return ret;
390 static void CS(lua_State *L) {
391 std::cerr << "++++++++++" << std::endl;
392 for (int i=1;i<=lua_gettop(L);i++) {
393 std::cerr << luaL_typename(L, i) << " " << lua_touserdata(L, i) << std::endl;
395 std::cerr << "----------" << std::endl;
398 static void lqtL_ensurepointer (lua_State *L, const void *p) { // (+1)
399 lqtL_getpointertable(L); // (1)
400 lua_pushlightuserdata(L, const_cast<void*>(p)); // (2)
401 lua_gettable(L, -2); // (2)
402 if (lua_isnil(L, -1)) { // (2)
403 lua_pop(L, 1); // (1)
404 const void **pp = static_cast<const void**>(lua_newuserdata(L, sizeof(void*))); // (2)
405 *pp = p; // (2)
406 lua_newtable(L); // (3)
407 lua_setfenv(L, -2); // (2)
408 lua_pushlightuserdata(L, const_cast<void*>(p)); // (3)
409 lua_pushvalue(L, -2); // (4)
410 lua_settable(L, -4); // (2)
411 } else {
412 //const void **pp = static_cast<const void**>(lua_touserdata(L, -1)); // (2)
413 //if (pp!=NULL) *pp = p; // (2)
415 // (2)
416 lua_remove(L, -2); // (1)
419 void lqtL_register (lua_State *L, const void *p) { // (+0)
420 lqtL_ensurepointer(L, p);
421 lua_pop(L, 1);
424 void lqtL_unregister (lua_State *L, const void *p) {
425 lqtL_getpointertable(L); // (1)
426 lua_pushlightuserdata(L, const_cast<void*>(p)); // (2)
427 lua_gettable(L, -2); // (2)
428 if (lua_isuserdata(L, -1)) {
429 const void **pp = static_cast<const void**>(lua_touserdata(L, -1)); // (2)
430 *pp = 0;
432 lua_pop(L, 1); // (1)
433 lua_pushlightuserdata(L, const_cast<void*>(p)); // (2)
434 lua_pushnil(L); // (3)
435 lua_settable(L, -3); // (1)
436 lua_pop(L, 1); // (0)
439 void lqtL_pushudata (lua_State *L, const void *p, const char *name) {
440 bool already = false;
442 if (p == NULL) {
443 lua_pushnil(L); // (1)
444 return;
447 lqtL_ensurepointer(L, p); // (1)
448 if (lua_getmetatable(L, -1)) {
449 // (2)
450 lua_pop(L, 1); // (1)
451 lua_getfield(L, -1, name); // (2)
452 already = lua_toboolean(L, -1) == 1; // (2)
453 lua_pop(L, 1); // (1)
454 } else {
455 // (1)
457 if (!already) {
458 luaL_newmetatable(L, name); // (2)
459 lua_setmetatable(L, -2); // (1)
461 return;
464 void lqtL_passudata (lua_State *L, const void *p, const char *name) {
465 lqtL_pushudata(L, p, name);
466 // used only when passing temporaries - should be deleted afterwards
467 lua_getfield(L, -1, "delete");
468 lua_setfield(L, -2, "__gc");
469 return;
472 void lqtL_copyudata (lua_State *L, const void *p, const char *name) {
473 luaL_newmetatable(L, name);
474 lua_pushstring(L, "new");
475 lua_rawget(L, -2);
476 if (lua_isnil(L, -1)) {
477 std::cerr << "cannot copy " << name << std::endl;
478 lua_pop(L, 2);
479 lua_pushnil(L);
480 } else {
481 lua_remove(L, -2);
482 lqtL_pushudata(L, p, name);
483 if (lqtL_pcall(L, 1, 1, 0)) {
484 std::cerr << "error copying " << name << std::endl;
485 lua_pop(L, 1);
486 lua_pushnil(L);
488 // Enable autodeletion for copied stuff
489 lua_getfield(L, -1, "delete");
490 lua_setfield(L, -2, "__gc");
492 return;
495 void *lqtL_toudata (lua_State *L, int index, const char *name) {
496 void *ret = 0;
497 if (!lqtL_testudata(L, index, name)) return 0;
498 void **pp = static_cast<void**>(lua_touserdata(L, index));
499 ret = *pp;
500 lua_getfield(L, index, name);
501 ret = (void*)(lua_tointeger(L, -1) + (char*)ret);
502 lua_pop(L, 1);
503 return ret;
506 void lqtL_eraseudata (lua_State *L, int index, const char *name) {
507 void *ret = 0;
508 if (name!=NULL && !lqtL_testudata(L, index, name)) return;
509 void **pp = static_cast<void**>(lua_touserdata(L, index));
510 void *p = *pp;
511 *pp = 0;
512 lqtL_getpointertable(L); // (1)
513 lua_pushlightuserdata(L, p); // (2)
514 lua_pushnil(L); // (3)
515 lua_settable(L, -3); // (1)
516 lua_pop(L, 1);
517 return;
520 bool lqtL_testudata (lua_State *L, int index, const char *name) {
521 if (!lua_isuserdata(L, index) || lua_islightuserdata(L, index)) return false;
522 lua_getfield(L, index, name);
523 if (lua_isnil(L, -1)) {
524 lua_pop(L, 1);
525 return false;
527 lua_pop(L, 1);
528 return true;
531 const char * lqtL_pushtrace(lua_State *L) {
532 lua_getglobal(L, "debug");
533 lua_getfield(L, -1, "traceback");
534 lua_remove(L, -2);
535 lua_call(L, 0, 1);
536 return lua_tostring(L, -1);
539 void lqtL_pushenum (lua_State *L, int value, const char *name) {
540 lqtL_getenumtable(L);
541 lua_getfield(L, -1, name);
542 lua_remove(L, -2);
543 if (!lua_istable(L, -1)) {
544 lua_pop(L, 1);
545 lua_pushnil(L);
546 return;
548 lua_pushnumber(L, value);
549 lua_gettable(L, -2);
550 lua_remove(L, -2);
553 bool lqtL_isenum (lua_State *L, int index, const char *name) {
554 bool ret = false;
555 if (!lua_isstring(L, index)) return false;
556 lqtL_getenumtable(L);
557 lua_getfield(L, -1, name);
558 if (!lua_istable(L, -1)) {
559 lua_pop(L, 2);
560 return false;
562 lua_remove(L, -2);
563 lua_pushvalue(L, index);
564 lua_gettable(L, -2);
565 ret = !lua_isnil(L, -1);
566 lua_pop(L, 2);
567 return ret;
570 int lqtL_toenum (lua_State *L, int index, const char *name) {
571 int ret = -1;
572 // index = LQT_TOPOSITIVE(L, index);
573 lqtL_getenumtable(L); // (1)
574 lua_getfield(L, -1, name); // (2)
575 if (lua_isnil(L, -1)) {
576 lua_pop(L, 2); //(0)
577 return 0;
579 lua_pushvalue(L, index); // (3)
580 lua_gettable(L, -2); // (3)
581 if (lqtL_isinteger(L, -1)) {
582 ret = lua_tointeger(L, -1); // (3)
583 } else {
584 ret = lua_tointeger(L, index); // (3)
586 lua_pop(L, 3); // (0)
587 return ret;
590 int lqtL_getflags (lua_State *L, int index, const char *name) {
591 int ret = 0;
592 int eindex = 0;
593 int i = 1;
594 if (lqtL_isinteger(L, index)) return lua_tointeger(L, index);
595 if (!lua_istable(L, index)) return 0;
596 lqtL_getenumtable(L); // (1)
597 lua_getfield(L, -1, name); // (2)
598 if (!lua_istable(L, -1)) {
599 // (2)
600 lua_pop(L, 2);
601 return 0;
603 // (2)
604 lua_remove(L, -2); // (1)
605 eindex = lua_gettop(L);
606 for (i=1;;i++) { // (1)
607 lua_rawgeti(L, index, i); // (2)
608 if (lua_type(L, -1)!=LUA_TSTRING) {
609 lua_pop(L, 1); // (1)
610 break;
611 } else {
612 lua_gettable(L, eindex); // (2)
613 ret = ret | (int)lua_tointeger(L, -1);
614 lua_pop(L, 1); // (1)
617 // (1)
618 lua_pop(L, 1); // (0)
619 return ret;
622 //#include <QDebug>
624 void lqtL_pushflags (lua_State *L, int value, const char *name) {
625 int index = 1;
626 lua_newtable(L); // (1) return value
627 lqtL_getenumtable(L); // (2)
628 lua_getfield(L, -1, name); // (3)
629 lua_remove(L, -2); // (2) stack: ret, enumtable
630 lua_pushnil(L); // (3) first index
631 while (lua_next(L, -2) != 0) { // (4) stack: ret, enumtable, index, value
632 //if (lua_isnumber(L, -2)) {
633 //qDebug() << ((void*)lua_tointeger(L, -2))
634 //<< ((void*)value) << (void*)(lua_tointeger(L, -2)&value)
635 //<< ((lua_tointeger(L, -2)&value)==lua_tointeger(L, -2)) << lua_tostring(L, -1);
637 if (lua_isnumber(L, -2) &&
638 ((lua_tointeger(L, -2)&value)==lua_tointeger(L, -2))) {
639 // (4) if index is the value
640 lua_rawseti(L, -4, index); // (3) the string is put into the ret table
641 index = index + 1; // (3) the size of the ret table has increased
642 } else {
643 lua_pop(L, 1); // (3) pop the value
646 // (2) lua_next pops the vale and pushes nothing at the end of the iteration
647 // (2) stack: ret, enumtable
648 lua_pop(L, 1); // (1) stack: ret
649 return;
652 int lqtL_touintarray (lua_State *L) {
653 unsigned int *p = NULL;
654 size_t i = 0;
655 size_t n, nb;
656 n = lua_objlen(L, -1);
657 nb = (n + 1) * sizeof(unsigned int);
658 p = (unsigned int *)lua_newuserdata(L, nb);
659 for (i=1;i<=n;i++) {
660 lua_rawgeti(L, -2, i);
661 p[i-1] = lua_tointeger(L, -1);
662 lua_pop(L, 1);
664 lua_remove(L, -2);
665 p[n] = 0;
666 return 1;
669 int lqtL_pcall_debug_default (lua_State *L, int narg, int nres, int err) {
670 int status = 0;
671 std::cerr << "entering a pcall" << std::endl;
672 status = lua_pcall(L, narg, nres, err);
673 std::cerr << "pcall finished with status " << status << std::endl;
674 return status;
677 int lqtL_pcall_debug (lua_State *L, int narg, int nres, int err) {
678 int status = 0;
679 lua_getfield(L, LUA_REGISTRYINDEX, LQT_PCALL);
680 lqt_PCallPtr pcall = (lqt_PCallPtr)lua_touserdata(L, -1);
681 lua_pop(L, 1);
682 if (pcall) {
683 status = pcall(L, narg, nres, err);
684 } else {
685 status = lqtL_pcall_debug_default(L, narg, nres, err);
687 return status;
690 void lqtL_pushudatatype (lua_State *L, int index) {
691 if (!lua_isuserdata(L, index) || lua_islightuserdata(L, index)) {
692 lua_pushstring(L, luaL_typename(L, index));
693 } else {
694 lua_getfield(L, index, "__type");
695 if (lua_isnil(L, -1)) {
696 lua_pop(L, 1);
697 lua_pushstring(L, luaL_typename(L, index));
702 const char * lqtL_getarglist (lua_State *L) {
703 int args = lua_gettop(L);
704 lua_checkstack(L, args * 2);
705 lua_pushliteral(L, "");
706 for(int i = 1; i <= args; i++) {
707 lqtL_pushudatatype(L, i);
708 if (i<args)
709 lua_pushliteral(L, ", ");
711 lua_concat(L, 2*args - 1);
712 return lua_tostring(L, -1);
715 const char * lqtL_source(lua_State *L, int idx) {
716 static char buf[1024]; // TODO: try something better
717 lua_Debug ar = {0};
718 lua_pushvalue(L, idx);
719 lua_getinfo(L, ">S", &ar);
720 if (ar.source[0] != '@') {
721 sprintf(buf, "%s", ar.source);
722 } else {
723 sprintf(buf, "%s %s:%d", ar.name, ar.source, ar.linedefined);
725 return buf;
728 bool lqtL_is_super(lua_State *L, int idx) {
729 lua_getfield(L, LUA_GLOBALSINDEX, LQT_SUPER);
730 void *super = lua_touserdata(L, -1);
731 void *comp = lua_touserdata(L, idx);
732 bool ret = lua_equal(L, -1, idx);
733 lua_pop(L, 1);
734 return ret;
737 void lqtL_register_super(lua_State *L) {
738 lua_getfield(L, LUA_GLOBALSINDEX, LQT_SUPER);
739 if (lua_isnil(L, -1)) {
740 void *ud = lua_newuserdata(L, sizeof(int));
741 lua_setfield(L, LUA_GLOBALSINDEX, LQT_SUPER);
742 } else {
743 lua_pop(L, -1);
747 // returns true if the value at index `n` can be converted to `to_type`
748 bool lqtL_canconvert(lua_State *L, int n, const char *to_type) {
749 if (lqtL_testudata(L, n, to_type))
750 return true;
751 int oldtop = lua_gettop(L);
752 luaL_getmetatable(L, to_type);
753 if (lua_isnil(L, -1)) {
754 lua_settop(L, oldtop);
755 return false;
757 lua_getfield(L, -1, "__test");
758 if (lua_isnil(L, -1)) {
759 lua_settop(L, oldtop);
760 return false;
762 lqt_testfunc func = (lqt_testfunc) lua_touserdata(L, -1);
763 lua_settop(L, oldtop);
764 return func(L, n);
767 // converts the value at index `n` to `to_type` and returns a pointer to it
768 void *lqtL_convert(lua_State *L, int n, const char *to_type) {
769 if (lqtL_testudata(L, n, to_type))
770 return lqtL_toudata(L, n, to_type);
771 int oldtop = lua_gettop(L);
772 luaL_getmetatable(L, to_type);
773 if (lua_isnil(L, -1)) {
774 lua_settop(L, oldtop);
775 return false;
777 lua_getfield(L, -1, "__convert");
778 if (lua_isnil(L, -1)) {
779 lua_settop(L, oldtop);
780 return false;
782 lqt_convertfunc func = (lqt_convertfunc) lua_touserdata(L, -1);
783 lua_settop(L, oldtop);
784 return func(L, n);
787 void lqtL_selfcheck(lua_State *L, void *self, const char *name) {
788 if (NULL==self) {
789 lua_pushfstring(L, "Instance of %s has already been deleted in:\n", name);
790 lqtL_pushtrace(L);
791 lua_concat(L, 2);
792 lua_error(L);