如何使用lua_yield和lua_sethook逐行进入代码?

时间:2016-10-10 21:53:44

标签: c++ debugging lua hook yield

我想实现一个调试器来逐步浏览lua脚本。该脚本嵌入在win32 c ++环境中,使用主gui线程创建,并通过gui交互调用,如按钮,计时器。第二个线程也使用CSingleLock保护调用脚本。

首先,我试过这个:

lua_State* start() {
  lua_State* L = luaL_newstate(); 
  luaL_loadfile(L, "script.lua");
  lua_pcall(L, 0, LUA_MULTRET, 0);
  int count(0);
  lua_sethook(L, trace, LUA_MASKLINE, count);
  return L;
}

void trace(lua_State *L, lua_Debug *ar) {
  // display debug info 
  while (blocked) {}
}

以下是对脚本的一些示例调用:

// called repeatedly by the gui
void timer() {
   lua_lock();
   lua_getglobal(L,"timer"); 
   lua_pcall(L, 0, 0, 0);
   lua_unlock();
}

// called often by another thread
int gettemperature() {  
  lua_lock();
  lua_getglobal(L,"gettemperature");
  lua_pcall(L, 0, 1, 0);
  int temperature = lua_checknumber(L, -1);
  lua_unlock();
  return temperature;
}

因为while循环阻止gui我试图使用协同程序:

lua_State* start() {
   lua_State* L = luaL_newstate(); 
   lua_State* co = luaL_newthread(L); 
   luaL_loadfile(co, "script.lua");
   lua_resume(co, L, 0);
   int count(0);
   lua_sethook(L, trace, LUA_MASKLINE, count);
   return co;
}

void trace(lua_State *L, lua_Debug *ar) {
   // display debug info 
   lua_yieldk(L, 0, 0, 0);
}

下一步按下按钮应使用lua_resume重新激活脚本。 Unfortunatley有这样的错误:“试图从协程外面屈服”。

0 个答案:

没有答案
相关问题