Skip to content

Commit

Permalink
Fix some errors missing source positions
Browse files Browse the repository at this point in the history
There was an off-by-one error in DebugHelpers.fileLine which meant we
never checked the first frame in the stack. This meant if we only had a
single Lua function on the stack (such as when in a fresh coroutine),
the error would not contain any line information.
  • Loading branch information
SquidDev committed Apr 3, 2024
1 parent 684e2ff commit e9f6a37
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/org/squiddev/cobalt/debug/DebugHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static Buffer traceback(Buffer sb, LuaThread thread, int level) {
public static String fileLine(LuaThread thread) {
DebugState ds = thread.getDebugState();
DebugFrame di;
for (int i = 0, n = ds.top; i < n; i++) {
for (int i = 0, n = ds.top; i <= n; i++) {
di = ds.getFrame(i);
if (di != null && di.closure != null) {
return di.sourceLine();
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/spec/vm_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,16 @@ describe("The Lua VM", function()
expect("bar!"):eq(varA.hello)
end)
end)

describe("error positions", function()
it("includes positions when there is a single frame", function()
local function f() string.gsub(nil) end
local ok, err = coroutine.resume(coroutine.create(f))

local info = debug.getinfo(f, "S")
local prefix = info.short_src .. ":" .. info.linedefined .. ": bad argument"
expect(ok):eq(false)
expect(err:sub(1, #prefix)):eq(prefix)
end)
end)
end)

0 comments on commit e9f6a37

Please sign in to comment.