Lua: lua_register(lua_State* L, const char *s, lua_CFunction f, void* data)

Started by
3 comments, last by marcusz 18 years ago
Hi! Existing Lua API: lua_register(lua_State *L, const char *s, lua_CFunction f,) I want: lua_register(lua_State *L, const char *s, lua_CFunction f, void* data) Is there a way to associate a C-pointer, (or any type of data) when calling lua_register() and get to this data from within my C-callback when Lua calls it? Normally in C API:s, like the Windows API, when you register a C-callback you can also supply a void* that your callback gets as argument when it's invoked. This let's you register the same C-callback many times but with different void*:s. This is what I miss with lua_register(). I need this for wrapping the lua_State in a C++ class and having clients register callback interfaces with the wrapper. I'm not thrilled to have to pass the data pointer into the Lua scripts and require the Lua script to pass them as argument to my C API... Any ideas? /Marcus
Advertisement
Ehm. Is this what lua_pushcclosure() is for?

I've sorted of use the IAHIGA on the concept of "closures" (Ignore And Hope It Goes Away)...


/Marcus
How do I bump my own rating? :-)
That is indeed what lua_pushcclosure is for. Use lua_pushlightuserdata to stick your void pointer on the stack and then use it as an upvalue when registering the function. Later, you can get that upvalue from within the function and use it.
Thank you again for your help! It helps a lot to get this confirmed.

I 've been think about this problem on and off for a long time, and now I really had to solve it.


/Marcus

This topic is closed to new replies.

Advertisement