Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 34 additions & 27 deletions src/Modules/Common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Loading