I'm trying to write a debugger for a process running lua scripts, and the documented way of doing so (in C) is to use lua_sethook
:
int lua_sethook (lua_State *L, lua_Hook f, int mask, int count);
lua_Hook
is defined as:
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
The hook only gets a lua_State
pointer, which is great, but how can I associate a pointer to my debugger class with it so that I can get back into my debugger class from there?
I would like to avoid using a global variable in this case as I have multiple lua_State
instances. I suppose I could use a map of lua_State *
pointers to debugger instances, but that doesn't seem efficient. And storing it as a global in the lua_State *
doesn't seem to make sense because in order to be able to retrieve it, I would have to push at least one value onto the lua stack, which seems hard/impossible to do in the case of a lua stack overflow.
Am I missing something? How would I accomplish this? I know, I could accomplish this in lua code, but I would like to understand how I can do this from the C side.