From cceafc0314dbcab09958ff49a2fc15de88b47846 Mon Sep 17 00:00:00 2001 From: Mauro Iazzi Date: Sun, 23 Sep 2007 16:34:58 +0200 Subject: [PATCH] added support for manipulating char* and int& so QApplication can be instantiated from Lua --- binder.lua | 7 ++++++ lqt_common.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lqt_common.hpp | 6 +++++ 3 files changed, 88 insertions(+) diff --git a/binder.lua b/binder.lua index 05698cf..c1e7793 100644 --- a/binder.lua +++ b/binder.lua @@ -24,6 +24,8 @@ function binder:init(filename) self.ids = xml.id self.type_names = {} self.types_to_stack = { + ['char * *'] = function(i) return 'lqtL_pusharguments(L, ' .. tostring(i) .. ')' end, + ['const char * *'] = function(i) return 'lqtL_pusharguments(L, ' .. tostring(i) .. ')' end, ['const char *'] = function(i) return 'lua_pushstring(L, ' .. tostring(i) .. ')' end, ['short int'] = function(i) return 'lua_pushinteger(L, ' .. tostring(i) .. ')' end, ['unsigned short int'] = function(i) return 'lua_pushinteger(L, ' .. tostring(i) .. ')' end, @@ -42,6 +44,9 @@ function binder:init(filename) ['void * *'] = function(i) return 'lua_pushlightuserdata(L, ' .. tostring(i) .. ')' end, } self.types_from_stack = { + ['int&'] = function(i) return 'lqtL_tointref(L, ' .. tostring(i) .. ')' end, + ['char * *'] = function(i) return 'lqtL_toarguments(L, ' .. tostring(i) .. ')' end, + ['const char * *'] = function(i) return 'lqtL_toarguments(L, ' .. tostring(i) .. ')' end, ['const char *'] = function(i) return 'lua_tostring(L, ' .. tostring(i) .. ')' end, ['short int'] = function(i) return 'lua_tointeger(L, ' .. tostring(i) .. ')' end, ['unsigned short int'] = function(i) return 'lua_tointeger(L, ' .. tostring(i) .. ')' end, @@ -60,6 +65,8 @@ function binder:init(filename) ['void * *'] = function(i) return 'static_cast(lua_touserdata(L, ' .. tostring(i) .. '))' end, } self.types_test = { + ['char * *'] = function(i) return 'lqtL_testarguments(L, ' .. tostring(i) .. ')' end, + ['const char * *'] = function(i) return 'lqtL_testarguments(L, ' .. tostring(i) .. ')' end, ['const char *'] = function(i) return '(lua_type(L, ' .. tostring(i) .. ')==LUA_TSTRING)' end, ['short int'] = function(i) return 'lua_isnumber(L, ' .. tostring(i) .. ')' end, ['unsigned short int'] = function(i) return 'lua_isnumber(L, ' .. tostring(i) .. ')' end, diff --git a/lqt_common.cpp b/lqt_common.cpp index 3054764..15007b8 100644 --- a/lqt_common.cpp +++ b/lqt_common.cpp @@ -1,6 +1,8 @@ #include "lqt_common.hpp" +#include #define LQT_FIXEDINDEX(i) (i<0?(1+lua_gettop(L)+i):i) +#define LQT_MAX_ARGS 50 //#include #ifndef SEE_STACK @@ -19,6 +21,79 @@ //#include //using namespace std; +int check_gc(lua_State*L){ + lua_newtable(L); + lua_getglobal(L, "print"); + lua_setfield(L, -2, "__gc"); + lua_setmetatable(L, -2); + return 0; +} + +void *get_buffer(lua_State *L, size_t sz) { +#if 1 + void *ret = lua_newuserdata(L, sz); + //check_gc(L); + lua_pushlightuserdata(L, ret); + lua_insert(L, -2); + lua_settable(L, LUA_REGISTRYINDEX); +#else + void *ret = malloc(sz); + cout << ret << endl; +#endif + return ret; +} + +int& lqtL_tointref (lua_State *L, int i) { + i = LQT_FIXEDINDEX(i); + int *ret = NULL; + ret = (int*)get_buffer(L, sizeof(int)); + *ret = lua_type(L, i)==LUA_TNUMBER?lua_tointeger(L, i):0; + //cout << "interef " << ret << endl; + return *ret; +} +void lqtL_pusharguments (lua_State *L, const char **argv) { + int i = 0; + lua_newtable(L); + for (i=0;*argv && i class LuaBinder; +extern int& lqtL_tointref (lua_State *, int); + +extern void lqtL_pusharguments (lua_State *, const char**); +extern char** lqtL_toarguments (lua_State *, int); +extern bool lqtL_testarguments (lua_State *, int); + extern void lqtL_manageudata (lua_State *, int); extern void lqtL_unmanageudata (lua_State *, int); extern void lqtL_pushudata (lua_State *, const void *, const char *); -- 2.11.4.GIT