From e9f6a37a8f2c1db3f0c56d402eea69c9a20b4384 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Wed, 3 Apr 2024 09:14:08 +0100 Subject: [PATCH] Fix some errors missing source positions 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. --- .../java/org/squiddev/cobalt/debug/DebugHelpers.java | 2 +- src/test/resources/spec/vm_spec.lua | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/squiddev/cobalt/debug/DebugHelpers.java b/src/main/java/org/squiddev/cobalt/debug/DebugHelpers.java index f2eb984b..cdaf69d8 100644 --- a/src/main/java/org/squiddev/cobalt/debug/DebugHelpers.java +++ b/src/main/java/org/squiddev/cobalt/debug/DebugHelpers.java @@ -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(); diff --git a/src/test/resources/spec/vm_spec.lua b/src/test/resources/spec/vm_spec.lua index 4b9a8fa2..a602336f 100644 --- a/src/test/resources/spec/vm_spec.lua +++ b/src/test/resources/spec/vm_spec.lua @@ -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)