lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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);
  int value=luaL_checkint(L,3);
  int count=luaL_checkint(L,4);
  CvTrackbarCallback on_change=NULL;

  if (lua_isfunction(L,5))
    on_change=(CvTrackbarCallback)lua_tocfunction(L,5);
  else
    luaL_error(L,"luacv.cvCreateTrackbar(string trackbar_name,string
window_name,int value,int count, void func(int pos))");

  cvCreateTrackbar(trackbar_name,window_name,&value,count,on_change);

  return 0;
}

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

cv=require('luacv')

function on_trackbar(pos)
    ...do some stuff here..
end

cv.cvCreateTrackbar(tbarname,wndname,edge_thresh,100,on_trackbar)


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
work.
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.

With regards,
Jiri Prymus

2009/10/17 Asko Kauppi <askok@dnainternet.net>:
> 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
> public.
>
> - asko
>
> On 17.10.2009, at 21.03, Wesley Smith <wesley.hoke@gmail.com> wrote:
>
>> I don't completely follow, but you can store arbitrary function pointers
>> as lightuserdata.
>>
>> wes
>>
>> On Oct 17, 2009, at 1:47 AM, Jiří Prymus <jiri.prymus@gmail.com> wrote:
>>
>>> Hi,
>>>
>>> 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
>