Merge pull request #58 from jojo59516/optimize

This commit is contained in:
Enrique García Cota 2022-01-20 23:31:22 +01:00 committed by GitHub
commit aa8f88b317
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -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,7 +155,9 @@ local DefaultMixin = {
local subclass = _createClass(name, self)
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
subclass.initialize = function(instance, ...) return self.initialize(instance, ...) end

View File

@ -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)