elua: add object system mixin support without interfering with parents
authorDaniel Kolesa <d.kolesa@samsung.com>
Wed, 25 Feb 2015 15:06:10 +0000 (15:06 +0000)
committerDaniel Kolesa <d.kolesa@samsung.com>
Wed, 25 Feb 2015 15:06:10 +0000 (15:06 +0000)
src/scripts/elua/core/util.lua

index 7b7120f..841ceb0 100644 (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)