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) 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,8 +155,10 @@ 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
if not (methodName == "__index" and type(f) == "table") then
_propagateInstanceMethod(subclass, methodName, f) _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
self.subclasses[subclass] = true self.subclasses[subclass] = true

View File

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