fix super_call to call in the next higher class

This commit is contained in:
M. Dietrich 2021-06-07 16:44:39 +02:00
parent b3bae9b0f2
commit a4e677466a

View File

@ -59,27 +59,24 @@ local function class(inherits)
error("super_call requires a string function name to look up, got "..tostring(func_name)) error("super_call requires a string function name to look up, got "..tostring(func_name))
end end
--todo: memoize the below :) --todo: memoize the below :)
local first_impl = c local previous_impl = c:super()
--find the first superclass that actually has the method --find the first superclass that actually has the method
while first_impl and not rawget(first_impl, func_name) do while previous_impl and not rawget(previous_impl, func_name) do
first_impl = first_impl:super() previous_impl = previous_impl:super()
end end
if not first_impl then if not previous_impl then
error("failed super call - no superclass in the chain has an implementation of "..func_name) error("failed super call - no superclass in the chain has an implementation of "..func_name)
end end
--get the superclass of that -- get the function
local super = first_impl:super() local f = previous_impl[func_name]
if not super then if not f then -- this should never happen because we bail out earlier
error("failed super call - no superclass to call from")
end
local f = super[func_name]
if not f then
error("failed super call - missing function "..func_name.." in superclass") error("failed super call - missing function "..func_name.." in superclass")
end end
-- check if someone reuses that reference
if f == self[func_name] then if f == self[func_name] then
error("failed super call - function "..func_name.." is same in superclass as in derived; this will be a infinite recursion!") error("failed super call - function "..func_name.." is same in superclass as in derived; this will be a infinite recursion!")
end end
-- call that function
return f(self, ...) return f(self, ...)
end end