3 HEADER_DEFINE
= tostring(os
.getenv'HEADER_DEFINE' or '__LQT_FUNCTION')
4 ARG_MAX
= tonumber(os
.getenv'ARG_MAX' or 2)
5 TYPES
= { bool
='lua_pushboolean', int
='lua_pushinteger', double
='lua_pushnumber', ['const char *']='lua_pushstring' }
10 cpp
.string = cpp
.string .. tostring(s
)
17 hpp
.string = hpp
.string .. tostring(s
)
21 hpp
.write("#ifndef "..HEADER_DEFINE
.."\n")
22 hpp
.write("#define "..HEADER_DEFINE
.."\n")
23 cpp
.write('#include "lqt_function.hpp"\n ')
28 #include "lqt_common.hpp"
33 #define LUA_FUNCTION_REGISTRY "Registry Function"
35 //# define SEE_STACK(L, j) for (int j=1;j<=lua_gettop(L);j++) { qDebug() << j << '=' << luaL_typename(L, j) << '@' << lua_topointer (L, j); }
38 class LuaFunction: public QObject {
42 LuaFunction(lua_State *state);
43 virtual ~LuaFunction();
47 static int __gc (lua_State *L);
53 LuaFunction::LuaFunction(lua_State *state):L(state) {
54 int functionTable = lua_gettop(L); // not yet but soon
55 //qDebug() << "Function" << this << "is born";
56 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
57 if (lua_isnil(L, -1)) {
61 lua_setfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
66 lua_gettable(L, functionTable);
68 if (!lqtL_testudata(L, -1, "QObject*")) {
69 //qDebug() << "not QObject* is" << luaL_typename(L, -1);
71 // top of stack is the function I want
72 //qDebug() << "to be bound is" << luaL_typename(L, -1);
73 lua_pushlightuserdata(L, this);
75 lua_settable(L, functionTable);
76 // registry this is associated to this function
77 lqtL_passudata(L, this, "QObject*");
80 lua_settable(L, functionTable);
82 // leave the qobject on top;
83 //qDebug() << "Function" << this << "scheduled for deletion";
86 lua_replace(L, functionTable);
87 lua_settop(L, functionTable);
89 LuaFunction::~LuaFunction() {
90 //qDebug() << "Function" << this << "is dead";
91 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
92 lua_pushlightuserdata(L, this);
96 lua_pushlightuserdata(L, this);
103 int LuaFunction::__gc (lua_State *L) {
104 QPointer<QObject> *qp = (QPointer<QObject> *)lua_touserdata(L, 1);
106 (*qp)->deleteLater();
108 qDebug() << "LuaFunction" << *qp << "scheduled for deletion";
119 for nargs
= 1,ARG_MAX
do
120 signatures
[nargs
] = {}
121 for oldsig
, oldbody
in pairs(signatures
[nargs
-1]) do
122 for argtype
, argpush
in pairs(TYPES
) do
123 signatures
[nargs
][oldsig
..((nargs
==1) and '' or ', ')..argtype
..' arg'..tostring(nargs
)] = oldbody
..argpush
.."(L, arg"..tostring(nargs
)..');'
129 for s
, b
in pairs(signatures
[i
]) do
130 hpp
.write(' void function ('..s
..');\n')
131 cpp
.write'void LuaFunction::function ('
134 int functionTable = lua_gettop(L) + 1;
135 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
136 if (!lua_istable(L, -1)) {
139 lua_pushlightuserdata(L, this);
140 lua_gettable(L, functionTable);
144 cpp
.write'lua_call(L,'
145 cpp
.write(tostring(i
))
158 hpp
.write("#endif // "..HEADER_DEFINE
.."\n")