fixed number of returned arguments in constructor helper
[lqt.git] / common / lqt_common.cpp
blobf80e90f5a38fd720fa033453e194b61022b3dd85
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, "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) {
71 void *ret = NULL;
72 lqtL_getreftable(L); // (1)
73 ret = lua_newuserdata(L, sz); // (2)
74 lua_rawseti(L, -2, 1+lua_objlen(L, -2));
75 return ret;
78 int * lqtL_tointref (lua_State *L, int index) {
79 int *ret = (int*)lqtL_getref(L, sizeof(int));
80 *ret = lua_tointeger(L, index);
81 return ret;
84 void lqtL_pusharguments (lua_State *L, char **argv) {
85 int i = 0;
86 lua_newtable(L);
87 for (i=0;*argv /* fix the maximum number? */;argv++,i++) {
88 lua_pushstring(L, *argv);
89 lua_rawseti(L, -2, i+1);
91 return;
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;
97 size_t strlen = 0;
98 int i = 0;
99 for (i=0;;i++) {
100 lua_rawgeti(L, index, i+1);
101 if (!lua_isstring(L, -1)) {
102 str = NULL; strlen = 0;
103 ret[i] = NULL;
104 lua_pop(L, 1);
105 break;
106 } else {
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);
110 lua_pop(L, 1);
113 return ret;
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)
128 e++; // (2)
130 lua_pop(L, 2); // (0)
131 return 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)
137 list++;
139 return 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)
147 return 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)
153 return 0;
155 lua_pushvalue(L, 1); // (2)
156 if (lua_pcall(L, 1, 0, 0)) { // (-2;+1/+0)
157 // (1)
158 return lua_error(L);
160 return 0; // (+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)
169 return 0;
171 lua_remove(L, 1); // (+0)
172 lua_insert(L, 1); // (+0)
173 lua_rawset(L, 1); // (-2)
174 return 0;
176 static int lqtL_indexfunc (lua_State *L) {
177 int i = 1;
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)) {
183 lua_remove(L, -2);
184 return 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)
192 if (i==1) {
193 lua_rawget(L, lua_upvalueindex(i)); // (+1)
194 } else {
195 lua_gettable(L, lua_upvalueindex(i)); // (+1)
197 if (!lua_isnil(L, -1)) break;
198 i++;
200 return 1; // (+1)
203 static int lqtL_pushindexfunc (lua_State *L, const char *name, lqt_Base *bases) {
204 int upnum = 1;
205 luaL_newmetatable(L, name); // (1)
206 while (bases->basename!=NULL) {
207 luaL_newmetatable(L, bases->basename); // (upnum)
208 upnum++;
209 bases++;
211 lua_pushcclosure(L, lqtL_indexfunc, upnum); // (1)
212 return 1;
215 static int lqtL_ctor_helper(lua_State*L) {
216 lua_getfield(L, 1, "new");
217 lua_replace(L, 1);
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)
226 // setup offsets
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
234 bi++;
236 // set metafunctions
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)
257 return 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)
280 list++;
282 return 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);
288 else
289 return false;
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) {
301 bool ret = true;
302 int i = 0;
303 for (i=index;i<index+n;i++) {
304 if (!lua_isnoneornil(L, i)) {
305 ret = false;
306 break;
309 return ret;
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)
327 *pp = p; // (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)
334 // (2)
335 lua_remove(L, -2); // (1)
338 void lqtL_register (lua_State *L, const void *p) { // (+0)
339 lqtL_ensurepointer(L, p);
340 lua_pop(L, 1);
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)
349 *pp = 0;
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)) {
362 // (2)
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)
367 } else {
368 // (1)
370 if (!already) {
371 luaL_newmetatable(L, name); // (2)
372 lua_setmetatable(L, -2); // (1)
374 return;
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");
382 return;
385 void lqtL_copyudata (lua_State *L, const void *p, const char *name) {
386 luaL_newmetatable(L, name);
387 lua_pushstring(L, "__copy");
388 lua_rawget(L, -2);
389 if (lua_isnil(L, -1)) {
390 qDebug() << "cannot copy" << name;
391 lua_pop(L, 2);
392 lua_pushnil(L);
393 } else {
394 lua_remove(L, -2);
395 lqtL_pushudata(L, p, name);
396 if (lua_pcall(L, 1, 1, 0)) {
397 qDebug() << "error copying" << name;
398 lua_pop(L, 1);
399 lua_pushnil(L);
402 return;
405 void *lqtL_toudata (lua_State *L, int index, const char *name) {
406 void *ret = 0;
407 if (!lqtL_testudata(L, index, name)) return 0;
408 void **pp = static_cast<void**>(lua_touserdata(L, index));
409 ret = *pp;
410 lua_getfield(L, index, name);
411 ret = (void*)(lua_tointeger(L, -1) + (char*)ret);
412 lua_pop(L, 1);
413 return 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)) {
420 lua_pop(L, 1);
421 return false;
423 lua_pop(L, 1);
424 return true;
427 void lqtL_pushenum (lua_State *L, int value, const char *name) {
428 lqtL_getenumtable(L);
429 lua_getfield(L, -1, name);
430 lua_remove(L, -2);
431 if (!lua_istable(L, -1)) {
432 lua_pop(L, 1);
433 lua_pushnil(L);
434 return;
436 lua_pushnumber(L, value);
437 lua_gettable(L, -2);
438 lua_remove(L, -2);
441 bool lqtL_isenum (lua_State *L, int index, const char *name) {
442 bool ret = false;
443 if (!lua_isstring(L, index)) return false;
444 lqtL_getenumtable(L);
445 lua_getfield(L, -1, name);
446 if (!lua_istable(L, -1)) {
447 lua_pop(L, 2);
448 return false;
450 lua_remove(L, -2);
451 lua_pushvalue(L, index);
452 lua_gettable(L, -2);
453 ret = lua_isnumber(L, -1);
454 lua_pop(L, 2);
455 return ret;
458 int lqtL_toenum (lua_State *L, int index, const char *name) {
459 int ret = -1;
460 lqtL_getenumtable(L); // (1)
461 lua_getfield(L, -1, name); // (2)
462 if (lua_isnil(L, -1)) {
463 lua_pop(L, 2); //(0)
464 return 0;
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)
470 return ret;
473 int lqtL_getflags (lua_State *L, int index, const char *name) {
474 int ret = 0;
475 int eindex = 0;
476 int i = 1;
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)) {
481 // (2)
482 lua_pop(L, 2);
483 return 0;
485 // (2)
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)
492 break;
493 } else {
494 lua_gettable(L, eindex); // (2)
495 ret = ret | (int)lua_tointeger(L, -1);
496 lua_pop(L, 1); // (1)
499 // (1)
500 lua_pop(L, 1); // (0)
501 return ret;
504 void lqtL_pushflags (lua_State *L, int index, const char *name) {
505 // TODO
506 lua_pushnil(L);
507 return;
510 int lqtL_touintarray (lua_State *L) {
511 uint *p = NULL;
512 size_t i = 0;
513 size_t n, nb;
514 n = lua_objlen(L, -1);
515 nb = (n + 1) * sizeof(uint);
516 p = (uint*)lua_newuserdata(L, nb);
517 for (i=1;i<=n;i++) {
518 lua_rawgeti(L, -2, i);
519 p[i-1] = lua_tointeger(L, -1);
520 lua_pop(L, 1);
522 lua_remove(L, -2);
523 p[n] = 0;
524 return 1;