'__call' metamethod can be any callable object

Removed the restriction that a '__call' metamethod must be an actual
function.
This commit is contained in:
Roberto Ierusalimschy
2019-06-25 17:45:50 -03:00
parent 4487c28ced
commit c1a63c45f8
2 changed files with 31 additions and 14 deletions

View File

@@ -151,6 +151,23 @@ end
print('+')
do -- testing chains of '__call'
local N = 20
local u = table.pack
for i = 1, N do
u = setmetatable({i}, {__call = u})
end
local Res = u("a", "b", "c")
assert(Res.n == N + 3)
for i = 1, N do
assert(Res[i][1] == i)
end
assert(Res[N + 1] == "a" and Res[N + 2] == "b" and Res[N + 3] == "c")
end
a = nil
(function (x) a=x end)(23)
assert(a == 23 and (function (x) return x*2 end)(20) == 40)