the list of duplicates is ready (still not used)
[lqt.git] / old_bindings / generate_function.lua
blobea93ee6a1775881f69b5e40f89ff20528ef9fcf2
1 #!/usr/bin/lua
3 --[[
5 Copyright (c) 2007-2008 Mauro Iazzi
7 Permission is hereby granted, free of charge, to any person
8 obtaining a copy of this software and associated documentation
9 files (the "Software"), to deal in the Software without
10 restriction, including without limitation the rights to use,
11 copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the
13 Software is furnished to do so, subject to the following
14 conditions:
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 OTHER DEALINGS IN THE SOFTWARE.
28 --]]
30 HEADER_DEFINE = tostring(os.getenv'HEADER_DEFINE' or '__LQT_FUNCTION')
31 ARG_MAX = tonumber(os.getenv'ARG_MAX' or 2)
32 TYPES = { bool='lua_pushboolean', int='lua_pushinteger', double='lua_pushnumber', ['const char *']='lua_pushstring' }
34 cpp = {
35 string = '',
36 write = function (s)
37 cpp.string = cpp.string .. tostring(s)
38 end,
41 hpp = {
42 string = '',
43 write = function (s)
44 hpp.string = hpp.string .. tostring(s)
45 end,
48 hpp.write("#ifndef "..HEADER_DEFINE.."\n")
49 hpp.write("#define "..HEADER_DEFINE.."\n")
50 cpp.write('#include "lqt_function.hpp"\n ')
53 hpp.write[[
55 #include "lqt_common.hpp"
57 #include <QObject>
58 //#include <QDebug>
60 #define LUA_FUNCTION_REGISTRY "Registry Function"
61 //#ifndef SEE_STACK
62 //# define SEE_STACK(L, j) for (int j=1;j<=lua_gettop(L);j++) { qDebug() << j << '=' << luaL_typename(L, j) << '@' << lua_topointer (L, j); }
63 //#endif
65 class LuaFunction: public QObject {
66 Q_OBJECT
68 public:
69 LuaFunction(lua_State *state);
70 virtual ~LuaFunction();
72 private:
73 lua_State *L;
74 static int __gc (lua_State *L);
75 protected:
76 public:
77 public slots:
79 cpp.write[[
80 LuaFunction::LuaFunction(lua_State *state):L(state) {
81 int functionTable = lua_gettop(L); // not yet but soon
82 //qDebug() << "Function" << this << "is born";
83 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
84 if (lua_isnil(L, -1)) {
85 lua_pop(L, 1);
86 lua_newtable(L);
87 lua_pushvalue(L, -1);
88 lua_setfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
90 lua_insert(L, -2);
92 lua_pushvalue(L, -1);
93 lua_gettable(L, functionTable);
95 if (!lqtL_testudata(L, -1, "QObject*")) {
96 //qDebug() << "not QObject* is" << luaL_typename(L, -1);
97 lua_pop(L, 1);
98 // top of stack is the function I want
99 //qDebug() << "to be bound is" << luaL_typename(L, -1);
100 lua_pushlightuserdata(L, this);
101 lua_pushvalue(L, -2);
102 lua_settable(L, functionTable);
103 // registry this is associated to this function
104 lqtL_passudata(L, this, "QObject*");
105 lua_insert(L, -2);
106 lua_pushvalue(L, -2);
107 lua_settable(L, functionTable);
108 } else {
109 // leave the qobject on top;
110 //qDebug() << "Function" << this << "scheduled for deletion";
111 this->deleteLater();
113 lua_replace(L, functionTable);
114 lua_settop(L, functionTable);
116 LuaFunction::~LuaFunction() {
117 //qDebug() << "Function" << this << "is dead";
118 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
119 lua_pushlightuserdata(L, this);
120 lua_gettable(L, -2);
121 lua_pushnil(L);
122 lua_settable(L, -3);
123 lua_pushlightuserdata(L, this);
124 lua_pushnil(L);
125 lua_settable(L, -3);
126 lua_pop(L, 1);
129 --[[
130 int LuaFunction::__gc (lua_State *L) {
131 QPointer<QObject> *qp = (QPointer<QObject> *)lua_touserdata(L, 1);
132 if (*qp) {
133 (*qp)->deleteLater();
135 qDebug() << "LuaFunction" << *qp << "scheduled for deletion";
136 return 0;
140 signatures = {
141 [0] = {
142 [''] = '',
146 for nargs = 1,ARG_MAX do
147 signatures[nargs] = {}
148 for oldsig, oldbody in pairs(signatures[nargs-1]) do
149 for argtype, argpush in pairs(TYPES) do
150 signatures[nargs][oldsig..((nargs==1) and '' or ', ')..argtype..' arg'..tostring(nargs)] = oldbody..argpush.."(L, arg"..tostring(nargs)..');'
155 for i=0,ARG_MAX do
156 for s, b in pairs(signatures[i]) do
157 hpp.write(' void function ('..s..');\n')
158 cpp.write'void LuaFunction::function ('
159 cpp.write(s)
160 cpp.write[[) {
161 int functionTable = lua_gettop(L) + 1;
162 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
163 if (!lua_istable(L, -1)) {
164 return;
166 lua_pushlightuserdata(L, this);
167 lua_gettable(L, functionTable);
169 cpp.write(b)
170 cpp.write'\n '
171 cpp.write'lua_call(L,'
172 cpp.write(tostring(i))
173 cpp.write', 0);\n'
174 cpp.write[[
175 };]]
176 cpp.write'\n'
180 hpp.write[[
185 hpp.write("#endif // "..HEADER_DEFINE.."\n")
188 io.write(hpp.string)