Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Object:extend now works as expected.

Object:extend previously failed for metamethods defined on table.meta.  These methods are now copied into the new objects meta where they will be set into the metatable.  A new test case has been added to verify this behavior.
  • Loading branch information...
commit ccf5454186e116cb73a5cd380d0e91ceddc70167 1 parent e250680
@kans kans authored
Showing with 20 additions and 2 deletions.
  1. +10 −1 lib/luvit/core.lua
  2. +10 −1 tests/test-object.lua
View
11 lib/luvit/core.lua
@@ -112,9 +112,18 @@ Creates a new sub-class.
self.h = h
end
]]
+
function Object:extend()
local obj = self:create()
- obj.meta = {__index = obj, super = self}
+ local meta = {}
+ -- move the meta methods defined in our ancestors meta into our own
+ --to preserve expected behavior in children (like __tostring, __add, etc)
+ for k, v in pairs(self.meta) do
+ meta[k] = v
+ end
+ meta.__index = obj
+ meta.super=self
+ obj.meta = meta
return obj
end
View
11 tests/test-object.lua
@@ -26,9 +26,18 @@ local Foo = require('core').Object:extend()
function Foo:initialize(bar)
self.bar = bar
end
+function Foo.meta.__tostring(table)
+ return tostring(table.bar)
+end
+
+local Baz = Foo:extend()
local foo1 = Foo:new(1)
local foo2 = Foo:new(1)
assert(foo1 ~= foo2)
-assert(tostring(foo1) ~= tostring(foo2))
+assert(tostring(foo1) == tostring(foo2))
assert(foo1.bar == foo2.bar)
+
+local msg = 'asd'
+local baz1 = Baz:new(msg)
+assert(tostring(baz1) == msg)
Please sign in to comment.
Something went wrong with that request. Please try again.