From 50c7c915ee2fa239043d5456237f5145d064089b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 19 Nov 2024 14:09:18 -0300 Subject: 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. --- testes/calls.lua | 11 +++++++++++ testes/db.lua | 3 +++ testes/errors.lua | 25 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+) (limited to 'testes') diff --git a/testes/calls.lua b/testes/calls.lua index 12312d60..31028215 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -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 diff --git a/testes/db.lua b/testes/db.lua index 49ff8e3e..fc0db9ea 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -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 diff --git a/testes/errors.lua b/testes/errors.lua index 80d91a92..0925fe58 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -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 -- cgit v1.2.3-55-g6feb