Create a coroutine to run plans

This commit is contained in:
2021-02-17 13:38:22 -05:00
parent 210644da57
commit 213bf12425
8 changed files with 122 additions and 45 deletions

View File

@@ -4,26 +4,26 @@
#define TRACEBACK_LEVELS2 10
static int traceback_general(lua_State *L, lua_State *L1, int msgindex) {
// Call this with the error message on top of the stack.
static void traceback_general(lua_State *L, lua_State *L1, int baselevel) {
int top = lua_gettop(L);
// Convert message to a string and push the string.
if (lua_tostring(L, msgindex)) {
lua_pushvalue(L, msgindex);
} else {
luaL_callmeta(L, msgindex, "__tostring");
}
// If we didn't end up with exactly one string on
// the stack, then clear the stack and push 'unknown error'.
if ((lua_gettop(L) != top + 1) || (!lua_tostring(L, -1))) {
lua_settop(L, top);
lua_pushstring(L, "unknown error");
// Convert message to a string
if (!lua_tostring(L, top)) {
luaL_callmeta(L, top, "__tostring");
// If callmeta didn't produce exactly one string, clear the stack
// and push "unknown error"
if ((lua_gettop(L) == top + 1) && (lua_tostring(L, -1))) {
lua_remove(L, top);
} else {
lua_settop(L, top - 1);
lua_pushstring(L, "unknown error");
}
}
// Append the traceback.
lua_Debug ar;
int level = 1;
int level = baselevel;
int firstpart = 1;
while (lua_getstack(L1, level++, &ar)) {
if (level > TRACEBACK_LEVELS1 && firstpart) {
@@ -55,27 +55,25 @@ static int traceback_general(lua_State *L, lua_State *L1, int msgindex) {
lua_pushfstring(L, " in function <%s:%d>",
ar.short_src, ar.linedefined);
}
if (lua_gettop(L) - top > 5) {
lua_concat(L, lua_gettop(L) - top);
if (1 + lua_gettop(L) - top > 5) {
lua_concat(L, 1 + lua_gettop(L) - top);
}
}
}
lua_pushstring(L, "\n");
if (lua_gettop(L) - top > 1) {
lua_concat(L, lua_gettop(L) - top);
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;
}
LuaDefine(traceback_handler, "c") {
return traceback_general(L, L, lua_gettop(L));
}
LuaDefine(traceback_coroutine, "c") {
LuaArg thread, message;
LuaStack LS(L, thread, message);
lua_State *L1 = LS.ckthread(thread);
return traceback_general(L, L1, message.index());
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) {