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)
|
||||
if f == nil then
|
||||
return aClass.__instanceDict
|
||||
else
|
||||
elseif type(f) == "function" then
|
||||
return function(self, name)
|
||||
local value = aClass.__instanceDict[name]
|
||||
|
||||
if value ~= nil then
|
||||
return value
|
||||
elseif type(f) == "function" then
|
||||
else
|
||||
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
|
||||
return f[name]
|
||||
end
|
||||
@ -147,8 +155,10 @@ local DefaultMixin = {
|
||||
local subclass = _createClass(name, self)
|
||||
|
||||
for methodName, f in pairs(self.__instanceDict) do
|
||||
if not (methodName == "__index" and type(f) == "table") then
|
||||
_propagateInstanceMethod(subclass, methodName, f)
|
||||
end
|
||||
end
|
||||
subclass.initialize = function(instance, ...) return self.initialize(instance, ...) end
|
||||
|
||||
self.subclasses[subclass] = true
|
||||
|
@ -100,6 +100,10 @@ describe('Metamethods', function()
|
||||
for k in pairs(v) do assert.not_table(k) 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()
|
||||
assert.equal(b[1], 3)
|
||||
@ -170,6 +174,10 @@ describe('Metamethods', function()
|
||||
for k in pairs(v2) do assert.not_table(k) 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()
|
||||
local Vector3 = class('Vector3', Vector2)
|
||||
local v3 = Vector3(1,2,3)
|
||||
|
Loading…
Reference in New Issue
Block a user