mirror of
https://github.com/kikito/middleclass.git
synced 2024-11-25 02:44:20 +00:00
Merge pull request #58 from jojo59516/optimize
This commit is contained in:
commit
aa8f88b317
@ -31,14 +31,22 @@ local middleclass = {
|
|||||||
local function _createIndexWrapper(aClass, f)
|
local function _createIndexWrapper(aClass, f)
|
||||||
if f == nil then
|
if f == nil then
|
||||||
return aClass.__instanceDict
|
return aClass.__instanceDict
|
||||||
else
|
elseif type(f) == "function" then
|
||||||
return function(self, name)
|
return function(self, name)
|
||||||
local value = aClass.__instanceDict[name]
|
local value = aClass.__instanceDict[name]
|
||||||
|
|
||||||
if value ~= nil then
|
if value ~= nil then
|
||||||
return value
|
return value
|
||||||
elseif type(f) == "function" then
|
else
|
||||||
return (f(self, name))
|
return (f(self, name))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else -- if type(f) == "table" then
|
||||||
|
return function(self, name)
|
||||||
|
local value = aClass.__instanceDict[name]
|
||||||
|
|
||||||
|
if value ~= nil then
|
||||||
|
return value
|
||||||
else
|
else
|
||||||
return f[name]
|
return f[name]
|
||||||
end
|
end
|
||||||
@ -147,7 +155,9 @@ local DefaultMixin = {
|
|||||||
local subclass = _createClass(name, self)
|
local subclass = _createClass(name, self)
|
||||||
|
|
||||||
for methodName, f in pairs(self.__instanceDict) do
|
for methodName, f in pairs(self.__instanceDict) do
|
||||||
_propagateInstanceMethod(subclass, methodName, f)
|
if not (methodName == "__index" and type(f) == "table") then
|
||||||
|
_propagateInstanceMethod(subclass, methodName, f)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
subclass.initialize = function(instance, ...) return self.initialize(instance, ...) end
|
subclass.initialize = function(instance, ...) return self.initialize(instance, ...) end
|
||||||
|
|
||||||
|
@ -100,6 +100,10 @@ describe('Metamethods', function()
|
|||||||
for k in pairs(v) do assert.not_table(k) end
|
for k in pairs(v) do assert.not_table(k) end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('instance should have a table __index if not overrided', function()
|
||||||
|
assert.is_true(type(debug.getmetatable(v).__index) == 'table')
|
||||||
|
end)
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
it('implements __index', function()
|
it('implements __index', function()
|
||||||
assert.equal(b[1], 3)
|
assert.equal(b[1], 3)
|
||||||
@ -170,6 +174,10 @@ describe('Metamethods', function()
|
|||||||
for k in pairs(v2) do assert.not_table(k) end
|
for k in pairs(v2) do assert.not_table(k) end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('instance should also have a table __index if not overrided', function()
|
||||||
|
assert.is_true(type(debug.getmetatable(v2).__index) == 'table')
|
||||||
|
end)
|
||||||
|
|
||||||
it('allows inheriting further', function()
|
it('allows inheriting further', function()
|
||||||
local Vector3 = class('Vector3', Vector2)
|
local Vector3 = class('Vector3', Vector2)
|
||||||
local v3 = Vector3(1,2,3)
|
local v3 = Vector3(1,2,3)
|
||||||
|
Loading…
Reference in New Issue
Block a user