From 4ecedc519866cdb56fc5d566cea17deb3401a01f Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Wed, 25 Feb 2015 15:06:10 +0000 Subject: [PATCH] elua: add object system mixin support without interfering with parents --- src/scripts/elua/core/util.lua | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/scripts/elua/core/util.lua b/src/scripts/elua/core/util.lua index 7b7120f..841ceb0 100644 --- a/src/scripts/elua/core/util.lua +++ b/src/scripts/elua/core/util.lua @@ -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) -- 2.7.4