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
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"
32 static void lqtL_getenumtable (lua_State
*L
) {
33 lua_getfield(L
, LUA_REGISTRYINDEX
, LQT_ENUMS
);
34 if (lua_isnil(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
) {
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)
81 lua_rawseti(L
, -2, 1+lua_objlen(L
, -2)); // (1)
83 lua_pushinteger(L
, 1+lua_objlen(L
, -2)); // (3)
84 lua_settable(L
, -3); // (1)
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);
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);
104 void lqtL_pusharguments (lua_State
*L
, char **argv
) {
107 for (i
=0;*argv
/* fix the maximum number? */;argv
++,i
++) {
108 lua_pushstring(L
, *argv
);
109 lua_rawseti(L
, -2, i
+1);
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
;
120 lua_rawgeti(L
, index
, i
+1);
121 if (!lua_isstring(L
, -1)) {
122 str
= NULL
; strlen
= 0;
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);
136 static int lqtL_createenum (lua_State
*L
, lqt_Enum e
[], const char *n
) {
137 luaL_Reg empty
[] = { { 0, 0 } };
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)
152 lua_pop(L
, 2); // (0)
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)
164 lua_pop(L
, 1); // (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)
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)
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)
189 lua_pushvalue(L
, 1); // (2)
190 if (lqtL_pcall(L
, 1, 0, 0)) { // (-2;+1/+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)
205 lua_remove(L
, 1); // (+0)
206 lua_insert(L
, 1); // (+0)
207 lua_rawset(L
, 1); // (-2)
211 int lqtL_getoverload (lua_State
*L
, int index
, const char *name
) {
212 if (lua_isuserdata(L
, index
) && !lua_islightuserdata(L
, index
)) {
213 lua_getfenv(L
, index
); // (1)
214 lua_getfield(L
, -1, name
); // (2)
215 lua_remove(L
, -2); // (1)
217 lua_pushnil(L
); // (1)
222 static int lqtL_indexfunc (lua_State
*L
) {
224 if (lua_isuserdata(L
, 1) && !lua_islightuserdata(L
, 1)) {
225 lua_getfenv(L
, 1); // (1)
226 lua_pushvalue(L
, 2); // (2)
227 lua_gettable(L
, -2); // (2)
228 if (!lua_isnil(L
, -1)) {
232 lua_pop(L
, 2); // (0)
234 lua_pushnil(L
); // (+1)
235 while (!lua_isnone(L
, lua_upvalueindex(i
))) { // (+1)
236 lua_pop(L
, 1); // (+0)
237 lua_pushvalue(L
, 2); // (+1)
239 lua_rawget(L
, lua_upvalueindex(i
)); // (+1)
241 lua_gettable(L
, lua_upvalueindex(i
)); // (+1)
243 if (!lua_isnil(L
, -1)) break;
249 static int lqtL_pushindexfunc (lua_State
*L
, const char *name
, lqt_Base
*bases
) {
251 luaL_newmetatable(L
, name
); // (1)
252 while (bases
->basename
!=NULL
) {
253 luaL_newmetatable(L
, bases
->basename
); // (upnum)
257 lua_pushcclosure(L
, lqtL_indexfunc
, upnum
); // (1)
261 static int lqtL_ctor_helper(lua_State
*L
) {
262 lua_getfield(L
, 1, "new");
264 lua_call(L
, lua_gettop(L
)-1, LUA_MULTRET
);
265 return lua_gettop(L
);
268 static int lqtL_local_ctor(lua_State
*L
) {
269 lua_pushvalue(L
, lua_upvalueindex(1)); // (+1)
270 lua_getfield(L
, -1, "new"); // (+2)
271 lua_insert(L
, 1); // (+2)
272 lua_pop(L
, 1); // (+1)
273 lua_call(L
, lua_gettop(L
)-1, LUA_MULTRET
); // (X)
274 lua_getfield(L
, 1, "delete"); // (X+1)
275 lua_setfield(L
, 1, "__gc"); // (X)
276 return lua_gettop(L
);
279 int lqtL_createclass (lua_State
*L
, const char *name
, luaL_Reg
*mt
, lqt_Base
*bases
) {
281 char *new_name
= NULL
;
282 lqt_Base
*bi
= bases
;
283 luaL_newmetatable(L
, name
); // (1)
284 luaL_register(L
, NULL
, mt
); // (1)
286 lua_pushstring(L
, name
); // (2) FIXME: remove
287 lua_pushinteger(L
, 0); // (3) FIXME: remove
288 lua_settable(L
, -3); // (1) FIXME: remove
289 while (bi
->basename
!=NULL
) {
290 lua_pushstring(L
, bi
->basename
); // (2) FIXME: remove
291 lua_pushinteger(L
, bi
->offset
); // (3) FIXME: remove
292 lua_settable(L
, -3); // (1) FIXME: remove
296 lqtL_pushindexfunc(L
, name
, bases
); // (2)
297 lua_setfield(L
, -2, "__index"); // (1)
298 lua_pushcfunction(L
, lqtL_newindexfunc
); // (2)
299 lua_setfield(L
, -2, "__newindex"); // (1)
300 lua_pushcfunction(L
, lqtL_gcfunc
); // (2)
301 lua_setfield(L
, -2, "__gc"); // (1)
303 // set it as its own metatable
304 lua_pushvalue(L
, -1); // (2)
305 lua_setmetatable(L
, -2); // (1)
306 lua_pop(L
, 1); // (0)
308 new_name
= (char*)malloc(len
*sizeof(char));
309 strncpy(new_name
, name
, len
);
310 new_name
[len
-1] = '\0';
311 luaL_register(L
, new_name
, mt
); // (1)
314 lua_newtable(L
); // (2)
315 lua_pushcfunction(L
, lqtL_ctor_helper
); // (3)
316 lua_setfield(L
, -2, "__call"); // (2)
317 lua_setmetatable(L
, -2); // (1)
318 lua_pushvalue(L
, -1); // (2)
319 lua_pushcclosure(L
, lqtL_local_ctor
, 1); // (2)
320 lua_setfield(L
, -2, "new_local");
321 lua_pop(L
, 1); // (0)
323 lua_pushlstring(L, name, strlen(name)-1); // (1)
324 lua_newtable(L); // (2)
325 luaL_newmetatable(L, name); // (3)
326 lua_setmetatable(L, -2); // (2)
327 // don't register again but use metatable
328 //luaL_register(L, NULL, mt); // (2)
329 lua_settable(L, LUA_GLOBALSINDEX); // (0)
334 bool lqtL_isinteger (lua_State
*L
, int i
) {
335 if (lua_type(L
, i
)==LUA_TNUMBER
) {
336 return lua_tointeger(L
, i
)==lua_tonumber(L
, i
);
341 bool lqtL_isnumber (lua_State
*L
, int i
) {
342 return lua_type(L
, i
)==LUA_TNUMBER
;
344 bool lqtL_isstring (lua_State
*L
, int i
) {
345 return lua_type(L
, i
)==LUA_TSTRING
;
347 bool lqtL_isboolean (lua_State
*L
, int i
) {
348 return lua_type(L
, i
)==LUA_TBOOLEAN
;
350 bool lqtL_missarg (lua_State
*L
, int index
, int n
) {
353 for (i
=index
;i
<index
+n
;i
++) {
354 if (!lua_isnoneornil(L
, i
)) {
362 static void CS(lua_State
*L
) {
363 std::cerr
<< "++++++++++" << std::endl
;
364 for (int i
=1;i
<=lua_gettop(L
);i
++) {
365 std::cerr
<< luaL_typename(L
, i
) << " " << lua_touserdata(L
, i
) << std::endl
;
367 std::cerr
<< "----------" << std::endl
;
370 static void lqtL_ensurepointer (lua_State
*L
, const void *p
) { // (+1)
371 lqtL_getpointertable(L
); // (1)
372 lua_pushlightuserdata(L
, const_cast<void*>(p
)); // (2)
373 lua_gettable(L
, -2); // (2)
374 if (lua_isnil(L
, -1)) { // (2)
375 lua_pop(L
, 1); // (1)
376 const void **pp
= static_cast<const void**>(lua_newuserdata(L
, sizeof(void*))); // (2)
378 lua_newtable(L
); // (3)
379 lua_setfenv(L
, -2); // (2)
380 lua_pushlightuserdata(L
, const_cast<void*>(p
)); // (3)
381 lua_pushvalue(L
, -2); // (4)
382 lua_settable(L
, -4); // (2)
384 //const void **pp = static_cast<const void**>(lua_touserdata(L, -1)); // (2)
385 //if (pp!=NULL) *pp = p; // (2)
388 lua_remove(L
, -2); // (1)
391 void lqtL_register (lua_State
*L
, const void *p
) { // (+0)
392 lqtL_ensurepointer(L
, p
);
396 void lqtL_unregister (lua_State
*L
, const void *p
) {
397 lqtL_getpointertable(L
); // (1)
398 lua_pushlightuserdata(L
, const_cast<void*>(p
)); // (2)
399 lua_gettable(L
, -2); // (2)
400 if (lua_isuserdata(L
, -1)) {
401 const void **pp
= static_cast<const void**>(lua_touserdata(L
, -1)); // (2)
404 lua_pop(L
, 1); // (1)
405 lua_pushlightuserdata(L
, const_cast<void*>(p
)); // (2)
406 lua_pushnil(L
); // (3)
407 lua_settable(L
, -3); // (1)
408 lua_pop(L
, 1); // (0)
411 void lqtL_pushudata (lua_State
*L
, const void *p
, const char *name
) {
412 bool already
= false;
413 lqtL_ensurepointer(L
, p
); // (1)
414 if (lua_getmetatable(L
, -1)) {
416 lua_pop(L
, 1); // (1)
417 lua_getfield(L
, -1, name
); // (2)
418 already
= lua_toboolean(L
, -1); // (2)
419 lua_pop(L
, 1); // (1)
424 luaL_newmetatable(L
, name
); // (2)
425 lua_setmetatable(L
, -2); // (1)
430 void lqtL_passudata (lua_State
*L
, const void *p
, const char *name
) {
431 lqtL_pushudata(L
, p
, name
);
432 // FIXME: these should be added, but it is not safe for now
433 lua_getfield(L
, -1, "delete");
434 lua_setfield(L
, -2, "__gc");
438 void lqtL_copyudata (lua_State
*L
, const void *p
, const char *name
) {
439 luaL_newmetatable(L
, name
);
440 lua_pushstring(L
, "new");
442 if (lua_isnil(L
, -1)) {
443 std::cerr
<< "cannot copy " << name
<< std::endl
;
448 lqtL_pushudata(L
, p
, name
);
449 if (lqtL_pcall(L
, 1, 1, 0)) {
450 std::cerr
<< "error copying " << name
<< std::endl
;
454 // Enable autodeletion for copied stuff
455 lua_getfield(L
, -1, "delete");
456 lua_setfield(L
, -2, "__gc");
461 void *lqtL_toudata (lua_State
*L
, int index
, const char *name
) {
463 if (!lqtL_testudata(L
, index
, name
)) return 0;
464 void **pp
= static_cast<void**>(lua_touserdata(L
, index
));
466 lua_getfield(L
, index
, name
);
467 ret
= (void*)(lua_tointeger(L
, -1) + (char*)ret
);
472 void lqtL_eraseudata (lua_State
*L
, int index
, const char *name
) {
474 if (name
!=NULL
&& !lqtL_testudata(L
, index
, name
)) return;
475 void **pp
= static_cast<void**>(lua_touserdata(L
, index
));
478 lqtL_getpointertable(L
); // (1)
479 lua_pushlightuserdata(L
, p
); // (2)
480 lua_pushnil(L
); // (3)
481 lua_settable(L
, -3); // (1)
486 bool lqtL_testudata (lua_State
*L
, int index
, const char *name
) {
487 if (!lua_isuserdata(L
, index
) || lua_islightuserdata(L
, index
)) return false;
488 lua_getfield(L
, index
, name
);
489 if (lua_isnil(L
, -1)) {
497 void lqtL_pushenum (lua_State
*L
, int value
, const char *name
) {
498 lqtL_getenumtable(L
);
499 lua_getfield(L
, -1, name
);
501 if (!lua_istable(L
, -1)) {
506 lua_pushnumber(L
, value
);
511 bool lqtL_isenum (lua_State
*L
, int index
, const char *name
) {
513 if (!lua_isstring(L
, index
)) return false;
514 lqtL_getenumtable(L
);
515 lua_getfield(L
, -1, name
);
516 if (!lua_istable(L
, -1)) {
521 lua_pushvalue(L
, index
);
523 ret
= !lua_isnil(L
, -1);
528 int lqtL_toenum (lua_State
*L
, int index
, const char *name
) {
530 // index = LQT_TOPOSITIVE(L, index);
531 lqtL_getenumtable(L
); // (1)
532 lua_getfield(L
, -1, name
); // (2)
533 if (lua_isnil(L
, -1)) {
537 lua_pushvalue(L
, index
); // (3)
538 lua_gettable(L
, -2); // (3)
539 if (lqtL_isinteger(L
, -1)) {
540 ret
= lua_tointeger(L
, -1); // (3)
542 ret
= lua_tointeger(L
, index
); // (3)
544 lua_pop(L
, 3); // (0)
548 int lqtL_getflags (lua_State
*L
, int index
, const char *name
) {
552 if (lqtL_isinteger(L
, index
)) return lua_tointeger(L
, index
);
553 if (!lua_istable(L
, index
)) return 0;
554 lqtL_getenumtable(L
); // (1)
555 lua_getfield(L
, -1, name
); // (2)
556 if (!lua_istable(L
, -1)) {
562 lua_remove(L
, -2); // (1)
563 eindex
= lua_gettop(L
);
564 for (i
=1;;i
++) { // (1)
565 lua_rawgeti(L
, index
, i
); // (2)
566 if (lua_type(L
, -1)!=LUA_TSTRING
) {
567 lua_pop(L
, 1); // (1)
570 lua_gettable(L
, eindex
); // (2)
571 ret
= ret
| (int)lua_tointeger(L
, -1);
572 lua_pop(L
, 1); // (1)
576 lua_pop(L
, 1); // (0)
582 void lqtL_pushflags (lua_State
*L
, int value
, const char *name
) {
584 lua_newtable(L
); // (1) return value
585 lqtL_getenumtable(L
); // (2)
586 lua_getfield(L
, -1, name
); // (3)
587 lua_remove(L
, -2); // (2) stack: ret, enumtable
588 lua_pushnil(L
); // (3) first index
589 while (lua_next(L
, -2) != 0) { // (4) stack: ret, enumtable, index, value
590 //if (lua_isnumber(L, -2)) {
591 //qDebug() << ((void*)lua_tointeger(L, -2))
592 //<< ((void*)value) << (void*)(lua_tointeger(L, -2)&value)
593 //<< ((lua_tointeger(L, -2)&value)==lua_tointeger(L, -2)) << lua_tostring(L, -1);
595 if (lua_isnumber(L
, -2) &&
596 ((lua_tointeger(L
, -2)&value
)==lua_tointeger(L
, -2))) {
597 // (4) if index is the value
598 lua_rawseti(L
, -4, index
); // (3) the string is put into the ret table
599 index
= index
+ 1; // (3) the size of the ret table has increased
601 lua_pop(L
, 1); // (3) pop the value
604 // (2) lua_next pops the vale and pushes nothing at the end of the iteration
605 // (2) stack: ret, enumtable
606 lua_pop(L
, 1); // (1) stack: ret
610 int lqtL_touintarray (lua_State
*L
) {
611 unsigned int *p
= NULL
;
614 n
= lua_objlen(L
, -1);
615 nb
= (n
+ 1) * sizeof(unsigned int );
616 p
= (unsigned int *)lua_newuserdata(L
, nb
);
618 lua_rawgeti(L
, -2, i
);
619 p
[i
-1] = lua_tointeger(L
, -1);
627 int lqtL_pcall_debug_default (lua_State
*L
, int narg
, int nres
, int err
) {
629 std::cerr
<< "entering a pcall" << std::endl
;
630 status
= lua_pcall(L
, narg
, nres
, err
);
631 std::cerr
<< "pcall finished with status " << status
<< std::endl
;
635 int lqtL_pcall_debug (lua_State
*L
, int narg
, int nres
, int err
) {
637 lua_getfield(L
, LUA_REGISTRYINDEX
, LQT_PCALL
);
638 lqt_PCallPtr pcall
= (lqt_PCallPtr
)lua_touserdata(L
, -1);
641 status
= pcall(L
, narg
, nres
, err
);
643 status
= lqtL_pcall_debug_default(L
, narg
, nres
, err
);