Skip to content

Commit

Permalink
Fix debug.getinfo source info for native functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Jan 21, 2024
1 parent 937ee1c commit 6df1e87
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/main/java/org/squiddev/cobalt/lib/DebugLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public final class DebugLib {
private static final LuaString MAIN = valueOf("main");
private static final LuaString LUA = valueOf("Lua");
private static final LuaString C = valueOf("C");
private static final LuaString C_SOURCE = valueOf("[C]");
private static final LuaString C_SHORT_SOURCE = valueOf("[C]");
private static final LuaString C_SOURCE = valueOf("=[C]");
public static final LuaString QMARK = valueOf("?");
private static final LuaString EXTERNAL_HOOK = valueOf("external hook");

Expand Down Expand Up @@ -191,11 +192,9 @@ private static Varargs getinfo(LuaState state, Varargs args) throws LuaError {
info.rawset(LINEDEFINED, valueOf(p.lineDefined));
info.rawset(LASTLINEDEFINED, valueOf(p.lastLineDefined));
} else {
String shortName = function == null ? "nil" : function.debugName();
LuaString name = valueOf("[C] " + shortName);
info.rawset(WHAT, C);
info.rawset(SOURCE, name);
info.rawset(SHORT_SRC, C_SOURCE);
info.rawset(SOURCE, C_SOURCE);
info.rawset(SHORT_SRC, C_SHORT_SOURCE);
info.rawset(LINEDEFINED, MINUSONE);
info.rawset(LASTLINEDEFINED, MINUSONE);
}
Expand Down
17 changes: 16 additions & 1 deletion src/test/resources/spec/_prelude.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
-- busted.


local function key_compare(a, b)
local ta, tb = type(a), type(b)

if ta == "string" then return tb ~= "string" or a < b
elseif tb == "string" then return false
end

if ta == "number" then return tb ~= "number" or a < b end
return false
end

local function serialise(value, seen, indent)
local ty = type(value)
if ty == "string" then return (("%q"):format(value):gsub("\\n", "n"))
Expand All @@ -26,9 +37,13 @@ local function serialise(value, seen, indent)
if item:find("\n") then contents_len = math.huge end
end

table.sort(keys, key_compare)

local contents_len = 0
for i = 1, keysn - 1 do
local k, v, item = keys[i], value[k]
local k = keys[i]
local v = value[k]
local item
if type(k) == "string" and k:match("^[%a_][%w_]*$") then
item = ("%s = %s"):format(k, serialise(v, seen, indent + 1))
else
Expand Down
19 changes: 19 additions & 0 deletions src/test/resources/spec/debug_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,23 @@ describe("The debug library", function()
expect(counts.line):eq(59)
end)
end)

describe("debug.getinfo", function()
it("a native function :lua>=5.3", function()
expect(debug.getinfo(print)):matches {
nups = 0,
currentline = -1,
func = print,
istailcall = false,
isvararg = true,
lastlinedefined = -1,
linedefined = -1,
namewhat = "",
nparams = 0,
short_src = "[C]",
source = "=[C]",
what = "C",
}
end)
end)
end)

0 comments on commit 6df1e87

Please sign in to comment.