mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-03 15:38:22 +00:00
Include variables in redbean lua traceback
This commit is contained in:
parent
be7c5e1071
commit
933f33bcc1
4 changed files with 59 additions and 13 deletions
46
third_party/lua/lauxlib.c
vendored
46
third_party/lua/lauxlib.c
vendored
|
@ -190,6 +190,52 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
|
|||
luaL_pushresult(&b);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Improved Lua traceback.
|
||||
* @see https://luyuhuang.tech/2020/12/01/lua-traceback-with-parameters.html
|
||||
* @author Luyu Huang
|
||||
*/
|
||||
LUALIB_API void luaL_traceback2(lua_State *L, lua_State *L1, const char *msg,
|
||||
int level) {
|
||||
lua_Debug ar;
|
||||
int top = lua_gettop(L);
|
||||
int last = lastlevel(L1);
|
||||
int n1 = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1;
|
||||
if (msg) lua_pushfstring(L, "%s\n", msg);
|
||||
luaL_checkstack(L, 10, NULL);
|
||||
lua_pushliteral(L, "stack traceback:");
|
||||
while (lua_getstack(L1, level++, &ar)) {
|
||||
if (n1-- == 0) { /* too many levels? */
|
||||
lua_pushliteral(L, "\n\t..."); /* add a '...' */
|
||||
level = last - LEVELS2 + 1; /* and skip to last ones */
|
||||
} else {
|
||||
lua_getinfo(L1, "Slntu", &ar);
|
||||
lua_pushfstring(L, "\n\t%s:", ar.short_src);
|
||||
if (ar.currentline > 0)
|
||||
lua_pushfstring(L, "%d:", ar.currentline);
|
||||
lua_pushliteral(L, " in ");
|
||||
pushfuncname(L, &ar);
|
||||
if (ar.nparams > 0)
|
||||
lua_pushliteral(L, ", params:");
|
||||
for (int i = 1; i <= ar.nparams; ++i) {
|
||||
const char *name = lua_getlocal(L1, &ar, i);
|
||||
if (name) {
|
||||
lua_xmove(L1, L, 1); // -3
|
||||
const char *val = luaL_tolstring(L, -1, NULL); // -2
|
||||
lua_pushfstring(L, " %s = %s;", name, val); // -1
|
||||
lua_insert(L, -3);
|
||||
lua_pop(L, 2);
|
||||
}
|
||||
}
|
||||
if (ar.istailcall) lua_pushliteral(L, "\n\t(...tail calls...)");
|
||||
lua_concat(L, lua_gettop(L) - top);
|
||||
}
|
||||
}
|
||||
lua_concat(L, lua_gettop(L) - top);
|
||||
}
|
||||
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
|
14
third_party/lua/lauxlib.h
vendored
14
third_party/lua/lauxlib.h
vendored
|
@ -1,16 +1,9 @@
|
|||
/*
|
||||
** $Id: lauxlib.h $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef lauxlib_h
|
||||
#define lauxlib_h
|
||||
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "third_party/lua/luaconf.h"
|
||||
#include "third_party/lua/lua.h"
|
||||
#include "third_party/lua/luaconf.h"
|
||||
/* clang-format off */
|
||||
|
||||
/* global table */
|
||||
#define LUA_GNAME "_G"
|
||||
|
@ -110,6 +103,9 @@ LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname);
|
|||
LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1,
|
||||
const char *msg, int level);
|
||||
|
||||
LUALIB_API void (luaL_traceback2) (lua_State *L, lua_State *L1,
|
||||
const char *msg, int level);
|
||||
|
||||
LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
|
||||
lua_CFunction openf, int glb);
|
||||
|
||||
|
|
2
third_party/lua/luacallwithtrace.c
vendored
2
third_party/lua/luacallwithtrace.c
vendored
|
@ -50,7 +50,7 @@ int LuaCallWithTrace(lua_State *L, int nargs, int nres, lua_State *C) {
|
|||
// move the error message
|
||||
lua_xmove(C, L, 1);
|
||||
// replace the error with the traceback on failure
|
||||
luaL_traceback(L, C, lua_tostring(L, -1), 0);
|
||||
luaL_traceback2(L, C, lua_tostring(L, -1), 0);
|
||||
lua_remove(L, -2); // remove the error message
|
||||
} else {
|
||||
if (!lua_checkstack(L, MAX(nresults, nres))) {
|
||||
|
|
|
@ -1,16 +1,18 @@
|
|||
globalvar1 = 31337
|
||||
|
||||
globalvar2 = {
|
||||
'hello': 'world',
|
||||
hello = 'world',
|
||||
}
|
||||
|
||||
function dosomething(param1, x)
|
||||
local res, y = 0
|
||||
local res
|
||||
local y
|
||||
y = 0
|
||||
SetStatus(200)
|
||||
SetHeader('Content-Type', 'text/plain; charset=utf-8')
|
||||
Write('preprae to crash... now\r\n')
|
||||
res = x / y
|
||||
Write(string.format('42 / 0 is %d\r\n', res)
|
||||
Write(string.format('42 / 0 is %d\r\n', res))
|
||||
end
|
||||
|
||||
function start(param1)
|
||||
|
@ -22,3 +24,5 @@ function main()
|
|||
local s = 'hello'
|
||||
start(s)
|
||||
end
|
||||
|
||||
main()
|
||||
|
|
Loading…
Add table
Reference in a new issue