Reputation: 1512
I'm trying to learn lua and building a virtual machine in c++, I want to write a debugging class, to make my life easier. I'm actually blocked because I don't understand how the callbacks are done, here is my code :
//Im here adding my fct to a global map.
void Debugger::setFctHook(void)
{
g_hookers[LUA_MASKCALL] = HookCall;
g_hookers[LUA_MASKRET] = HookRet;
g_hookers[LUA_HOOKTAILRET] = HookRet;
g_hookers[LUA_MASKLINE] = HookLine;
g_hookers[LUA_MASKCOUNT] = HookCount;
}
Here is my constructor :
Debugger::Debugger(VirtualMachine &vm, uint count)
: VM_(vm), count_(count)
{
setFctHook();
if (vm.isFonctionnal())
{
vm.addDebugger(this);
lua_sethook(vm.getLua(), HookEvents, 0, count_);
}
}
and my setter :
void Debugger ::setHook(int hookMask) const
{
std::cout << hookMask << "SETHOOOOOOOOOK" << std::endl;
lua_sethook(VM_.getLua(), HookEvents, hookMask, count_);
}
Here is my Central hook :
static void HookEvents(lua_State *lua, lua_Debug *debug)
{
std::map<int, fctHook>::iterator it;
std::cout << debug->event << std::endl;
it = g_hookers.find(debug->event);
if (it != g_hookers.end())
{
std::cout << "First: " << it->first << std::endl;
it->second(lua);
}
}
The problem is that the value show in my setter differs from the value printed in my central function hook, I tried many defines and I dont see any logic in the different values.
Result :
8 SETHOOOOOOOOOK // received on my setter.
3 // received on my central hook
Upvotes: 0
Views: 1346
Reputation: 1512
I solved my problem, the problem is that my map has bad values on it, the correct defines for the hooks are :
void Debugger::setFctHook(void)
{
g_hookers[LUA_HOOKCALL] = HookCall;
g_hookers[LUA_HOOKRET] = HookRet;
g_hookers[LUA_HOOKTAILRET] = HookRet;
g_hookers[LUA_HOOKLINE] = HookLine;
g_hookers[LUA_HOOKCOUNT] = HookCount;
}
Upvotes: 1