Skip to content

Commit

Permalink
Fixed debugger not adding hooks to old coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
MCJack123 committed May 10, 2020
1 parent 0a93116 commit 03ff0c5
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/lbaselib.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,45 @@ static int luaB_auxwrap (lua_State *L) {
return r;
}

static int luaB_codelete (lua_State *L) {
lua_getfield(L, LUA_REGISTRYINDEX, "_coroutine_stack"); /* remove coroutine from coroutine stack */
if (lua_istable(L, -1)) {
int found = 0;
for (int i = 1; i <= lua_objlen(L, -1); i++) {
lua_rawgeti(L, -1, i);
if (found) {
lua_rawseti(L, -2, i - found);
lua_pushnil(L);
lua_rawseti(L, -2, i);
} else {
if (lua_tothread(L, -1) == lua_tothread(L, 1)) {
found = 1;
lua_pushnil(L);
lua_rawseti(L, -3, i);
}
lua_pop(L, 1);
}
}
}
lua_pop(L, 1);
return 0;
}

static int luaB_cocreate (lua_State *L) {
lua_State *NL = lua_newthread(L);
luaL_checkany(L, 1); /* any callable is ok */
lua_pushvalue(L, 1); /* move function to top */
lua_xmove(L, NL, 1); /* move function from L to NL */
lua_getfield(L, LUA_REGISTRYINDEX, "_coroutine_stack"); /* add coroutine to coroutine stack */
if (lua_istable(L, -1)) {
lua_pushvalue(L, -2);
lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
lua_newtable(L);
lua_pushcfunction(L, luaB_codelete);
lua_setfield(L, -2, "__gc");
lua_setmetatable(L, -3);
}
lua_pop(L, 1);
return 1;
}

Expand Down

0 comments on commit 03ff0c5

Please sign in to comment.