Added support for finding Lua in cmake
[lqt.git] / generate_function.lua
blobbfce6fc7ec1d1d47be5746acdc97712fc4306043
1 #!/usr/bin/lua
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' }
7 cpp = {
8 string = '',
9 write = function (s)
10 cpp.string = cpp.string .. tostring(s)
11 end,
14 hpp = {
15 string = '',
16 write = function (s)
17 hpp.string = hpp.string .. tostring(s)
18 end,
21 hpp.write("#ifndef "..HEADER_DEFINE.."\n")
22 hpp.write("#define "..HEADER_DEFINE.."\n")
23 cpp.write('#include "lqt_function.hpp"\n ')
26 hpp.write[[
28 #include "lqt_common.hpp"
30 #include <QObject>
31 //#include <QDebug>
33 #define LUA_FUNCTION_REGISTRY "Registry Function"
34 //#ifndef SEE_STACK
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); }
36 //#endif
38 class LuaFunction: public QObject {
39 Q_OBJECT
41 public:
42 LuaFunction(lua_State *state);
43 virtual ~LuaFunction();
45 private:
46 lua_State *L;
47 static int __gc (lua_State *L);
48 protected:
49 public:
50 public slots:
52 cpp.write[[
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)) {
58 lua_pop(L, 1);
59 lua_newtable(L);
60 lua_pushvalue(L, -1);
61 lua_setfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
63 lua_insert(L, -2);
65 lua_pushvalue(L, -1);
66 lua_gettable(L, functionTable);
68 if (!lqtL_testudata(L, -1, "QObject*")) {
69 //qDebug() << "not QObject* is" << luaL_typename(L, -1);
70 lua_pop(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);
74 lua_pushvalue(L, -2);
75 lua_settable(L, functionTable);
76 // registry this is associated to this function
77 lqtL_passudata(L, this, "QObject*");
78 lua_insert(L, -2);
79 lua_pushvalue(L, -2);
80 lua_settable(L, functionTable);
81 } else {
82 // leave the qobject on top;
83 //qDebug() << "Function" << this << "scheduled for deletion";
84 this->deleteLater();
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);
93 lua_gettable(L, -2);
94 lua_pushnil(L);
95 lua_settable(L, -3);
96 lua_pushlightuserdata(L, this);
97 lua_pushnil(L);
98 lua_settable(L, -3);
99 lua_pop(L, 1);
102 --[[
103 int LuaFunction::__gc (lua_State *L) {
104 QPointer<QObject> *qp = (QPointer<QObject> *)lua_touserdata(L, 1);
105 if (*qp) {
106 (*qp)->deleteLater();
108 qDebug() << "LuaFunction" << *qp << "scheduled for deletion";
109 return 0;
113 signatures = {
114 [0] = {
115 [''] = '',
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)..');'
128 for i=0,ARG_MAX do
129 for s, b in pairs(signatures[i]) do
130 hpp.write(' void function ('..s..');\n')
131 cpp.write'void LuaFunction::function ('
132 cpp.write(s)
133 cpp.write[[) {
134 int functionTable = lua_gettop(L) + 1;
135 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
136 if (!lua_istable(L, -1)) {
137 return;
139 lua_pushlightuserdata(L, this);
140 lua_gettable(L, functionTable);
142 cpp.write(b)
143 cpp.write'\n '
144 cpp.write'lua_call(L,'
145 cpp.write(tostring(i))
146 cpp.write', 0);\n'
147 cpp.write[[
148 };]]
149 cpp.write'\n'
153 hpp.write[[
158 hpp.write("#endif // "..HEADER_DEFINE.."\n")
161 io.write(hpp.string)