From https://zeux.io/2010/11/07/lua-callstack-with-c-debugger/ and also https://github.com/defold/defold/blob/dev/engine/lua/src/lua/ldblib.c#L321
| 349 | // and also |
| 350 | // https://github.com/defold/defold/blob/dev/engine/lua/src/lua/ldblib.c#L321 |
| 351 | void GetLuaTraceback(lua_State* L, const char* infostring, void (*cbk)(lua_State* L, lua_Debug* entry, void* ctx), void* ctx) |
| 352 | { |
| 353 | const int LEVELS1 = 12; // size of the first part of the stack |
| 354 | const int LEVELS2 = 10; // size of the second part of the stack |
| 355 | |
| 356 | int firstpart = 1; |
| 357 | int arg = 0; |
| 358 | lua_State* L1 = GetLuaThread(L, &arg); |
| 359 | |
| 360 | lua_Debug entry; |
| 361 | int level = 0; |
| 362 | |
| 363 | if (lua_isnumber(L, arg+2)) { |
| 364 | level = (int)lua_tointeger(L, arg+2); |
| 365 | lua_pop(L, 1); // problematic?! |
| 366 | } |
| 367 | else { |
| 368 | level = (L == L1) ? 1 : 0; // level 0 may be this own function |
| 369 | } |
| 370 | |
| 371 | if (lua_gettop(L) != arg && !lua_isstring(L, arg+1)) return; // message is not a string |
| 372 | |
| 373 | while (lua_getstack(L1, level++, &entry)) |
| 374 | { |
| 375 | if (level > LEVELS1 && firstpart) { |
| 376 | // no more than `LEVELS2' more levels? |
| 377 | if (!lua_getstack(L1, level+LEVELS2, &entry)) { |
| 378 | level--; // keep going |
| 379 | } else { |
| 380 | lua_pushliteral(L, "\n\t..."); // too many levels |
| 381 | while (lua_getstack(L1, level+LEVELS2, &entry)) // find last levels |
| 382 | level++; |
| 383 | } |
| 384 | firstpart = 0; |
| 385 | continue; |
| 386 | } |
| 387 | int status = lua_getinfo(L1, infostring, &entry); |
| 388 | if (!status) |
| 389 | continue; |
| 390 | |
| 391 | cbk(L1, &entry, ctx); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | // From ldblib.c function db_errorfb() |
| 396 | uint32_t WriteLuaTracebackEntry(lua_Debug* entry, char* buffer, uint32_t buffer_size) |
no test coverage detected