Merge pull request #24 from emdete/master

fix super_call to call in the next higher class
This commit is contained in:
Max Cahill 2021-06-08 09:56:35 +10:00 committed by GitHub
commit 2128a38702
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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