elua: completely revamped object system
authorDaniel Kolesa <d.kolesa@samsung.com>
Wed, 25 Feb 2015 15:45:18 +0000 (15:45 +0000)
committerDaniel Kolesa <d.kolesa@samsung.com>
Wed, 25 Feb 2015 15:45:47 +0000 (15:45 +0000)
These changes now allow us to do proper __tostring overrides and require
fewer method references, as everything shares a single metatable.

src/scripts/elua/core/util.lua

index e6d59f4..1ef16fb 100644 (file)
@@ -25,12 +25,26 @@ local proto_lookup = function(protos, 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)
+local Object_MT = {
+    __index = function(self, name)
+        local v = proto_lookup(self.__mixins, name)
+        if v == nil then
+            return proto_lookup(self.__protos, name)
+        end
+    end,
+
+    __tostring = function(self)
+        local  f = self["__tostring"]
+        if not f then
+            return ("Object: %s"):format(self.name or "unnamed")
+        end
+        return f(self)
+    end,
+
+    __call = function(self, ...)
+        return self["__call"](self, ...)
     end
-end
+}
 
 M.Object = {
     __call = function(self, ...)
@@ -41,12 +55,8 @@ M.Object = {
 
     clone = function(self, o)
         o = o or {}
-        o.__index, o.__protos, o.__mixins, o.__call =
-            multi_index, { self }, {}, self.__call
-        if not o.__tostring then
-            o.__tostring = self.__tostring
-        end
-        setmetatable(o, o)
+        o.__protos, o.__mixins = { self }, {}
+        setmetatable(o, Object_MT)
         return o
     end,
 
@@ -69,10 +79,6 @@ M.Object = {
     add_mixin = function(self, mixin)
         local mixins = self.__mixins
         mixins[#mixins + 1] = mixin
-    end,
-
-    __tostring = function(self)
-        return ("Object: %s"):format(self.name or "unnamed")
     end
 }