fixed old include in lqt_common.cpp
[lqt.git] / generate_function.lua
bloba224eaff87e44d00beecf60782cfb0850337dca6
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 "luafunction.hpp"\n ')
26 hpp.write[[
28 #include "common_bind.hpp"
30 #include <QObject>
31 #include <QDebug>
33 #define LUA_FUNCTION_REGISTRY "LuaFunction"
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);
44 cpp.write[[
45 LuaFunction::LuaFunction(lua_State *state):L(state) {
46 int functionTable = lua_gettop(L); // not yet but soon
47 qDebug() << "Function" << this << "is born";
48 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
49 if (lua_isnil(L, -1)) {
50 lua_pop(L, 1);
51 lua_newtable(L);
52 lua_pushvalue(L, -1);
53 lua_setfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
55 lua_insert(L, -2);
57 lua_pushvalue(L, -1);
58 lua_gettable(L, functionTable);
60 if (!lqtL_testudata(L, -1, "QObject*")) {
61 qDebug() << "not QObject* is" << luaL_typename(L, -1);
62 lua_pop(L, 1);
63 // top of stack is the function I want
64 qDebug() << "to be bound is" << luaL_typename(L, -1);
65 lua_pushlightuserdata(L, this);
66 lua_pushvalue(L, -2);
67 lua_settable(L, functionTable);
68 // registry this is associated to this function
69 lqtL_passudata(L, this, "QObject*");
70 lua_insert(L, -2);
71 lua_pushvalue(L, -2);
72 lua_settable(L, functionTable);
73 } else {
74 // leave the qobject on top;
75 qDebug() << "Function" << this << "scheduled for deletion";
76 this->deleteLater();
78 lua_replace(L, functionTable);
79 lua_settop(L, functionTable);
82 hpp.write[[
83 virtual ~LuaFunction();
85 private:
86 lua_State *L;
87 static int __gc (lua_State *L);
88 protected:
89 public:
90 public slots:
92 cpp.write[[
93 LuaFunction::~LuaFunction() {
94 qDebug() << "Function" << this << "is dead";
97 --[[
98 int LuaFunction::__gc (lua_State *L) {
99 QPointer<QObject> *qp = (QPointer<QObject> *)lua_touserdata(L, 1);
100 if (*qp) {
101 (*qp)->deleteLater();
103 qDebug() << "LuaFunction" << *qp << "scheduled for deletion";
104 return 0;
108 signatures = {
109 [0] = {
110 [''] = '',
114 for nargs = 1,ARG_MAX do
115 signatures[nargs] = {}
116 for oldsig, oldbody in pairs(signatures[nargs-1]) do
117 for argtype, argpush in pairs(TYPES) do
118 signatures[nargs][oldsig..((nargs==1) and '' or ', ')..argtype..' arg'..tostring(nargs)] = oldbody..argpush.."(L, arg"..tostring(nargs)..');'
123 for i=0,ARG_MAX do
124 for s, b in pairs(signatures[i]) do
125 hpp.write(' void function ('..s..');\n')
126 cpp.write'void LuaFunction::function ('
127 cpp.write(s)
128 cpp.write[[) {
129 int functionTable = lua_gettop(L) + 1;
130 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
131 if (!lua_istable(L, -1)) {
132 return;
134 lua_pushlightuserdata(L, this);
135 lua_gettable(L, functionTable);
137 cpp.write(b)
138 cpp.write'\n '
139 cpp.write'lua_call(L,'
140 cpp.write(tostring(i))
141 cpp.write', 0);\n'
142 cpp.write[[
143 };]]
144 cpp.write'\n'
148 hpp.write[[
154 hpp.write("#endif // "..HEADER_DEFINE.."\n")
157 io.write(cpp.string)