1 #include "lqt_common.hpp"
3 #define LQT_FIXEDINDEX(i) (i<0?(1+lua_gettop(L)+i):i)
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); }
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
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*")) {
26 lua_setfield(L
, -2, "__index");
27 lua_pushstring(L
, "void*");
28 lua_setfield(L
, -2, "__qtype");
30 lua_setmetatable(L
, i
);
34 lua_setfield(L
, -2, "__unknown_type");
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;
46 lua_pushstring(L
, "kv");
47 lua_setfield(L
, -2, "__mode");
48 lua_setmetatable(L
, -2);
50 lua_setfield(L
, LUA_REGISTRYINDEX
, LQT_POINTERS
);
55 int lqtL_reusepointer (lua_State
*L
, const void *p
, const char *t
) {
57 lua_pushlightuserdata(L
, const_cast<void*>(p
));
59 if (lqtL_testudata(L
, -1, t
) && (p
==*(const void**)lua_touserdata(L
, -1))) {
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);
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 *));
75 lua_getfield(L
, LUA_REGISTRYINDEX
, t
);
76 if (lua_istable(L
, -1)) {
77 lua_setmetatable(L
, -2);
81 //cout << "NO metatable given" << endl;
82 // TODO: add a fallback?
84 lqtL_setvoidmetatable(L
, -1, t
);
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;
94 lua_pushlightuserdata(L
, const_cast<void *>(obj
));
95 lua_pushvalue(L
, index
);
100 void lqtL_unsetpointer (lua_State
*L
, const void *obj
) {
102 lua_pushlightuserdata(L
, const_cast<void *>(obj
));
109 // TODO: don't need, it's debug
110 void * lqtL_topointer (lua_State
*L
, int i
) {
112 ret
= lua_touserdata(L
, i
);
113 ret
= (ret
==0)?0:*static_cast<void**>(ret
);
117 void lqtL_unmanageudata (lua_State
*L
, int i
) {
118 if (!lua_isuserdata(L
, i
)) return;
120 if (lua_istable(L
, -1)) {
122 lua_setfield(L
, -2, "__gc");
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)) {
136 lua_getfenv(L
, index
);
137 if (!lua_istable(L
, -1)) {
141 lua_pushvalue(L
, -1);
142 lua_setfenv(L
, index
);
145 lua_setfield(L
, -2, "__gc");
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);
155 if (lqtL_reusepointer(L
, obj
, t
)) {
156 //cout << obj << " reused " << t << endl;
159 //cout << obj << " NOT reused " << t << endl;
162 lua_pushlightuserdata(L
, const_cast<void *>(obj
));
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;
171 //cout << "no metatable" << endl;
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);
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");
188 //cout << "no metatable" << endl;
192 if (lqtL_testudata(L
, -1, t
)) {
193 //cout << obj << " reused" << endl;
197 //cout << luaL_typename(L, -1) << " testudata failed " << lua_touserdata(L, -1) << ' ' << lqtL_topointer(L, -1) << endl;
203 lqtL_pushpointer(L
, obj
, t
);
205 const void **objp
= (const void**)lua_newuserdata(L
, sizeof(const void *));
207 lua_getfield(L
, LUA_REGISTRYINDEX
, t
);
208 if (lua_istable(L
, -1)) {
209 lua_setmetatable(L
, -2);
211 //cout << "NO metatable given" << endl;
213 // TODO: add a fallback?
216 //cout << "pushed " << objp << ' ' << obj << endl;
218 //cout << lua_gettop(L) << endl;
220 lqtL_setpointer(L
, -1, obj
);
222 lua_getfield(L
, LUA_REGISTRYINDEX
, LQT_POINTERS
);
223 lua_pushlightuserdata(L
, const_cast<void *>(obj
));
224 lua_pushvalue(L
, -3);
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
) {
239 int oldtop
= lua_gettop(L
);
240 i
= LQT_FIXEDINDEX(i
);
242 if (!lua_istable(L
, i
)) {
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);
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)) {
269 lua_getfield(L
, -1, t
);
270 ret
= lua_isnil(L
, -1)?0:1;
271 //if (ret) qDebug() << (lua_toboolean(L, -1)?"directly":"INDIRECTLY") << "derives from" << t;
272 // DANGER: order of control expression is important
273 while ((ret
== 0) && (lua_next(L
, -2) != 0)) {
274 if (!lua_istable(L
, -1)) {
276 lua_pushvalue(L
, -1);
277 lua_gettable(L
, LUA_REGISTRYINDEX
);
278 if (lua_istable(L
, -1)) {
279 lua_pushvalue(L
, -2);
280 lua_pushvalue(L
, -2);
284 if (lua_istable(L
, -1)) {
285 ret
= lqtL_derivesfrom(L
, -1, t
);
292 lua_settop(L
, oldtop
);
297 bool lqtL_testudata (lua_State
*L
, int i
, const char *t
) {
298 //qDebug() << "================ testudata" << t;
299 //luaL_checkstack(L, 99, "");
300 i
= LQT_FIXEDINDEX(i
);
302 if (lua_getmetatable(L
, i
)) {
303 //SEE_STACK(L, pippo);
304 //SEE_STACK(L, pippo);
306 lua_getfield(L
, LUA_REGISTRYINDEX
, "QEvent*");
307 if (0 && lua_istable(L
, -1)) {
308 lua_getfield(L
, -1, "__qtype");
309 //SEE_STACK(L, pippo);
314 ret
= (bool) lqtL_derivesfrom(L
, -1, t
);
316 //qDebug() << "derives?" << ret;
317 if (!ret
&& lqtL_derivesfrom(L
, -1, "void*")) {
318 //cout << "checking for a generic void* pointer" << endl;
320 luaL_checkstack(L
, 3, "cannot check void* real type");
322 lua_getfield(L
, -1, "__unknown_type");
323 //TODO: assign dynamically?
324 lua_pushstring(L
, t
);
325 ret
= (bool)lua_equal(L
, -1, -2);
328 // FIXME: deleting makes QMetaObjects not work
334 //cout << t << (ret?"":" NOT") <<" found" << endl;
338 int oldtop = lua_gettop(L);
339 lua_getfield(L, LUA_REGISTRYINDEX, t);
340 if (lua_getmetatable(L, i)) {
341 ret = (bool)lua_equal(L, -1, -2);
344 ret = lqTL_derivesfrom(L, -1, t);
347 lua_settop(L, oldtop);
353 int lqtL_pushtype (lua_State
*L
, int i
) {
354 int type
= lua_type(L
, i
);
355 if (type
!= LUA_TUSERDATA
) {
356 lua_pushstring(L
, lua_typename(L
, type
));
358 lua_getfield(L
, i
, "__qtype");
359 if (!lua_isstring(L
, -1)) {
361 lua_pushstring(L
, "<unknown>");
367 void * lqtL_checkudata (lua_State
*L
, int i
, const char *t
) {
368 if (lqtL_testudata(L
, i
, t
)) {
369 return lua_touserdata(L
, i
);
371 lua_pushstring(L
, "Fatal error: userdata type mismatch. requested ");
372 lua_pushstring(L
, t
);
373 lua_pushstring(L
, " found ");
381 void * lqtL_toudata (lua_State
*L
, int i
, const char *t
) {
383 if (lqtL_testudata(L
, i
, t
)) {
384 ret
= *static_cast<void**>(lua_touserdata(L
, i
));
389 void lqtL_pushenum (lua_State
*L
, int v
, const char *e
) {
390 lua_getfield(L
, LUA_REGISTRYINDEX
, LQT_ENUMS
);
391 if (lua_istable(L
, -1)) {
392 //qDebug() << "LQT_ENUMS is a table";
393 lua_getfield(L
, -1, e
);
396 //qDebug() << "LQT_ENUMS is NOT a table";
398 lua_pushinteger(L
, v
);
399 if (lua_istable(L
, -2)) {
400 //qDebug() << "getting translation";
403 //qDebug() << "no translation table for" << e;
407 bool lqtL_isenum (lua_State
*L
, int i
, const char *e
) {
409 if (lua_type(L
, i
)==LUA_TNUMBER
) {
410 ret
= (lua_tointeger(L
, i
)==lua_tonumber(L
, i
));
411 } else if (lua_type(L
, i
)==LUA_TSTRING
) {
412 lua_getfield(L
, LUA_REGISTRYINDEX
, LQT_ENUMS
);
413 if (lua_istable(L
, -1)) {
414 lua_getfield(L
, -1, e
);
418 if (lua_istable(L
, -2)) {
421 if (lua_type(L
, -1)==LUA_TNUMBER
) {
422 ret
= (lua_tointeger(L
, -1)==lua_tonumber(L
, -1));
428 int lqtL_toenum (lua_State
*L
, int i
, const char *e
) {
430 if (lua_type(L
, i
)==LUA_TNUMBER
) {
431 ret
= lua_tointeger(L
, i
);
432 } else if (lua_type(L
, i
)==LUA_TSTRING
) {
433 lua_getfield(L
, LUA_REGISTRYINDEX
, LQT_ENUMS
);
434 if (lua_istable(L
, -1)) {
435 lua_getfield(L
, -1, e
);
439 if (lua_istable(L
, -2)) {
442 if (lua_type(L
, -1)==LUA_TNUMBER
) {
443 ret
= lua_tointeger(L
, -1);
450 int lqtL_baseindex (lua_State
*L
, int index
, int key
) {
452 int oldtop
= lua_gettop(L
);
453 index
= LQT_FIXEDINDEX(index
);
454 key
= LQT_FIXEDINDEX(key
);
456 if (!lua_istable(L
, index
)) {
460 luaL_checkstack(L
, 1, "cannot grow stack for retrieving member");
462 lua_pushvalue(L
, key
);
463 lua_gettable(L
, index
);
465 if (!lua_isnil(L
, -1)) {
468 luaL_checkstack(L
, 7, "cannot grow stack for retrieving member"); // FIXME: is it enough?
469 lua_getfield(L
, index
, "__base");
471 if (!lua_istable(L
, -1)) {
476 // DANGER: order of control expression is important
477 while ((ret
== 0) && (lua_next(L
, -2) != 0)) {
478 if (!lua_istable(L
, -1) && lua_isboolean(L
, -1) && lua_toboolean(L
, -1)) {
480 lua_pushvalue(L
, -1);
481 lua_gettable(L
, LUA_REGISTRYINDEX
);
482 if (lua_istable(L
, -1)) {
483 lua_pushvalue(L
, -2);
484 lua_pushvalue(L
, -2);
488 if (lua_istable(L
, -1)) {
489 ret
= lqtL_baseindex(L
, -1, key
);
499 lua_insert(L
, oldtop
+1);
501 lua_settop(L
, oldtop
+ret
);
506 int lqtL_index (lua_State
*L
) {
508 luaL_checkstack(L
, 1, "cannot grow stack for retrieving member");
509 if (!lua_getmetatable(L
, 1)) {
513 return lqtL_baseindex(L
, 3, 2);
515 luaL_checkstack(L, 2, "cannot grow stack for retrieving member");
516 lua_getmetatable(L, 1);
517 if (lua_istable(L, -1)) {
528 int lqtL_newindex (lua_State
*L
) {
529 if (!lua_getmetatable(L
, 1)) {
532 if (lua_istable(L
, -1)) {
541 int lqtL_gc (lua_State
*L
) {
542 if (!lua_isuserdata(L
, 1)) return 0;
545 if (lua_istable(L
, -1)) {
546 lua_getfield(L
, -1, "__gc");
547 if (lua_isfunction(L
, -1)) {
548 //cout << "found fenv gc " << lua_topointer(L, -1) << endl;
552 //cout << "NOT found fenv gc" << endl;
558 // FIXME: this is useless, isn't it?
559 void **p
= static_cast<void**>(lua_touserdata(L
, 1));