Do not wrap __newindex

This commit is contained in:
mpeterv 2015-11-28 18:10:35 +03:00
parent 7574380519
commit ab1699bb05
2 changed files with 5 additions and 28 deletions

View File

@ -67,25 +67,9 @@ local function _createIndexWrapper(aClass, f)
end end
end end
local function _createNewIndexWrapper(aClass, f)
if f ~= nil then
return function(self, key, value)
if aClass.__instanceDict[key] ~= nil then
rawset(self, key, value)
elseif type(f) == "function" then
f(self, key, value)
else
f[key] = value
end
end
end
end
local function _setMetamethod(aClass, name, f) local function _setMetamethod(aClass, name, f)
if name == "__index" then if name == "__index" then
f = _createIndexWrapper(aClass, f) f = _createIndexWrapper(aClass, f)
elseif name == "__newindex" then
f = _createNewIndexWrapper(aClass, f)
end end
aClass.__instanceMeta[name] = f aClass.__instanceMeta[name] = f
@ -125,15 +109,8 @@ local function _setClassMetatable(aClass)
end end
local function _createClass(name, super) local function _createClass(name, super)
local aClass = { local aClass = { name = name, super = super, static = {}, __mixins = {}, __instanceDict = {}, __instanceMeta = {} }
name = name, aClass.subclasses = setmetatable({}, {__mode = "k"})
super = super,
static = {},
subclasses = setmetatable({}, {__mode = "k"}),
__mixins = {},
__instanceDict = {},
__instanceMeta = {}
}
_setClassDictionariesMetatables(aClass) _setClassDictionariesMetatables(aClass)
_setClassMetatable(aClass) _setClassMetatable(aClass)

View File

@ -229,11 +229,11 @@ describe('Metamethods', function()
assert.equal(fallback.key, 'value') assert.equal(fallback.key, 'value')
end) end)
it('does not use __newindex when field exists in class', function() it('uses __newindex when field exists in class', function()
p.common = 'value' p.common = 'value'
assert.equal(p.common, 'value') assert.equal(p.common, 'class')
assert.equal(Proxy.common, 'class') assert.equal(Proxy.common, 'class')
assert.equal(fallback.common, 'fallback') assert.equal(fallback.common, 'value')
end) end)
end) end)