Lua: lua_register(lua_State* L, const char *s, lua_CFunction f, void* data)
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
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
I've sorted of use the IAHIGA on the concept of "closures" (Ignore And Hope It Goes Away)...
/Marcus
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement