big module refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-18 12:43:59 +02:00
parent 6796fc1784
commit 63c317a3ff
3 changed files with 70 additions and 52 deletions
+2 -2
View File
@@ -23,7 +23,7 @@ function m.LOAD()
) )
end end
local orig = SetItemRef local orig = SetItemRef
SetItemRef = function(...) setglobal('SetItemRef', function(...)
local result = orig(unpack(arg)) local result = orig(unpack(arg))
local name, _, quality = GetItemInfo(arg[1]) local name, _, quality = GetItemInfo(arg[1])
if not IsShiftKeyDown() and not IsControlKeyDown() and name then if not IsShiftKeyDown() and not IsControlKeyDown() and name then
@@ -32,7 +32,7 @@ function m.LOAD()
m.extend_tooltip(ItemRefTooltip, link, 1) m.extend_tooltip(ItemRefTooltip, link, 1)
end end
return result return result
end end)
local orig = GameTooltip:GetScript('OnTooltipAddMoney') local orig = GameTooltip:GetScript('OnTooltipAddMoney')
GameTooltip:SetScript('OnTooltipAddMoney', function(...) GameTooltip:SetScript('OnTooltipAddMoney', function(...)
if m.hooked_setter then if m.hooked_setter then
+5 -7
View File
@@ -25,16 +25,14 @@ do
end end
do do
local data = {} local data = setmetatable({}, {__mode='kv'})
local mt = { local __index = function(self, key)
__index = function(self, key) return data[self].handler({public=self, private=data[self].state}, key)
return data[self].handler({public=self, private=data[self].state}, key) end
end,
}
function public.index_function(state, handler) function public.index_function(state, handler)
local self = {} local self = {}
data[self] = {handler=handler, state=state} data[self] = {handler=handler, state=state}
return setmetatable(self, mt) return setmetatable(self, {__metatable=false, __index=__index, state=data})
end end
end end
+63 -43
View File
@@ -1,58 +1,78 @@
local PUBLIC, PRIVATE = 1, 2 local DECLARED, MUTABLE, PUBLIC = 1, 2, 4
local state = {} local DECLARED_KEY, MUTABLE_KEY, PUBLIC_KEY = 'declared', 'mutable', 'public'
local function_mt = { local MODIFIER_VALUE = {[DECLARED_KEY]=DECLARED, [MUTABLE_KEY]=MUTABLE, [PUBLIC_KEY]=PUBLIC}
__call = function(self, ...)
local f = state[self] local band, bor, bnot = bit.band, bit.bor, bit.bnot
local temp = getfenv(f).__ local _G = getfenv(0)
getfenv(f).__ = {}
f(unpack(arg)) local _state, _data, _metadata = {}, {}, setmetatable({}, {__index=function() return 0 end})
getfenv(f).__ = temp
--local function_mt = {
-- __call = function(self, ...)
-- local f = state[self]
-- getfenv(f).__ = {}
-- f(unpack(arg))
-- getfenv(f).__ = temp
-- end,
--}
--local method_mt = {
-- __call = function(self, ...)
-- local f = self[1]
-- local temp = getfenv(f)
-- setfenv(f, state[self])
-- f(...)
-- setfenv(f, temp)
-- end,
--}
local environment_mt = {
__metatable = false,
__index = function(self, key)
if _metadata[self][key] == 0 then return _G[key] or error('No key "'..key..'".', 2) end
return _data[self][key]
end,
__newindex = function(self, key, value)
local m = _metadata[self][key]
if band(bor(DECLARED, MUTABLE), m) == DECLARED then error('"'..key..'" is immutable.', 2) end
_data[self][key] = value
_metadata[self][key] = bor(1, m)
end, end,
} }
local interface_mt = { local interface_mt = {
__metatable = false,
__index = function(self, key) __index = function(self, key)
if not state[self].access[state[self].type][key] then if band(PUBLIC_V, _metadata[self][key]) == 0 then error('No key "'..key..'".', 2) end
error('Read of undeclared "'..key..'".', 2) return _data[self][key]
end
return state[self].data[key]
end,
__newindex = function(self, key, value)
if type(value) == 'string' then
aux.log('kek', debugstack(1, 0, 5))
end
if state[self].type == PUBLIC then
error('Unsupported operation.', 2)
elseif not state[self].access[PRIVATE][key] then
error('Write of undeclared "'..key..'".', 2)
end
state[self].data[key] = value
end, end,
__newindex = function() error('Unsupported operation.', 2) end,
} }
local declarator_mt = { local modifier_mt = {
__index = function() __metatable = false,
error('Unsupported operation.', 2) __call = function(self, key)
_state[self] = MODIFIER_VALUE[key]
end,
__index = function(self, key)
local value = MODIFIER_VALUE[key]
if not value then error('Unsupported operation.', 2) end
_state[self] = bor(_state[self], value)
return self
end, end,
__newindex = function(self, key, value) __newindex = function(self, key, value)
if state[self].access[PRIVATE][key] then if _metadata[self][key] then error('Duplicate key "'..key..'".', 2) end
error('Multiple declarations of "'..key..'".', 2) _data[self][key] = value
end _metadata[self][key] = bor(_state[self], DECLARED)
state[self].data[key] = value
state[self].access[PRIVATE][key] = true
state[self].access[state[self].type][key] = true
end, end,
} }
function aux_module() function aux_module()
local data, access = {}, {{}, {__=true}} local modifier, environment, interface = setmetatable({}, modifier_mt), setmetatable({}, environment_mt), setmetatable({}, interface_mt)
local public_state, private_state = {type=PUBLIC, data=data, access=access}, {type=PRIVATE, data=data, access=access} local data, metadata = {mutable=modifier, public=modifier}, {mutable=1, public=1}
local interface, private_interface = setmetatable({}, interface_mt), setmetatable({}, interface_mt)
local public_declarator, private_declarator = setmetatable({}, declarator_mt), setmetatable({}, declarator_mt)
state[interface], state[private_interface] = public_state, private_state
state[public_declarator], state[private_declarator] = public_state, private_state
local env = setmetatable({m=private_interface, public=public_declarator, private=private_declarator}, {__index=getfenv(0)}) _data[modifier], _data[environment], _data[interface] = data, data, data
-- public_state.env = env _metadata[modifier], _metadata[environment], _metadata[interface] = metadata, metadata, metadata
-- private_state.env = env _state[modifier] = 1
setfenv(2, env) environment.mutable.__()
environment.m = environment -- TODO for compatibility, remove later
setfenv(2, environment)
return interface return interface
end end