Hello, I need some help or ideas for the following problem:Assume a third party C library to wich I can register any number of callbacks that this library will sometimes call, like:
foo * f = foo_open(...); foo_reg_cb(f, "nameA", <void *userdata>, <void *callback(void *)> ); foo_reg_cb(f, "nameB", <void *userdata>, <void *callback(void *)> ); ...I can register as many callbacks as I would like. Every callback is given a unique name. Now, somewhere later, the foo-library will use the callbacks during a heavy computation. The call to the libray is somthing like:
int l_foo_open(lua_State * L)
{
foo * f = foo_open(lua_tostring(L, 1), ...);
lua_pushlightuserdata(L, f);
return 1;
}
int l_foo_compute(lua_State * L)
{
foo * f = (foo *)lua_touserdata(L, 1);
foo_compute(f);
return 0;
}
Now the problem:
How can I write a wrapper arround foo_reg_cb() to register lua
functions? This will not work:
static int callback_wrapper(void *userdata)
{
lua_State * L = (lua_State * userdata);
/* PROBLEM: how to find the the function to call ???
lua_call(L, 0, 0);
return 0;
}
int l_foo_reg_cb(lua_State * L)
{
foo * f = (foo *)lua_touserdata(L, 1);
char * name = lua_tostring(L, 2);
/* PROBLEM: What todo with the 3rd arg, the lua callback function? */
foo_reg_cb(f, name, L, callback_wrapper);
return 0;
}
How should I deal with this problem? I don't like the idea of a global
lua_State and pass the function itself or some reference to the wrapper,
because then it won't be possible to use this binding with two or more
different lua-States in the same host.
The Problem is, that there is not an fixed number of callbacks, so that
I can't write N callback_wrappers.
Any Ideas?
Attachment:
signature.asc
Description: OpenPGP digital signature