big module refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-19 11:18:31 +02:00
parent 337bc19578
commit 667990f7d8
19 changed files with 157 additions and 192 deletions
+30 -44
View File
@@ -1,28 +1,16 @@
local band, bor, bnot, lshift = bit.band, bit.bor, bit.bnot, bit.lshift
local _G = getfenv(0)
local FIELD, ACCESSOR, MUTABLE, PUBLIC = 1, 2, 4, 8
local mask, _G = bit.band, getfenv(0)
local DECLARED, ACCESSOR, MUTABLE, PUBLIC = 1, 2, 4, 8
local ACCESSOR_KEY, MUTABLE_KEY, PUBLIC_KEY = 'accessor', 'mutable', 'public'
local MODIFIER = {[ACCESSOR_KEY]=ACCESSOR, [MUTABLE_KEY]=MUTABLE, [PUBLIC_KEY]=PUBLIC}
local _state, _data, _metadata = {}, {}, {}
--local function_mt = {
-- __call = function(self, ...)
-- local f = state[self]
-- getfenv(f).__ = {}
-- f(unpack(arg))
-- getfenv(f).__ = temp
-- end,
--}
local PROPERTY = {[ACCESSOR_KEY]=ACCESSOR, [MUTABLE_KEY]=MUTABLE, [PUBLIC_KEY]=PUBLIC}
local _data, _metadata, _modifier_properties = {}, {}, {}
local metadata_mt = {__index=function() return 0 end}
local environment_mt = {
local env_mt = {
__metatable = false,
__index = function(self, key)
local value, properties = _data[self][key], _metadata[self][key]
if band(FIELD, properties) ~= 0 then
if mask(DECLARED+ACCESSOR, properties) == DECLARED then
return value
elseif band(ACCESSOR, properties) ~= 0 then
elseif mask(ACCESSOR, properties) == ACCESSOR then
return value(key)
else
return _G[key] or error('No key "'..key..'".', 2)
@@ -31,8 +19,8 @@ local environment_mt = {
__newindex = function(self, key, value)
local properties = _metadata[self][key]
if properties == 0 then
_metadata[self][key] = FIELD
-- elseif band(MUTABLE, properties) == 0 then
_metadata[self][key] = DECLARED
-- elseif mask(MUTABLE, properties) == 0 then
-- error('"'..key..'" is immutable.', 2)
end
_data[self][key] = value
@@ -42,48 +30,46 @@ local interface_mt = {
__metatable = false,
__index = function(self, key)
local value, properties = _data[self][key], _metadata[self][key]
if band(FIELD+PUBLIC, properties) == FIELD+PUBLIC then
if mask(ACCESSOR+PUBLIC, properties) == PUBLIC then
return value
elseif band(ACCESSOR+PUBLIC, properties) == ACCESSOR+PUBLIC then
return value(key)
elseif mask(PUBLIC, properties) == PUBLIC then
return value()
else
error('No key "'..key..'".', 2)
end
end,
__newindex = function() error('Unsupported operation.', 2) end,
__newindex = function(self, key, value) self[key](value) end,
}
local modifier_mt = {
__metatable = false,
__call = function(self, key)
_state[self] = bor(_state[self], MODIFIER[key])
return self
end,
__call = function() end,
__index = function(self, key)
local value = MODIFIER[key]
if not value then error('Unsupported operation.', 2) end
_state[self] = bor(_state[self], value)
local property = PROPERTY[key]
if not property then error('Unsupported modifier "'..key..'".', 2) end
if mask(property, _modifier_properties[self]) ~= 0 then error('Duplicate modifier "'..key..'".', 2) end
_modifier_properties[self] = _modifier_properties[self] + property
return self
end,
__newindex = function(self, key, value)
if _metadata[self][key] ~= 0 then error('Duplicate key "'..key..'".', 2) end
_data[self][key] = value
_metadata[self][key] = bor(band(FIELD, bnot(lshift(_state[self], 1))), _state[self])
_state[self] = 0
_metadata[self][key] = _modifier_properties[self]
end,
}
function _G.aux_module()
local modifier, environment, interface = setmetatable({}, modifier_mt), setmetatable({}, environment_mt), setmetatable({}, interface_mt)
local data = {_G=_G, [ACCESSOR_KEY]=modifier, [MUTABLE_KEY]=modifier, [PUBLIC_KEY]=modifier}
local metadata = setmetatable({_G=FIELD, [ACCESSOR_KEY]=ACCESSOR, [MUTABLE_KEY]=ACCESSOR, [PUBLIC_KEY]=ACCESSOR}, metadata_mt)
local data, metadata, modifier, env, interface
modifier = setmetatable({}, modifier_mt) env = setmetatable({}, env_mt) interface = setmetatable({}, interface_mt)
local function modifier_accessor(key) _modifier_properties[modifier] = DECLARED+PROPERTY[key] return modifier end
data = {_G=_G, [ACCESSOR_KEY]=modifier_accessor, [MUTABLE_KEY]=modifier_accessor, [PUBLIC_KEY]=modifier_accessor}
metadata = setmetatable({_G=DECLARED, [ACCESSOR_KEY]=ACCESSOR, [MUTABLE_KEY]=ACCESSOR, [PUBLIC_KEY]=ACCESSOR}, metadata_mt)
_data[modifier], _data[environment], _data[interface] = data, data, data
_metadata[modifier], _metadata[environment], _metadata[interface] = metadata, metadata, metadata
_state[modifier] = 0
_data[modifier], _data[env], _data[interface] = data, data, data
_metadata[modifier], _metadata[env], _metadata[interface] = metadata, metadata, metadata
environment.mutable.__ = nil
environment.m = environment -- TODO for compatibility, remove later
environment.private = environment -- TODO for compatibility, remove later
env.mutable.__ = nil
env.m = env -- TODO for compatibility, remove later
env.private = env -- TODO for compatibility, remove later
setfenv(2, environment)
setfenv(2, env)
return interface
end