[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Conversion Lua function to C function
- From: Jiří Prymus <jiri.prymus@...>
- Date: Sat, 17 Oct 2009 18:22:07 +0200
Thanks for answers.
I'm little confused from yours replies but I try to explain my problem clearly.
I need to pass lua function to C++ wrapper function where it will be
used as "update event" of GtkTrackBar widget.
My wrapper is compiled as shared library for lua.
Maybe that sample code make this problem clear.
C++ wrapper function -----------------
static int luacv_cvCreateTrackbar(lua_State *L)
const char *trackbar_name=luaL_checkstring(L,1);
const char *window_name=luaL_checkstring(L,2);
window_name,int value,int count, void func(int pos))");
Type CvTrackbarCallback is defined like
typedef void (CV_CDECL *CvTrackbarCallback)(int pos);
As you can see , the "on_change" variable is pointer to c function
with int as argument.
Lua script may be look like this
...do some stuff here..
So I can get pointer of lua function "on_trackbar" via stack in
wrapper , but when I want to convert it to c function, it doesn't
No segmentation fault, but it does nothing.
I tried functions lua_tolightuserdata, lua_touserdata, lua_tocfunction
but none of them worked.
Thanks for answer and sorry for my english.
2009/10/17 Asko Kauppi <firstname.lastname@example.org>:
> What he needs to do is store the Lua function entry as such (make a copy,
> forget about C pointers) in the Lua registry or a userdata's environment.
> This is a common need and many source codes will carry samples. Then have a
> C side common callback which picks up that stored ref to the Lua function
> and executes it. I've made a helper for all this but that code is not
> - asko
> On 17.10.2009, at 21.03, Wesley Smith <email@example.com> wrote:
>> I don't completely follow, but you can store arbitrary function pointers
>> as lightuserdata.
>> On Oct 17, 2009, at 1:47 AM, Jiří Prymus <firstname.lastname@example.org> wrote:
>>> I would like to know if there is any chance to convert lua function to
>>> c function. I program a small opencv wrapper and there is one wrapping
>>> function where I need pointer to function. And that's a problem
>>> because I can get only pointer to Lua function and when I convert it
>>> to C function then it does nothing. I know that lua functions are
>>> stored in tables and that's probably why it doesn't work.
>>> In fact my problem is function gtk_signal_connect(...) where slot
>>> function is my lua function.
>>> Thanks for answer.
>>> With regards,
>>> Jiri Prymus