lqt icon indicating copy to clipboard operation
lqt copied to clipboard

Possible to add a way to interface with uchar*?

Open daurnimator opened this issue 12 years ago • 4 comments

I'm working with a QImage and would appriciate a way to work with the raw uchar * As a note: I am working in luajit, and using the ffi would be great! If you could just give me a userdata (eg, img:bits() ) I can use ffi.cast

daurnimator avatar May 31 '12 03:05 daurnimator

Using FFI has been discussed with Mike Pall before, however there is no plan in supporting all the various intricacies of the C++ ABI between operating systems and compilers. Therefore LuaJIT-FFI-Qt is not to be expected soon.

You can try extending types.lua with definition for unsigned char * based on the available implementation of base_types['char const*'], and use lua_pushlightuserdata() instead.

mkottman avatar May 31 '12 05:05 mkottman

Thanks for hint; diff is below:

$ git diff -w
diff --git a/common/lqt_common.cpp b/common/lqt_common.cpp
index 56c7a7c..3924237 100644
--- a/common/lqt_common.cpp
+++ b/common/lqt_common.cpp
@@ -375,6 +375,9 @@ bool lqtL_isstring (lua_State *L, int i) {
 bool lqtL_isboolean (lua_State *L, int i) {
     return lua_type(L, i)==LUA_TBOOLEAN;
 }
+bool lqtL_islightuserdata (lua_State *L, int i) {
+    return lua_type(L, i)==LUA_TLIGHTUSERDATA;
+}
 bool lqtL_missarg (lua_State *L, int index, int n) {
     bool ret = true;
     int i = 0;
diff --git a/common/lqt_common.hpp b/common/lqt_common.hpp
index de53760..690af14 100644
--- a/common/lqt_common.hpp
+++ b/common/lqt_common.hpp
@@ -105,6 +105,7 @@ bool lqtL_isinteger (lua_State *, int);
 bool lqtL_isnumber (lua_State *, int);
 bool lqtL_isstring (lua_State *, int);
 bool lqtL_isboolean (lua_State *, int);
+bool lqtL_islightuserdata (lua_State *, int);

 bool lqtL_missarg (lua_State *, int, int);
 //int lqtL_baseindex (lua_State *, int, int);
diff --git a/generator/types.lua b/generator/types.lua
index 8958fe2..d47aeb7 100644
--- a/generator/types.lua
+++ b/generator/types.lua
@@ -81,6 +81,18 @@ base_types['char const*'] = {
        end,
        onstack = 'string,',
 }
+base_types['unsigned char*'] = {
+       get = function(j)
+               return '(unsigned char*)lua_touserdata(L, '..tostring(j)..')', 1
+       end,
+       push = function(j) -- must handle arguments (e.g. in virtual callbacks) and return values
+               return 'lua_pushlightuserdata(L, '..tostring(j)..')', 1
+       end,
+       test = function(j)
+               return 'lqtL_islightuserdata(L, '..tostring(j)..')', 1
+       end,
+       onstack = 'string,',
+}
 base_types['char'] = integer_type(3)
 base_types['unsigned char'] = integer_type(3)
 base_types['int'] = integer_type(1)

daurnimator avatar May 31 '12 06:05 daurnimator

Should I make a pull request?

daurnimator avatar Jun 01 '12 00:06 daurnimator

That would be great :)

mkottman avatar Jun 01 '12 12:06 mkottman