[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: debug.traceback() in Lua?
- From: Eduardo Ochs <eduardoochs@...>
- Date: Tue, 22 Mar 2022 03:21:16 -0300
Hi list,
where can I find a version in Lua of debug.traceback()?
I know that there is one in Fennel... its original code -
in Fennel - is here,
https://git.sr.ht/~technomancy/fennel/tree/main/item/src/fennel/compiler.fnl#L827-873
and we can obtain its translation to Lua by running this:
rm -Rfv /tmp/fennel/
cd /tmp/
git clone https://git.sr.ht/~technomancy/fennel
cd /tmp/fennel/
make -f Makefile LUA_VERSION=5.2
awk '2902<=NR && NR<=2963' bootstrap/fennel.lua | tee tb.lua
After running the code above the file /tmp/fennel/tb.lua
will contain this:
local function traceback_frame(info)
if ((info.what == "C") and info.name) then
return string.format(" [C]: in function '%s'", info.name)
elseif (info.what == "C") then
return " [C]: in ?"
else
local remap = sourcemap[info.source]
if (remap and remap[info.currentline]) then
if remap[info.currentline][1] then
info.short_src = sourcemap[("@" ..
remap[info.currentline][1])].short_src
else
info.short_src = remap.short_src
end
info.currentline = (remap[info.currentline][2] or -1)
else
end
if (info.what == "Lua") then
local function _325_()
if info.name then
return ("'" .. info.name .. "'")
else
return "?"
end
end
return string.format(" %s:%d: in function %s",
info.short_src, info.currentline, _325_())
elseif (info.short_src == "(tail call)") then
return " (tail call)"
else
return string.format(" %s:%d: in main chunk", info.short_src,
info.currentline)
end
end
end
local function traceback(msg, start)
local msg0 = tostring((msg or ""))
if ((msg0:find("^Compile error") or msg0:find("^Parse error")) and
not utils["debug-on?"]("trace")) then
return msg0
else
local lines = {}
if (msg0:find(":%d+: Compile error") or msg0:find(":%d+: Parse
error")) then
table.insert(lines, msg0)
else
local newmsg = msg0:gsub("^[^:]*:%d+:%s+", "runtime error: ")
table.insert(lines, newmsg)
end
table.insert(lines, "stack traceback:")
local done_3f, level = false, (start or 2)
while not done_3f do
do
local _329_ = debug.getinfo(level, "Sln")
if (_329_ == nil) then
done_3f = true
elseif (nil ~= _329_) then
local info = _329_
table.insert(lines, traceback_frame(info))
else
end
end
level = (level + 1)
end
return table.concat(lines, "\n")
end
end
This seems to be loosely based on the function db_errorfb in the file
src/ldblib.c of the Lua source, but with several changes... I can try
to modify that code to make it more similar to db_errorfb, but I am
not a good programmer, and that would probably take many hours - and I
think that having other "debug.tracebacks()"s in Lua to compare would
save me a lot of time...
Thanks in advance!
Eduardo Ochs
http://angg.twu.net/#eev
http://angg.twu.net/dednat6.html
http://angg.twu.net/dednat6/tug-slides.pdf