I'll want to implement a debugger to navigate through a lua script step by step. The script is embedded in a win32 c++ enviroment, created with the main gui thread and called by gui interaction like buttons, timers. A second thread also calls the script using CSingleLock protection.
First, I tried this:
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) {}
}
Here are some example calls to the script:
// 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;
}
Because the while loop blocks the gui I tried to use coroutines:
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;
}
and
void trace(lua_State *L, lua_Debug *ar) {
// display debug info
lua_yieldk(L, 0, 0, 0);
}
A next step button press should reactivate the script using lua_resume. Unfortunatley there's this error: "attempt to yield from outside a coroutine".