Debug information about extra arguments from __call

'debug.getinfo' can return number of extra arguments added to a call by
a chain of __call metavalues. That information is being used to improve
error messages about errors in these extra arguments.
This commit is contained in:
Roberto Ierusalimschy
2024-11-19 14:09:18 -03:00
parent b117bdb344
commit 50c7c915ee
9 changed files with 83 additions and 12 deletions

View File

@@ -204,6 +204,17 @@ do print"testing chains of '__call'"
assert(Res[i][1] == i)
end
assert(Res[N + 1] == "a" and Res[N + 2] == "b" and Res[N + 3] == "c")
local function u (...)
local n = debug.getinfo(1, 't').extraargs
assert(select("#", ...) == n)
return n
end
for i = 0, N do
assert(u() == i)
u = setmetatable({}, {__call = u})
end
end

View File

@@ -624,6 +624,9 @@ local function f (x)
end
end
assert(debug.getinfo(print, 't').istailcall == false)
assert(debug.getinfo(print, 't').extraargs == 0)
function g(x) return f(x) end
function g1(x) g(x) end

View File

@@ -117,6 +117,31 @@ else
return 1
]]
assert(string.find(res, "xuxu.-main chunk"))
do -- tests for error messages about extra arguments from __call
local function createobj (n)
-- function that raises an error on its n-th argument
local code = string.format("argerror %d 'msg'", n)
local func = T.makeCfunc(code)
-- create a chain of 2 __call objects
local M = setmetatable({}, {__call = func})
M = setmetatable({}, {__call = M})
-- put it as a method for a new object
return {foo = M}
end
_G.a = createobj(1) -- error in first (extra) argument
checkmessage("a:foo()", "bad extra argument #1")
_G.a = createobj(2) -- error in second (extra) argument
checkmessage("a:foo()", "bad extra argument #2")
_G.a = createobj(3) -- error in self (after two extra arguments)
checkmessage("a:foo()", "bad self")
_G.a = createobj(4) -- error in first regular argument (after self)
checkmessage("a:foo()", "bad argument #1")
end
end