More work on thread scheduling and tracebacks

This commit is contained in:
2021-02-20 19:17:20 -05:00
parent 36ff4eacae
commit 5f783e643b
7 changed files with 44 additions and 30 deletions

View File

@@ -5,7 +5,9 @@
// Call this with the error message on top of the stack.
static void traceback_general(lua_State *L, lua_State *L1, int baselevel) {
// The error message is replaced with a traceback.
//
int traceback_coroutine(lua_State *L) {
int top = lua_gettop(L);
// Convert message to a string
@@ -23,22 +25,24 @@ static void traceback_general(lua_State *L, lua_State *L1, int baselevel) {
// Append the traceback.
lua_Debug ar;
int level = baselevel;
int firstpart = 1;
while (lua_getstack(L1, level++, &ar)) {
for (int level = 0; lua_getstack(L, level, &ar); level++) {
if (level > TRACEBACK_LEVELS1 && firstpart) {
/* no more than `LEVELS2' more levels? */
if (!lua_getstack(L1, level + TRACEBACK_LEVELS2, &ar))
if (!lua_getstack(L, level + TRACEBACK_LEVELS2, &ar))
level--; /* keep going */
else {
lua_pushliteral(L, "\n\t..."); /* too many levels */
while (lua_getstack(L1, level + TRACEBACK_LEVELS2, &ar)) /* find last levels */
while (lua_getstack(L, level + TRACEBACK_LEVELS2, &ar)) /* find last levels */
level++;
}
firstpart = 0;
continue;
}
lua_getinfo(L1, "Snl", &ar);
lua_getinfo(L, "Snl", &ar);
if ((level == 0) && (*ar.what == 'C') && (std::string(ar.name) == "__newindex")) {
continue;
}
if ((ar.currentline > 0) || (*ar.namewhat != 0) || (*ar.what != 'C')) {
lua_pushliteral(L, "\n\t");
lua_pushfstring(L, "%s:", ar.short_src);
@@ -64,22 +68,14 @@ static void traceback_general(lua_State *L, lua_State *L1, int baselevel) {
if (1 + lua_gettop(L) - top > 1) {
lua_concat(L, 1 + lua_gettop(L) - top);
}
}
int traceback_handler(lua_State *L) {
traceback_general(L, L, 1);
return 1;
}
void traceback_coroutine(lua_State *L, lua_State *CO) {
lua_xmove(CO, L, 1);
traceback_general(L, CO, 0);
}
int traceback_pcall(lua_State *L, int narg, int nret) {
int status;
int base = lua_gettop(L) - narg; /* function index */
lua_pushcfunction(L, traceback_handler); /* push traceback function */
lua_pushcfunction(L, traceback_coroutine); /* push traceback function */
lua_insert(L, base); /* put it under chunk and args */
status = lua_pcall(L, narg, nret, base);
lua_remove(L, base); /* remove traceback function */