From a4e677466a0b9ba9025ec0b37e8d2ba9921ede70 Mon Sep 17 00:00:00 2001 From: "M. Dietrich" Date: Mon, 7 Jun 2021 16:44:39 +0200 Subject: [PATCH] fix super_call to call in the next higher class --- class.lua | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/class.lua b/class.lua index d9dc1bc..ef6a8b6 100644 --- a/class.lua +++ b/class.lua @@ -59,27 +59,24 @@ local function class(inherits) error("super_call requires a string function name to look up, got "..tostring(func_name)) end --todo: memoize the below :) - local first_impl = c + local previous_impl = c:super() --find the first superclass that actually has the method - while first_impl and not rawget(first_impl, func_name) do - first_impl = first_impl:super() + while previous_impl and not rawget(previous_impl, func_name) do + previous_impl = previous_impl:super() 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) end - --get the superclass of that - local super = first_impl:super() - if not super then - error("failed super call - no superclass to call from") - end - - local f = super[func_name] - if not f then + -- get the function + local f = previous_impl[func_name] + if not f then -- this should never happen because we bail out earlier error("failed super call - missing function "..func_name.." in superclass") end + -- check if someone reuses that reference 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!") end + -- call that function return f(self, ...) end