diff --git a/middleclass.lua b/middleclass.lua index 7cbec64..431d52f 100644 --- a/middleclass.lua +++ b/middleclass.lua @@ -97,9 +97,9 @@ end local Object = _createClass("Object", nil) -Object.static.__metamethods = { '__add', '__call', '__concat', '__div', '__ipairs', '__le', - '__len', '__lt', '__mod', '__mul', '__pairs', '__pow', '__sub', - '__tostring', '__unm'} +Object.static.__metamethods = { '__add', '__band', '__bor', '__bxor', '__bnot', '__call', '__concat', '__div', '__eq', + '__gc', '__ipairs', '__idiv', '__le', '__len', '__lt', '__metatable', '__mod', '__mode', + '__mul', '__pairs', '__pow', '__sh1', '__shr', '__sub', '__tostring', '__unm' } function Object.static:allocate() assert(type(self) == 'table', "Make sure that you are using 'Class:allocate' instead of 'Class.allocate'") diff --git a/spec/metamethods_spec.lua b/spec/metamethods_spec.lua index 93becaa..8b8bb63 100644 --- a/spec/metamethods_spec.lua +++ b/spec/metamethods_spec.lua @@ -5,6 +5,10 @@ local function is_lua_5_2_compatible() return type(rawlen) == 'function' end +local function is_lua_5_3_compatible() + return type(string.unpack) == 'function' +end + describe('Metamethods', function() describe('Custom Metamethods', function() @@ -48,6 +52,10 @@ describe('Metamethods', function() local a = Vector:new(1,2,3) local b = Vector:new(2,4,6) + function Vector.__gc(a) + b.x, b.y, b.z = a.x, a.y, a.z + end + for metamethod,values in pairs({ __tostring = { tostring(a), "Vector[1,2,3]" }, __eq = { a, a}, @@ -60,7 +68,7 @@ describe('Metamethods', function() __concat = { a..b, 28 }, __call = { a(), math.sqrt(14) }, __pow = { a^b, Vector(0,0,0) }, - __mul = { 4*a, Vector(4,8,12) } + __mul = { 4*a, Vector(4,8,12) }, --__index = { b[1], 3 } }) do describe(metamethod, function() @@ -100,6 +108,14 @@ describe('Metamethods', function() end + if is_lua_5_3_compatible() then + describe('__gc', function() + a = nil + collectgarbage() + assert.are.same({b.x, b.y, b.z}, {1,2,3}) + end) + end + describe('Inherited Metamethods', function() local Vector2= class('Vector2', Vector) function Vector2:initialize(x,y,z) Vector.initialize(self,x,y,z) end