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
, "kv"); // (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
) {
72 lqtL_getreftable(L
); // (1)
73 ret
= lua_newuserdata(L
, sz
); // (2)
74 lua_rawseti(L
, -2, 1+lua_objlen(L
, -2));
78 int * lqtL_tointref (lua_State
*L
, int index
) {
79 int *ret
= (int*)lqtL_getref(L
, sizeof(int));
80 *ret
= lua_tointeger(L
, index
);
84 void lqtL_pusharguments (lua_State
*L
, char **argv
) {
87 for (i
=0;*argv
/* fix the maximum number? */;argv
++,i
++) {
88 lua_pushstring(L
, *argv
);
89 lua_rawseti(L
, -2, i
+1);
94 char ** lqtL_toarguments (lua_State
*L
, int index
) {
95 char ** ret
= (char**)lqtL_getref(L
, sizeof(char*)*(lua_objlen(L
, index
)+1));
96 const char *str
= NULL
;
100 lua_rawgeti(L
, index
, i
+1);
101 if (!lua_isstring(L
, -1)) {
102 str
= NULL
; strlen
= 0;
107 str
= lua_tolstring(L
, -1, &strlen
);
108 ret
[i
] = (char*)lqtL_getref(L
, sizeof(char)*(strlen
+1));
109 strncpy(ret
[i
], str
, strlen
+1);
116 static int lqtL_createenum (lua_State
*L
, lqt_Enum e
[], const char *n
) {
117 lqtL_getenumtable(L
); // (1)
118 lua_newtable(L
); // (2)
119 lua_pushvalue(L
, -1); // (3)
120 lua_setfield(L
, -3, n
); // (2)
121 while ( (e
->name
!=0) ) { // (2)
122 lua_pushstring(L
, e
->name
); // (3)
123 lua_pushinteger(L
, e
->value
); // (4)
124 lua_settable(L
, -3); // (2)
125 lua_pushinteger(L
, e
->value
); // (3)
126 lua_pushstring(L
, e
->name
); // (4)
127 lua_settable(L
, -3); // (2)
130 lua_pop(L
, 2); // (0)
134 int lqtL_createenumlist (lua_State
*L
, lqt_Enumlist list
[]) {
135 while (list
->enums
!=0 && list
->name
!=0) {
136 lqtL_createenum(L
, list
->enums
, list
->name
); // (0)
142 static int lqtL_gcfunc (lua_State
*L
) {
143 if (!lua_isuserdata(L
, 1) && lua_islightuserdata(L
, 1)) return 0;
144 lua_getfenv(L
, 1); // (1)
145 if (!lua_istable(L
, -1)) {
146 lua_pop(L
, 1); // (0)
149 lua_getfield(L
, -1, "__gc"); // (2)
150 lua_remove(L
, -2); // (1)
151 if (!lua_isfunction(L
, -1)) {
152 lua_pop(L
, 1); // (0)
155 lua_pushvalue(L
, 1); // (2)
156 if (lua_pcall(L
, 1, 0, 0)) { // (-2;+1/+0)
163 static int lqtL_newindexfunc (lua_State
*L
) {
164 lua_settop(L
, 3); // (=3)
165 if (!lua_isuserdata(L
, 1) && lua_islightuserdata(L
, 1)) return 0;
166 lua_getfenv(L
, 1); // (+1)
167 if (!lua_istable(L
, -1)) {
168 lua_pop(L
, 1); // (+0)
171 lua_remove(L
, 1); // (+0)
172 lua_insert(L
, 1); // (+0)
173 lua_rawset(L
, 1); // (-2)
176 static int lqtL_indexfunc (lua_State
*L
) {
178 if (lua_isuserdata(L
, 1) && !lua_islightuserdata(L
, 1)) {
179 lua_getfenv(L
, 1); // (1)
180 lua_pushvalue(L
, 2); // (2)
181 lua_gettable(L
, -2); // (2)
182 if (!lua_isnil(L
, -1)) {
186 lua_pop(L
, 2); // (0)
188 lua_pushnil(L
); // (+1)
189 while (!lua_isnone(L
, lua_upvalueindex(i
))) { // (+1)
190 lua_pop(L
, 1); // (+0)
191 lua_pushvalue(L
, 2); // (+1)
193 lua_rawget(L
, lua_upvalueindex(i
)); // (+1)
195 lua_gettable(L
, lua_upvalueindex(i
)); // (+1)
197 if (!lua_isnil(L
, -1)) break;
203 static int lqtL_pushindexfunc (lua_State
*L
, const char *name
, lqt_Base
*bases
) {
205 luaL_newmetatable(L
, name
); // (1)
206 while (bases
->basename
!=NULL
) {
207 luaL_newmetatable(L
, bases
->basename
); // (upnum)
211 lua_pushcclosure(L
, lqtL_indexfunc
, upnum
); // (1)
215 static int lqtL_ctor_helper(lua_State
*L
) {
216 lua_getfield(L
, 1, "new");
218 lua_call(L
, lua_gettop(L
)-1, LUA_MULTRET
);
219 return lua_gettop(L
);
222 int lqtL_createclass (lua_State
*L
, const char *name
, luaL_Reg
*mt
, lqt_Base
*bases
) {
223 lqt_Base
*bi
= bases
;
224 luaL_newmetatable(L
, name
); // (1)
225 luaL_register(L
, NULL
, mt
); // (1)
227 lua_pushstring(L
, name
); // (2) FIXME: remove
228 lua_pushinteger(L
, 0); // (3) FIXME: remove
229 lua_settable(L
, -3); // (1) FIXME: remove
230 while (bi
->basename
!=NULL
) {
231 lua_pushstring(L
, bi
->basename
); // (2) FIXME: remove
232 lua_pushinteger(L
, bi
->offset
); // (3) FIXME: remove
233 lua_settable(L
, -3); // (1) FIXME: remove
237 lqtL_pushindexfunc(L
, name
, bases
); // (2)
238 lua_setfield(L
, -2, "__index"); // (1)
239 lua_pushcfunction(L
, lqtL_newindexfunc
); // (2)
240 lua_setfield(L
, -2, "__newindex"); // (1)
241 lua_pushcfunction(L
, lqtL_gcfunc
); // (2)
242 lua_setfield(L
, -2, "__gc"); // (1)
243 lua_pushcfunction(L
, lqtL_ctor_helper
); // (2)
244 lua_setfield(L
, -2, "__call"); // (1)
246 // set it as its own metatable
247 lua_pushvalue(L
, -1); // (2)
248 lua_setmetatable(L
, -2); // (1)
249 lua_pop(L
, 1); // (0)
250 lua_pushlstring(L
, name
, strlen(name
)-1); // (1)
251 lua_newtable(L
); // (2)
252 luaL_newmetatable(L
, name
); // (3)
253 lua_setmetatable(L
, -2); // (2)
254 // don't register again but use metatable
255 //luaL_register(L, NULL, mt); // (2)
256 lua_settable(L
, LUA_GLOBALSINDEX
); // (0)
260 int lqtL_createclasses (lua_State
*L
, lqt_Class
*list
) {
261 while (list
->name
!=0) { // (0)
262 luaL_newmetatable(L
, list
->name
); // (1)
263 luaL_register(L
, NULL
, list
->mt
); // (1)
264 lua_pushstring(L
, list
->name
); // (2)
265 lua_pushboolean(L
, 1); // (3)
266 lua_settable(L
, -3); // (1)
267 lqtL_pushindexfunc(L
, list
->name
, list
->bases
); // (2)
268 lua_setfield(L
, -2, "__index"); // (1)
269 lua_pushcfunction(L
, lqtL_newindexfunc
); // (2)
270 lua_setfield(L
, -2, "__newindex"); // (1)
271 lua_pushcfunction(L
, lqtL_gcfunc
); // (2)
272 lua_setfield(L
, -2, "__gc"); // (1)
273 lua_pushvalue(L
, -1); // (2)
274 lua_setmetatable(L
, -2); // (1)
275 lua_pop(L
, 1); // (0)
276 lua_pushlstring(L
, list
->name
, strlen(list
->name
)-1); // (1)
277 lua_newtable(L
); // (2)
278 luaL_register(L
, NULL
, list
->mt
); // (2)
279 lua_settable(L
, LUA_GLOBALSINDEX
); // (0)
285 bool lqtL_isinteger (lua_State
*L
, int i
) {
286 if (lua_type(L
, i
)==LUA_TNUMBER
)
287 return lua_tointeger(L
, i
)==lua_tonumber(L
, i
);
291 bool lqtL_isnumber (lua_State
*L
, int i
) {
292 return lua_type(L
, i
)==LUA_TNUMBER
;
294 bool lqtL_isstring (lua_State
*L
, int i
) {
295 return lua_type(L
, i
)==LUA_TSTRING
;
297 bool lqtL_isboolean (lua_State
*L
, int i
) {
298 return lua_type(L
, i
)==LUA_TBOOLEAN
;
300 bool lqtL_missarg (lua_State
*L
, int index
, int n
) {
303 for (i
=index
;i
<index
+n
;i
++) {
304 if (!lua_isnoneornil(L
, i
)) {
312 static void CS(lua_State
*L
) {
313 qDebug() << "++++++++++";
314 for (int i
=1;i
<=lua_gettop(L
);i
++) {
315 qDebug() << luaL_typename(L
, i
) << lua_touserdata(L
, i
);
317 qDebug() << "----------";
320 static void lqtL_ensurepointer (lua_State
*L
, const void *p
) { // (+1)
321 lqtL_getpointertable(L
); // (1)
322 lua_pushlightuserdata(L
, const_cast<void*>(p
)); // (2)
323 lua_gettable(L
, -2); // (2)
324 if (lua_isnil(L
, -1)) { // (2)
325 lua_pop(L
, 1); // (1)
326 const void **pp
= static_cast<const void**>(lua_newuserdata(L
, sizeof(void*))); // (2)
328 lua_newtable(L
); // (3)
329 lua_setfenv(L
, -2); // (2)
330 lua_pushlightuserdata(L
, const_cast<void*>(p
)); // (3)
331 lua_pushvalue(L
, -2); // (4)
332 lua_settable(L
, -4); // (2)
335 lua_remove(L
, -2); // (1)
338 void lqtL_register (lua_State
*L
, const void *p
) { // (+0)
339 lqtL_ensurepointer(L
, p
);
343 void lqtL_unregister (lua_State
*L
, const void *p
) {
344 lqtL_getpointertable(L
); // (1)
345 lua_pushlightuserdata(L
, const_cast<void*>(p
)); // (2)
346 lua_gettable(L
, -2); // (2)
347 if (lua_isuserdata(L
, -1)) {
348 const void **pp
= static_cast<const void**>(lua_touserdata(L
, -1)); // (2)
351 lua_pop(L
, 1); // (1)
352 lua_pushlightuserdata(L
, const_cast<void*>(p
)); // (2)
353 lua_pushnil(L
); // (3)
354 lua_settable(L
, -3); // (1)
355 lua_pop(L
, 1); // (0)
358 void lqtL_pushudata (lua_State
*L
, const void *p
, const char *name
) {
359 bool already
= false;
360 lqtL_ensurepointer(L
, p
); // (1)
361 if (lua_getmetatable(L
, -1)) {
363 lua_pop(L
, 1); // (1)
364 lua_getfield(L
, -1, name
); // (2)
365 already
= lua_toboolean(L
, -1); // (2)
366 lua_pop(L
, 1); // (1)
371 luaL_newmetatable(L
, name
); // (2)
372 lua_setmetatable(L
, -2); // (1)
377 void lqtL_passudata (lua_State
*L
, const void *p
, const char *name
) {
378 lqtL_pushudata(L
, p
, name
);
379 // FIXME: these should be added, but it is not safe for now
380 //lua_getfield(L, -1, "delete");
381 //lua_setfield(L, -2, "__gc");
385 void lqtL_copyudata (lua_State
*L
, const void *p
, const char *name
) {
386 luaL_newmetatable(L
, name
);
387 lua_pushstring(L
, "__copy");
389 if (lua_isnil(L
, -1)) {
390 qDebug() << "cannot copy" << name
;
395 lqtL_pushudata(L
, p
, name
);
396 if (lua_pcall(L
, 1, 1, 0)) {
397 qDebug() << "error copying" << name
;
405 void *lqtL_toudata (lua_State
*L
, int index
, const char *name
) {
407 if (!lqtL_testudata(L
, index
, name
)) return 0;
408 void **pp
= static_cast<void**>(lua_touserdata(L
, index
));
410 lua_getfield(L
, index
, name
);
411 ret
= (void*)(lua_tointeger(L
, -1) + (char*)ret
);
416 bool lqtL_testudata (lua_State
*L
, int index
, const char *name
) {
417 if (!lua_isuserdata(L
, index
) || lua_islightuserdata(L
, index
)) return false;
418 lua_getfield(L
, index
, name
);
419 if (lua_isnil(L
, -1)) {
427 void lqtL_pushenum (lua_State
*L
, int value
, const char *name
) {
428 lqtL_getenumtable(L
);
429 lua_getfield(L
, -1, name
);
431 if (!lua_istable(L
, -1)) {
436 lua_pushnumber(L
, value
);
441 bool lqtL_isenum (lua_State
*L
, int index
, const char *name
) {
443 if (!lua_isstring(L
, index
)) return false;
444 lqtL_getenumtable(L
);
445 lua_getfield(L
, -1, name
);
446 if (!lua_istable(L
, -1)) {
451 lua_pushvalue(L
, index
);
453 ret
= lua_isnumber(L
, -1);
458 int lqtL_toenum (lua_State
*L
, int index
, const char *name
) {
460 lqtL_getenumtable(L
); // (1)
461 lua_getfield(L
, -1, name
); // (2)
462 if (lua_isnil(L
, -1)) {
466 lua_pushvalue(L
, index
); // (3)
467 lua_gettable(L
, -2); // (3)
468 ret
= lua_tointeger(L
, -1); // (3)
469 lua_pop(L
, 3); // (0)
473 int lqtL_getflags (lua_State
*L
, int index
, const char *name
) {
477 if (!lua_istable(L
, index
)) return 0;
478 lqtL_getenumtable(L
); // (1)
479 lua_getfield(L
, -1, name
); // (2)
480 if (!lua_istable(L
, -1)) {
486 lua_remove(L
, -2); // (1)
487 eindex
= lua_gettop(L
);
488 for (i
=1;;i
++) { // (1)
489 lua_rawgeti(L
, index
, i
); // (2)
490 if (lua_type(L
, -1)!=LUA_TSTRING
) {
491 lua_pop(L
, 1); // (1)
494 lua_gettable(L
, eindex
); // (2)
495 ret
= ret
| (int)lua_tointeger(L
, -1);
496 lua_pop(L
, 1); // (1)
500 lua_pop(L
, 1); // (0)
504 void lqtL_pushflags (lua_State
*L
, int index
, const char *name
) {
510 int lqtL_touintarray (lua_State
*L
) {
514 n
= lua_objlen(L
, -1);
515 nb
= (n
+ 1) * sizeof(uint
);
516 p
= (uint
*)lua_newuserdata(L
, nb
);
518 lua_rawgeti(L
, -2, i
);
519 p
[i
-1] = lua_tointeger(L
, -1);