elua: add object system mixin support without interfering with parents

This commit is contained in:
Daniel Kolesa 2015-02-25 15:06:10 +00:00
parent 9593a720c1
commit 4ecedc5198
1 changed files with 15 additions and 4 deletions

View File

@ -14,8 +14,7 @@ local M = {}
local getmetatable, setmetatable = getmetatable, setmetatable
-- multiple inheritance index with depth-first search
local multi_index = function(self, name)
local protos = self.__protos
local proto_lookup = function(protos, name)
for i = 1, #protos do
local proto = protos[i]
local v = proto[name]
@ -25,6 +24,13 @@ local multi_index = function(self, name)
end
end
local multi_index = function(self, name)
local v = proto_lookup(self.__mixins, name)
if v == nil then
return proto_lookup(self.__protos, name)
end
end
M.Object = {
__call = function(self, ...)
local r = self:clone()
@ -65,8 +71,13 @@ M.Object = {
end
end,
mixin = function(self, obj)
for k, v in pairs(obj) do self[k] = v end
add_mixin = function(self, mixin)
local mixins = self.__mixins
if mixins then
mixins[#mixins + 1] = mixin
else
self.__mixins = { mixin }
end
end,
__tostring = function(self)