renamed common_* files to better names
[lqt.git] / lqt_common.cpp
blob2eaa27b3df2ce77652b58ca405e67a7fe97d3bd1
1 #include "common_bind.hpp"
3 #define LQT_FIXEDINDEX(i) (i<0?(1+lua_gettop(L)+i):i)
5 //#include <QDebug>
6 #ifndef SEE_STACK
7 # define SEE_STACK(L, j) for (int j=1;j<=lua_gettop(L);j++) { qDebug() << j << '=' << luaL_typename(L, j) << '@' << lua_topointer (L, j); }
8 #endif
10 // TODO
12 // - add a metatable for void* which always results in nil and possibly
13 // accounts for reassigning the userdata to the right metatable.
14 // - proper descending down the dependency tree for indexing, and proper
15 // handling of missing binds
19 //#include <iostream>
20 //using namespace std;
22 void lqtL_setvoidmetatable (lua_State *L, int i, const char *t = 0) {
23 i = LQT_FIXEDINDEX(i);
24 if (luaL_newmetatable(L, "void*")) {
25 lua_newtable(L);
26 lua_setfield(L, -2, "__index");
27 lua_pushstring(L, "void*");
28 lua_setfield(L, -2, "__qtype");
30 lua_setmetatable(L, i);
31 if (t!=0) {
32 lua_newtable(L);
33 lua_pushstring(L, t);
34 lua_setfield(L, -2, "__unknown_type");
35 lua_setfenv(L, i);
39 void lqtL_getpointers (lua_State *L) {
40 lua_getfield(L, LUA_REGISTRYINDEX, LQT_POINTERS);
41 if (!lua_istable(L, -1)) {
42 //cout << "Registry Pointers created" << endl;
43 lua_pop(L, 1);
44 lua_newtable(L);
45 lua_newtable(L);
46 lua_pushstring(L, "kv");
47 lua_setfield(L, -2, "__mode");
48 lua_setmetatable(L, -2);
49 lua_pushvalue(L, -1);
50 lua_setfield(L, LUA_REGISTRYINDEX, LQT_POINTERS);
52 return;
55 int lqtL_reusepointer (lua_State *L, const void *p, const char *t) {
56 lqtL_getpointers(L);
57 lua_pushlightuserdata(L, const_cast<void*>(p));
58 lua_gettable(L, -2);
59 if (lqtL_testudata(L, -1, t) && (p==*(const void**)lua_touserdata(L, -1))) {
60 lua_remove(L, -2);
61 //qDebug();
62 //qDebug() << "reused" << p << "in" << lua_touserdata(L, -1) << t;
63 //lua_getmetatable(L, -1);luaL_getmetatable(L, t);
64 //qDebug() << "meta" << lua_topointer(L, -2) << "type" << lua_topointer(L, -1);
65 //lua_pop(L, 2);
66 return 1;
68 lua_pop(L, 2);
69 return 0;
72 void lqtL_pushpointer (lua_State *L, const void *obj, const char *t) {
73 const void **objp = (const void**)lua_newuserdata(L, sizeof(const void *));
74 *objp = 0;
75 lua_getfield(L, LUA_REGISTRYINDEX, t);
76 if (lua_istable(L, -1)) {
77 lua_setmetatable(L, -2);
78 lua_newtable(L);
79 lua_setfenv(L, -2);
80 } else {
81 //cout << "NO metatable given" << endl;
82 // TODO: add a fallback?
83 lua_pop(L, 1);
84 lqtL_setvoidmetatable(L, -1, t);
86 *objp = obj;
87 //cout << "pushed " << objp << ' ' << obj << endl;
90 void lqtL_setpointer (lua_State *L, int i, const void *obj) {
91 int index = i<0?(1+lua_gettop(L)+i):i;
92 //cout << lua_gettop(L) << ' ' << i << ' ' << index << endl;
93 lqtL_getpointers(L);
94 lua_pushlightuserdata(L, const_cast<void *>(obj));
95 lua_pushvalue(L, index);
96 lua_settable(L, -3);
97 lua_pop(L, 1);
98 return;
100 void lqtL_unsetpointer (lua_State *L, const void *obj) {
101 lqtL_getpointers(L);
102 lua_pushlightuserdata(L, const_cast<void *>(obj));
103 lua_pushnil(L);
104 lua_settable(L, -3);
105 lua_pop(L, 1);
106 return;
109 // TODO: don't need, it's debug
110 void * lqtL_topointer (lua_State *L, int i) {
111 void * ret = NULL;
112 ret = lua_touserdata(L, i);
113 ret = (ret==0)?0:*static_cast<void**>(ret);
114 return ret;
117 void lqtL_unmanageudata (lua_State *L, int i) {
118 if (!lua_isuserdata(L, i)) return;
119 lua_getfenv(L, i);
120 if (lua_istable(L, -1)) {
121 lua_pushnil(L);
122 lua_setfield(L, -2, "__gc");
124 lua_pop(L, 1);
125 return;
127 void lqtL_manageudata (lua_State *L, int index) {
128 //luaL_checkstack(L, 20, "");
129 index = LQT_FIXEDINDEX(index);
130 if (!lua_isuserdata(L, index)) return;
131 lua_getfield(L, index, "delete");
132 if (!lua_isfunction(L, -1)) {
133 lua_pop(L, 1);
134 return;
136 lua_getfenv(L, index);
137 if (!lua_istable(L, -1)) {
138 lua_pop(L, 1);
139 //if (1) {
140 lua_newtable(L);
141 lua_pushvalue(L, -1);
142 lua_setfenv(L, index);
144 lua_insert(L, -2);
145 lua_setfield(L, -2, "__gc");
146 lua_pop(L, 1);
147 return;
149 void lqtL_pushudata (lua_State *L, const void *obj, const char *t) {
150 // TODO: make the udata unique
151 //cout << endl << "pushing udata " << obj << " with type " << t << endl;
153 lua_checkstack(L, 5);
154 if (1) {
155 if (lqtL_reusepointer(L, obj, t)) {
156 //cout << obj << " reused " << t << endl;
157 return;
159 //cout << obj << " NOT reused " << t << endl;
160 } else {
161 lqtL_getpointers(L);
162 lua_pushlightuserdata(L, const_cast<void *>(obj));
163 lua_gettable(L, -2);
164 if (0) {
165 //cout << lua_gettop(L) << endl;
166 lua_getglobal(L, "print");
167 if (lua_getmetatable(L, -2)) {
168 //cout << "metatable "; // << lua_tostring(L, -1) << endl << endl;
169 lua_call(L, 1, 0);
170 } else {
171 //cout << "no metatable" << endl;
172 lua_pop(L, 1);
174 //cout << lua_gettop(L) << endl;
175 lua_getglobal(L, "print");
176 lua_getfield(L, LUA_REGISTRYINDEX, t);
177 //cout << "registry "; // << lua_tostring(L, -1) << endl; lua_pop(L, 1);
178 lua_call(L, 1, 0);
179 } else if (0) {
180 //cout << lua_gettop(L) << endl;
181 if (lua_getmetatable(L, -1)) {
182 lua_getfield(L, LUA_REGISTRYINDEX, t);
183 //cout << (bool)lua_equal(L, -1, -2) << endl;
184 lua_getglobal(L, "print");
185 lua_insert(L, -3);
186 lua_call(L, 2, 0);
187 } else {
188 //cout << "no metatable" << endl;
189 lua_pop(L, 1);
192 if (lqtL_testudata(L, -1, t)) {
193 //cout << obj << " reused" << endl;
194 lua_remove(L, -2);
195 return;
196 } else {
197 //cout << luaL_typename(L, -1) << " testudata failed " << lua_touserdata(L, -1) << ' ' << lqtL_topointer(L, -1) << endl;
199 lua_pop(L, 2);
202 if (1) {
203 lqtL_pushpointer(L, obj, t);
204 } else {
205 const void **objp = (const void**)lua_newuserdata(L, sizeof(const void *));
206 *objp = 0;
207 lua_getfield(L, LUA_REGISTRYINDEX, t);
208 if (lua_istable(L, -1)) {
209 lua_setmetatable(L, -2);
210 } else {
211 //cout << "NO metatable given" << endl;
212 lua_pop(L, 1);
213 // TODO: add a fallback?
215 *objp = obj;
216 //cout << "pushed " << objp << ' ' << obj << endl;
218 //cout << lua_gettop(L) << endl;
219 if (1) {
220 lqtL_setpointer(L, -1, obj);
221 } else {
222 lua_getfield(L, LUA_REGISTRYINDEX, LQT_POINTERS);
223 lua_pushlightuserdata(L, const_cast<void *>(obj));
224 lua_pushvalue(L, -3);
225 lua_settable(L, -3);
226 lua_pop(L, 1);
228 //cout << lua_gettop(L) << endl;
230 void lqtL_passudata (lua_State *L, const void *obj, const char *t) {
231 //cout << "passing: " << obj << " " << t << endl;
232 lqtL_pushudata(L, obj, t);
233 lqtL_manageudata(L, -1);
237 int lqtL_derivesfrom (lua_State *L, int i, const char *t) {
238 int ret = 0;
239 int oldtop = lua_gettop(L);
240 i = LQT_FIXEDINDEX(i);
242 if (!lua_istable(L, i)) {
243 return 0;
246 luaL_checkstack(L, 1, "cannot grow stack for checking object type");
248 //qDebug() << "given a table" << lua_topointer(L, i);
249 lua_getfield(L, LUA_REGISTRYINDEX, t);
250 //qDebug() << "table" << t << lua_topointer(L, -1);
251 ret = lua_equal(L, i, -1);
252 //qDebug() << "same as" << t << "?" << (bool)ret;
255 lua_settop(L, oldtop);
257 return ret;
258 // */
260 if (!ret) {
261 lua_pop(L, 1);
262 luaL_checkstack(L, 13, "cannot grow stack for checking object type"); // FIXME: is it enough?
263 lua_getfield(L, i, "__base");
265 if (!lua_istable(L, -1)) {
266 lua_pop(L, 1);
267 ret = 0;
268 } else {
269 lua_getfield(L, -1, t);
270 ret = lua_isnil(L, -1)?0:1;
271 // DANGER: order of control expression is important
272 while ((ret == 0) && (lua_next(L, -2) != 0)) {
273 if (!lua_istable(L, -1)) {
274 lua_pop(L, 1);
275 lua_pushvalue(L, -1);
276 lua_gettable(L, LUA_REGISTRYINDEX);
277 if (lua_istable(L, -1)) {
278 lua_pushvalue(L, -2);
279 lua_pushvalue(L, -2);
280 lua_settable(L, -5);
283 if (lua_istable(L, -1)) {
284 ret = lqtL_derivesfrom(L, -1, t);
286 lua_pop(L, 1);
291 lua_settop(L, oldtop);
293 return ret;
296 bool lqtL_testudata (lua_State *L, int i, const char *t) {
297 //qDebug() << "================ testudata" << t;
298 //luaL_checkstack(L, 99, "");
299 i = LQT_FIXEDINDEX(i);
300 bool ret = false;
301 if (lua_getmetatable(L, i)) {
302 //SEE_STACK(L, pippo);
303 //SEE_STACK(L, pippo);
304 if (0) {
305 lua_getfield(L, LUA_REGISTRYINDEX, "QEvent*");
306 if (0 && lua_istable(L, -1)) {
307 lua_getfield(L, -1, "__qtype");
308 //SEE_STACK(L, pippo);
309 lua_pop(L, 1);
311 lua_pop(L, 2);
313 ret = (bool) lqtL_derivesfrom(L, -1, t);
315 //qDebug() << "derives?" << ret;
316 if (!ret && lqtL_derivesfrom(L, -1, "void*")) {
317 //cout << "checking for a generic void* pointer" << endl;
319 luaL_checkstack(L, 3, "cannot check void* real type");
320 lua_getfenv(L, i);
321 lua_getfield(L, -1, "__unknown_type");
322 //TODO: assign dynamically?
323 lua_pushstring(L, t);
324 ret = (bool)lua_equal(L, -1, -2);
325 lua_pop(L, 3);
326 // */
327 // FIXME: deleting makes QMetaObjects not work
328 //ret = true;
330 lua_pop(L, 1);
331 } else {
333 //cout << t << (ret?"":" NOT") <<" found" << endl;
334 return ret;
336 bool ret = false;
337 int oldtop = lua_gettop(L);
338 lua_getfield(L, LUA_REGISTRYINDEX, t);
339 if (lua_getmetatable(L, i)) {
340 ret = (bool)lua_equal(L, -1, -2);
341 if (!ret) {
342 lua_remove(L, -2);
343 ret = lqTL_derivesfrom(L, -1, t);
346 lua_settop(L, oldtop);
347 return ret;
352 int lqtL_pushtype (lua_State *L, int i) {
353 int type = lua_type(L, i);
354 if (type != LUA_TUSERDATA) {
355 lua_pushstring(L, lua_typename(L, type));
356 } else {
357 lua_getfield(L, i, "__qtype");
358 if (!lua_isstring(L, -1)) {
359 lua_pop(L, 1);
360 lua_pushstring(L, "<unknown>");
363 return 1;
366 void * lqtL_checkudata (lua_State *L, int i, const char *t) {
367 if (lqtL_testudata(L, i, t)) {
368 return lua_touserdata(L, i);
369 } else {
370 lua_pushstring(L, "Fatal error: userdata type mismatch. requested ");
371 lua_pushstring(L, t);
372 lua_pushstring(L, " found ");
373 lqtL_pushtype(L, i);
374 lua_concat(L, 4);
375 lua_error(L);
377 return 0;
379 //*/
380 void * lqtL_toudata (lua_State *L, int i, const char *t) {
381 void * ret = NULL;
382 if (lqtL_testudata(L, i, t)) {
383 ret = *static_cast<void**>(lua_touserdata(L, i));
385 return ret;
388 void lqtL_pushenum (lua_State *L, int v, const char *e) {
389 lua_getfield(L, LUA_REGISTRYINDEX, LQT_ENUMS);
390 if (lua_istable(L, -1)) {
391 //qDebug() << "LQT_ENUMS is a table";
392 lua_getfield(L, -1, e);
393 lua_remove(L, -2);
394 } else {
395 //qDebug() << "LQT_ENUMS is NOT a table";
397 lua_pushinteger(L, v);
398 if (lua_istable(L, -2)) {
399 //qDebug() << "getting translation";
400 lua_gettable(L, -2);
401 } else {
402 //qDebug() << "no translation table for" << e;
404 lua_remove(L, -2);
406 bool lqtL_isenum (lua_State *L, int i, const char *e) {
407 bool ret = false;
408 if (lua_type(L, i)==LUA_TNUMBER) {
409 ret = (lua_tointeger(L, i)==lua_tonumber(L, i));
410 } else if (lua_type(L, i)==LUA_TSTRING) {
411 lua_getfield(L, LUA_REGISTRYINDEX, LQT_ENUMS);
412 if (lua_istable(L, -1)) {
413 lua_getfield(L, -1, e);
414 lua_remove(L, -2);
416 lua_pushvalue(L, i);
417 if (lua_istable(L, -2)) {
418 lua_gettable(L, -2);
420 if (lua_type(L, -1)==LUA_TNUMBER) {
421 ret = (lua_tointeger(L, -1)==lua_tonumber(L, -1));
423 lua_pop(L, 2);
425 return ret;
427 int lqtL_toenum (lua_State *L, int i, const char *e) {
428 int ret = -1;
429 if (lua_type(L, i)==LUA_TNUMBER) {
430 ret = lua_tointeger(L, i);
431 } else if (lua_type(L, i)==LUA_TSTRING) {
432 lua_getfield(L, LUA_REGISTRYINDEX, LQT_ENUMS);
433 if (lua_istable(L, -1)) {
434 lua_getfield(L, -1, e);
435 lua_remove(L, -2);
437 lua_pushvalue(L, i);
438 if (lua_istable(L, -2)) {
439 lua_gettable(L, -2);
441 if (lua_type(L, -1)==LUA_TNUMBER) {
442 ret = lua_tointeger(L, -1);
444 lua_pop(L, 2);
446 return ret;
449 int lqtL_baseindex (lua_State *L, int index, int key) {
450 int ret = 0;
451 int oldtop = lua_gettop(L);
452 index = LQT_FIXEDINDEX(index);
453 key = LQT_FIXEDINDEX(key);
455 if (!lua_istable(L, index)) {
456 return 0;
459 luaL_checkstack(L, 1, "cannot grow stack for retrieving member");
461 lua_pushvalue(L, key);
462 lua_gettable(L, index);
464 if (!lua_isnil(L, -1)) {
465 ret = 1;
466 } else {
467 luaL_checkstack(L, 7, "cannot grow stack for retrieving member"); // FIXME: is it enough?
468 lua_getfield(L, index, "__base");
470 if (!lua_istable(L, -1)) {
471 lua_pop(L, 2);
472 ret = 0;
473 } else {
474 lua_insert(L, -2);
475 // DANGER: order of control expression is important
476 while ((ret == 0) && (lua_next(L, -2) != 0)) {
477 if (!lua_istable(L, -1)) {
478 lua_pop(L, 1);
479 lua_pushvalue(L, -1);
480 lua_gettable(L, LUA_REGISTRYINDEX);
481 if (lua_istable(L, -1)) {
482 lua_pushvalue(L, -2);
483 lua_pushvalue(L, -2);
484 lua_settable(L, -5);
487 if (lua_istable(L, -1)) {
488 ret = lqtL_baseindex(L, -1, key);
490 if (ret == 0) {
491 lua_pop(L, 1);
497 if (ret != 0) {
498 lua_insert(L, oldtop+1);
500 lua_settop(L, oldtop+ret);
502 return ret;
505 int lqtL_index (lua_State *L) {
506 lua_settop(L, 2);
507 luaL_checkstack(L, 1, "cannot grow stack for retrieving member");
508 if (!lua_getmetatable(L, 1)) {
509 return 0;
512 return lqtL_baseindex(L, 3, 2);
514 luaL_checkstack(L, 2, "cannot grow stack for retrieving member");
515 lua_getmetatable(L, 1);
516 if (lua_istable(L, -1)) {
517 lua_pushvalue(L, 2);
518 lua_gettable(L, -2);
519 } else {
520 lua_pushnil(L);
522 lua_remove(L, -2);
523 return 1;
527 int lqtL_newindex (lua_State *L) {
528 if (!lua_getmetatable(L, 1)) {
529 return 0;
531 if (lua_istable(L, -1)) {
532 lua_pushvalue(L, 2);
533 lua_pushvalue(L, 3);
534 lua_settable(L, -3);
535 lua_pop(L, 1);
537 return 0;
540 int lqtL_gc (lua_State *L) {
541 if (!lua_isuserdata(L, 1)) return 0;
543 lua_getfenv(L, 1);
544 if (lua_istable(L, -1)) {
545 lua_getfield(L, -1, "__gc");
546 if (lua_isfunction(L, -1)) {
547 //cout << "found fenv gc " << lua_topointer(L, -1) << endl;
548 lua_pushvalue(L, 1);
549 lua_call(L, 1, 0);
550 } else {
551 //cout << "NOT found fenv gc" << endl;
552 lua_pop(L, 1);
555 lua_pop(L, 1);
557 // FIXME: this is useless, isn't it?
558 void **p = static_cast<void**>(lua_touserdata(L, 1));
559 *p = 0;
561 return 0;