diff --git a/src/Modules/Common.lua b/src/Modules/Common.lua index 6fd1ec57a9..d0db40e98b 100644 --- a/src/Modules/Common.lua +++ b/src/Modules/Common.lua @@ -98,20 +98,41 @@ function newClass(className, ...) class._superParents = { } addSuperParents(class, class) -- Set up inheritance - setmetatable(class, { - __index = function(self, key) - for _, parent in ipairs(class._parents) do - local val = parent[key] - if val ~= nil then - self[key] = val - return val - end + for _, parent in ipairs(class._parents) do + for k, v in pairs(parent) do + if class[k] == nil then + class[k] = v end end - }) + end end return class end + +local parentCall = function(proxy, ...) + local parent = proxy._parent + local object = proxy._object + + if not parent._constructor then + error("Parent class '"..parent._className.."' has no constructor") + end + if object._parentInit[parent] then + error("Parent class '"..parent._className.."' has already been initialised") + end + + parent._constructor(object, ...) + object._parentInit[parent] = true +end + +local parentIndex = function(self, key) + local v = rawget(self._object, key) + if v ~= nil then + return v + else + return self._parent[key] + end +end + function new(className, ...) local class = getClass(className) local object = setmetatable({ }, class) @@ -121,25 +142,11 @@ function new(className, ...) object._parentInit = { } for parent in pairs(class._superParents) do local proxyMeta = { - __index = function(self, key) - local v = rawget(object, key) - if v ~= nil then - return v - else - return parent[key] - end - end, + _parent = parent, + _object = object, + __index = parentIndex, __newindex = object, - __call = function(...) - if not parent._constructor then - error("Parent class '"..parent._className.."' of class '"..class._className.."' has no constructor") - end - if object._parentInit[parent] then - error("Parent class '"..parent._className.."' of class '"..class._className.."' has already been initialised") - end - parent._constructor(...) - object._parentInit[parent] = true - end, + __call = parentCall, } object[parent._className] = setmetatable(proxyMeta, proxyMeta) end