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
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.
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' }
37 cpp
.string = cpp
.string .. tostring(s
)
44 hpp
.string = hpp
.string .. tostring(s
)
48 hpp
.write("#ifndef "..HEADER_DEFINE
.."\n")
49 hpp
.write("#define "..HEADER_DEFINE
.."\n")
50 cpp
.write('#include "lqt_function.hpp"\n ')
55 #include "lqt_common.hpp"
60 #define LUA_FUNCTION_REGISTRY "Registry Function"
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); }
65 class LuaFunction: public QObject {
69 LuaFunction(lua_State *state);
70 virtual ~LuaFunction();
74 static int __gc (lua_State *L);
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)) {
88 lua_setfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
93 lua_gettable(L, functionTable);
95 if (!lqtL_testudata(L, -1, "QObject*")) {
96 //qDebug() << "not QObject* is" << luaL_typename(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*");
106 lua_pushvalue(L, -2);
107 lua_settable(L, functionTable);
109 // leave the qobject on top;
110 //qDebug() << "Function" << this << "scheduled for deletion";
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);
123 lua_pushlightuserdata(L, this);
130 int LuaFunction::__gc (lua_State *L) {
131 QPointer<QObject> *qp = (QPointer<QObject> *)lua_touserdata(L, 1);
133 (*qp)->deleteLater();
135 qDebug() << "LuaFunction" << *qp << "scheduled for deletion";
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
)..');'
156 for s
, b
in pairs(signatures
[i
]) do
157 hpp
.write(' void function ('..s
..');\n')
158 cpp
.write'void LuaFunction::function ('
161 int functionTable = lua_gettop(L) + 1;
162 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FUNCTION_REGISTRY);
163 if (!lua_istable(L, -1)) {
166 lua_pushlightuserdata(L, this);
167 lua_gettable(L, functionTable);
171 cpp
.write'lua_call(L,'
172 cpp
.write(tostring(i
))
185 hpp
.write("#endif // "..HEADER_DEFINE
.."\n")