From db41eebbf25b40dcc544ff2ed11ab92cd15652bb Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Thu, 25 Aug 2016 17:03:56 +0200 Subject: [PATCH] refactoring --- aux-addon.lua | 55 +++-------- core/disenchant.lua | 4 +- module.lua | 203 +++++++++++++++++++++++------------------ tabs/search/filter.lua | 4 +- util/core.lua | 8 ++ util/filter.lua | 6 +- 6 files changed, 142 insertions(+), 138 deletions(-) diff --git a/aux-addon.lua b/aux-addon.lua index 1886a83..d9867b0 100644 --- a/aux-addon.lua +++ b/aux-addon.lua @@ -22,31 +22,31 @@ public.empty = {} public.pass = function() end do - local pool, overflow_pool, transient = {}, setmetatable({}, {__mode='v'}), {} + local pool, weak_pool, transient = {}, setmetatable({}, {__mode='v'}), {} CreateFrame'Frame':SetScript('OnUpdate', function() - for t in transient do recycle(t) end +-- local t = tremove(transient) + for _, t in transient do recycle(t) end wipe(transient) end) function public.wipe(t) -- like with a cloth or something for k in t do t[k] = nil end table.setn(t, 0) - return setmetatable(t, nil) + return t end function public.recycle(t) - transient[t] = nil wipe(t) if getn(pool) < 50 then tinsert(pool, t) else - tinsert(overflow_pool, t) + tinsert(weak_pool, t) end - log(getn(table_pool), '-', getn(overflow_pool)) + log(getn(table_pool), '-', getn(weak_pool)) end function public.t.get() - return tremove(pool) or tremove(overflow_pool) or {} + return tremove(pool) or tremove(weak_pool) or {} end function public.tt.get() - local t = tremove(pool) or tremove(overflow_pool) or {} + local t = tremove(pool) or tremove(weak_pool) or {} transient[t] = true return t end @@ -59,45 +59,14 @@ do return {__unm=function(self) self.raw = true end, __call=apply, __sub=apply} end do - local make_transient, make_persistent, temp_mt, perm_mt - function make_transient(t) transient[t] = true return t end - function make_persistent(t) transient[t] = nil return t end + local make_transient, temp_mt + function make_transient(t) tinsert(transient, t) return t end temp_mt = operator_mt(make_transient) - perm_mt = operator_mt(make_persistent) public.temp { get = function() return setmetatable(t, temp_mt) end, set = function(t) return make_transient(t) end, } - public.perm - { - get = function() return setmetatable(t, perm_mt) end, - set = function(t) return make_persistent(t) end, - } - end - local function keys(t,k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15,k16,k17,k18,k19,k20,overflow) - if overflow ~= nil then error 'Overflow.' end - if k1 ~= nil then t[k1] = true end - if k2 ~= nil then t[k2] = true end - if k3 ~= nil then t[k3] = true end - if k4 ~= nil then t[k4] = true end - if k5 ~= nil then t[k5] = true end - if k6 ~= nil then t[k6] = true end - if k7 ~= nil then t[k7] = true end - if k8 ~= nil then t[k8] = true end - if k9 ~= nil then t[k9] = true end - if k10 ~= nil then t[k10] = true end - if k11 ~= nil then t[k11] = true end - if k12 ~= nil then t[k12] = true end - if k13 ~= nil then t[k13] = true end - if k14 ~= nil then t[k14] = true end - if k15 ~= nil then t[k15] = true end - if k16 ~= nil then t[k16] = true end - if k17 ~= nil then t[k17] = true end - if k18 ~= nil then t[k18] = true end - if k19 ~= nil then t[k19] = true end - if k20 ~= nil then t[k20] = true end - return t end local function values(t,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,overflow) if overflow ~= nil then error 'Overflow.' end @@ -142,13 +111,13 @@ do end local set_mt, list_mt, object_mt = collector_mt(keys), collector_mt(values), collector_mt(pairs) public.property() - set, list, object = {get=function() return setmetatable(t, set_mt) end}, {get=function() return setmetatable(t, list_mt) end}, {get=function() return setmetatable(t, object_mt) end} + list, object = {get=function() return setmetatable(t, list_mt) end}, {get=function() return setmetatable(t, object_mt) end} private() -- TODO or 'auto' 'transient'? end local event_frame = CreateFrame 'Frame' -for event in -temp-set('ADDON_LOADED', 'VARIABLES_LOADED', 'PLAYER_LOGIN', 'AUCTION_HOUSE_SHOW', 'AUCTION_HOUSE_CLOSED', 'AUCTION_BIDDER_LIST_UPDATE', 'AUCTION_OWNED_LIST_UPDATE') do +for _, event in -temp-list('ADDON_LOADED', 'VARIABLES_LOADED', 'PLAYER_LOGIN', 'AUCTION_HOUSE_SHOW', 'AUCTION_HOUSE_CLOSED', 'AUCTION_BIDDER_LIST_UPDATE', 'AUCTION_OWNED_LIST_UPDATE') do event_frame:RegisterEvent(event) end diff --git a/core/disenchant.lua b/core/disenchant.lua index 95050d7..6d9e2cd 100644 --- a/core/disenchant.lua +++ b/core/disenchant.lua @@ -39,7 +39,7 @@ do end function LOAD() - ARMOR = -set( + ARMOR = set( 'INVTYPE_HEAD', 'INVTYPE_NECK', 'INVTYPE_SHOULDER', @@ -56,7 +56,7 @@ function LOAD() 'INVTYPE_CLOAK', 'INVTYPE_HOLDABLE' ) - WEAPON = -set( + WEAPON = set( 'INVTYPE_2HWEAPON', 'INVTYPE_WEAPONMAINHAND', 'INVTYPE_WEAPON', diff --git a/module.lua b/module.lua index f2cf35a..a47f9f6 100644 --- a/module.lua +++ b/module.lua @@ -1,41 +1,27 @@ -local type, setmetatable, setfenv, unpack, next, mask, pcall, _G = type, setmetatable, setfenv, unpack, next, bit.band, pcall, getfenv(0) -local PRIVATE, PUBLIC, MUTABLE, DYNAMIC, PROPERTY = 0, 1, 2, 4, 8 -local function error(message, ...) return _G.error(format(message or '', unpack(arg))..'\n'..debugstack(), 0) end -local import_error, declaration_error = function() error 'Invalid import statement.' end, function() error 'Invalid declaration.' end -local collision_error, mutability_error = function(key) error('Field "%s" already exists.', key) end, function(key) error('Field "%s" is immutable.', key) end -local declare, env_mt, interface_mt, declarator_mt, importer_mt -local empty, pass = {}, function() end +local type, setmetatable, setfenv, unpack, next, mask, combine, pcall, _G = type, setmetatable, setfenv, unpack, next, bit.band, bit.bor, pcall, getfenv(0) +local error, import_error, declaration_error, collision_error, mutability_error +local pass, env_mt, interface_mt, declarator_mt, importer_mt + +local PRIVATE, PUBLIC, MUTABLE, DYNAMIC, PROPERTY = 1, 2, 4, 8, 16 local _state, _modules = {}, {} -importer_mt = {__metatable=false } + +function error(message, ...) + return _G.error(format(message or '', unpack(arg))..'\n'..debugstack(), 0) +end +import_error = function() error 'Invalid import statement.' end +declaration_error = function() error 'Invalid declaration.' end +collision_error = function(key) error('Field "%s" already exists.', key) end +mutability_error = function(key) error('Field "%s" is immutable.', key) end + +pass = function() end + +function get() + setfenv() +end + do - local function unpack_property_value(t) - local get, set; get, set, t.get, t.set = t.get or pass, t.set or pass, nil, nil - if next(t) or type(get ~= 'function') or type(set) ~= 'function' then error() end - return get, set - end - function declare(self, modifiers, key, value) - local metadata = self.metadata - if metadata[key] then collision_error(key) end - metadata[key] = modifiers - if mask(PROPERTY, modifiers) ~= 0 then - local success, getter, setter = pcall(unpack_property_value, value) - if success or declaration_error() then self.getters[key], self.setters[key] = getter, setter end - else - self.data[key] = value - end - end -end -function importer_mt.__index(self, key, state) state=_state[self] - if type(key) ~= 'string' then import_error() end - state[self] = key; return self -end -function importer_mt.__call(self, arg1, arg2, state) state=_state[self] - local name, module, alias - name = arg2 or arg1 - if type(name) ~= 'string' then import_error() end - module = _modules[name] - alias, state[self] = state[self] or name, nil - if module then + importer_mt = {__metatable=false} + local function import(state, module, alias) if alias == '' then for key, modifiers in module.metadata do if not state.metadata[key] and mask(PUBLIC, modifiers) ~= 0 then @@ -46,47 +32,71 @@ function importer_mt.__call(self, arg1, arg2, state) state=_state[self] state.metadata[alias], state.data[alias] = PRIVATE, module.interface end end - return self -end -declarator_mt = {__metatable=false} -do - local MODIFIER = {private=PRIVATE, public=PUBLIC, mutable=MUTABLE} - local COMPATIBLE = {private=MUTABLE+PROPERTY, public=PROPERTY, mutable=PRIVATE} - function declarator_mt.__index(self, key, state) state=_state[self] - if state.property then declaration_error() end - local modifiers, modifier, compatible = state[self], MODIFIER[key], COMPATIBLE[key] - if not modifier then modifier, compatible, state.property = PROPERTY, PRIVATE+PUBLIC, key end - if mask(compatible, modifiers) ~= modifiers then declaration_error() end - state[self] = modifiers + modifier - return self + function importer_mt:__index(key, state) state=_state[self] + state[self] = key; return self + end + function importer_mt:__call(arg1, arg2, state) state=_state[self] + local name, alias, module; name = arg2 or arg1 + alias, state[self] = state[self] or name, nil + return pcall(import, state, _modules[name], alias) and self or import_error() end end + do - declarator_mt.__newindex = function(self, key, value, state) state=_state[self] - local property, modifiers; property, modifiers, state[self] = state.property, state[self], PRIVATE - if property then state.property = nil; declare(state, modifiers, property, {[key]=value}) end - end - function declarator_mt.__call(self, value, state) state=_state[self] - local property, modifiers; property, modifiers, state[self] = state.property, state[self], PRIVATE - if property then state.property = nil; declare(state, modifiers, property, value) else state.modifiers = modifiers end - end -end -do - local function index(public) - local access = public and PUBLIC or 0 - return function(self, key, state) state=_state[self] - local masked = mask(access+PROPERTY, state.metadata[key] or 0) - if masked == access+PROPERTY then - return state.getters[key]() - elseif masked == access then - return state.data[key] - elseif not public then - return _G[key] + local declare + do + local function unpack_property_value(t) + local get, set; get, set, t.get, t.set = t.get or pass, t.set or pass, nil, nil + if next(t) or type(get) ~= 'function' or type(set) ~= 'function' then error() end + return get, set + end + function declare(state, modifiers, key, value) + local metadata = state.metadata + metadata[key] = metadata[key] and collision_error(key) or modifiers + if mask(PROPERTY, modifiers) == 0 then + state.data[key] = value + else + local success, getter, setter = pcall(unpack_property_value, value) + if success or declaration_error() then state.getters[key], state.setters[key] = getter, setter end end end end - env_mt = {__metatable=false, __index=index()} - function env_mt.__newindex(self, key, value, state) state=_state[self] + + declarator_mt = {__metatable=false} + do + local MODIFIER = {private=PRIVATE, public=PUBLIC, mutable=MUTABLE, dynamic=DYNAMIC, property=PROPERTY} + local COMPATIBLE = {private=MUTABLE+PROPERTY, public=DYNAMIC+PROPERTY, mutable=PRIVATE, dynamic=PUBLIC+PROPERTY, property=PUBLIC} + function declarator_mt:__index(key, state) state=_state[self] + if state.property then declaration_error() end + local modifiers, modifier, compatible = state[self], MODIFIER[key], COMPATIBLE[key] + if not modifier then modifier, compatible, state.property = PROPERTY, PUBLIC, key end + if mask(compatible, modifiers) ~= modifiers then declaration_error() end + state[self] = modifiers + modifier + return self + end + end + do + function declarator_mt:__newindex(key, value, state) state=_state[self] + local property, modifiers; property, modifiers, state[self] = state.property, state[self], PRIVATE + if property then key, value, state.property = property, {[key]=value}, nil end + declare(state, modifiers, key, value) + end + function declarator_mt:__call(value, state) state=_state[self] + local property, modifiers; property, modifiers, state[self] = state.property, state[self], PRIVATE + if property then state.property = nil; declare(state, modifiers, property, value) else state.modifiers = modifiers end + end + end + + env_mt = {__metatable=false} + function env_mt:__index(key, state) state=_state[self] + if mask(PROPERTY, state.metadata[key] or 0) ~= 0 then + return state.getters[key]() + else + local value = state.data[key] + if value ~= nil then return value else return _G[key] end + end + end + function env_mt:__newindex(key, value, state) state=_state[self] local modifiers = state.metadata[key] if modifiers then if mask(PROPERTY, modifiers) ~= 0 then @@ -98,30 +108,47 @@ do declare(state, state.modifiers, key, value) end end - interface_mt = {__metatable=false, __index=index(true)} - function interface_mt.__newindex(self, key, value, state) state=_state[self] - local modifiers = state.metadata[key] - if modifiers then - if mask(PUBLIC+PROPERTY, modifiers) == PUBLIC+PROPERTY then - return state.setters[key](value) - elseif mask(PUBLIC, modifiers) ~= 0 then - return state.data[key](value) - end +end + +interface_mt = {__metatable=false} +function interface_mt:__index(key, state) state=_state[self] + local masked = mask(PUBLIC+PROPERTY, state.metadata[key] or 0) + if masked == PUBLIC+PROPERTY then + return state.getters[key]() + elseif masked == PUBLIC then + return state.data[key] + end +end +function interface_mt:__newindex(key, value, state) state=_state[self] + local modifiers = state.metadata[key] + if modifiers then + if mask(PUBLIC+PROPERTY, modifiers) == PUBLIC+PROPERTY then + return state.setters[key](value) + elseif mask(PUBLIC, modifiers) ~= 0 then + return state.data[key](value) end end end + function module(name) if not _modules[name] then - local state, getters, env, interface, declarator, importer + local state, env, interface, declarator, importer env, interface, declarator, importer = setmetatable({}, env_mt), setmetatable({}, interface_mt), setmetatable({}, declarator_mt), setmetatable({}, importer_mt) - getters = { - private=function() state[declarator]=state.modifiers; return declarator.private end, public=function() state[declarator]=state.modifiers; return declarator.public end, - mutable=function() state[declarator]=state.modifiers; return declarator.mutable end, property=function() state[declarator]=state.modifiers; return declarator.property end, - } state = { - env=env, interface=interface, modifiers=PRIVATE, - metadata = {_=MUTABLE, _G=PRIVATE, M=PRIVATE, error=PRIVATE, import=PRIVATE, private=PROPERTY, public=PROPERTY, mutable=PROPERTY, property=PROPERTY}, - data = {_G=_G, M=env, error=error, import=importer}, getters=getters, setters={}, + env = env, + interface = interface, + modifiers = PRIVATE, + metadata = {_=PROPERTY, _G=PRIVATE, M=PRIVATE, error=PRIVATE, import=PRIVATE, private=PROPERTY, public=PROPERTY, mutable=PROPERTY, dynamic=PROPERTY, property=PROPERTY}, + data = {_G=_G, M=env, error=error, import=importer}, + getters = { + _ = pass, + private = function() state[declarator] = PRIVATE; return declarator end, + public = function() state[declarator] = PUBLIC; return declarator end, + mutable = function() state[declarator] = MUTABLE; return declarator end, + dynamic = function() state[declarator] = DYNAMIC; return declarator end, + property = function() state[declarator] = PROPERTY; return declarator end, + }, + setters = {_=pass, private=pass, public=pass, mutable=pass, dynamic=pass, property=pass}, } _modules[name], _state[env], _state[interface], _state[declarator], _state[importer] = state, state, state, state, state importer [''] 'core' -- TODO diff --git a/tabs/search/filter.lua b/tabs/search/filter.lua index b32d57d..619ec87 100644 --- a/tabs/search/filter.lua +++ b/tabs/search/filter.lua @@ -72,14 +72,14 @@ function update_form() end if blizzard_query.exact then - for key in -temp-set('class', 'subclass', 'slot', 'quality') do + for _, key in -temp-list('class', 'subclass', 'slot', 'quality') do M[key..'_dropdown'].button:Disable() end else class_dropdown.button:Enable() quality_dropdown.button:Enable() end - for key in -temp-set('min_level', 'max_level') do + for _, key in -temp-list('min_level', 'max_level') do if blizzard_query.exact then M[key..'_input']:Disable() else diff --git a/util/core.lua b/util/core.lua index 5e4c68f..4fd55bc 100644 --- a/util/core.lua +++ b/util/core.lua @@ -64,6 +64,14 @@ function public.index(t, ...) temp=arg return t end +function public.set(...) temp=arg + local t = t + for i=1,arg.n do + t[arg[i]] = true + end + return t +end + public.huge = 1.8*10^308 function public.modified.get() return IsShiftKeyDown() or IsControlKeyDown() or IsAltKeyDown() end diff --git a/util/filter.lua b/util/filter.lua index 0504d1d..7432391 100644 --- a/util/filter.lua +++ b/util/filter.lua @@ -208,7 +208,7 @@ public.filters = { function operator(str) local operator = str == 'not' and -list('operator', 'not', 1) - for name in -set('and', 'or') do + for _, name in -temp-list('and', 'or') do for arity in present(select(3, strfind(str, '^'..name..'(%d*)$'))) do arity = tonumber(arity) operator = not (arity and arity < 2) and -list('operator', name, arity) @@ -229,7 +229,7 @@ do end for number in present(tonumber(select(3, strfind(str, '^(%d+)$')))) do if number >= 1 and number <= 60 then - for filter in -temp-set('min_level', 'max_level') do + for _, filter in -temp-list('min_level', 'max_level') do if not self[filter] then self[filter] = {str, number} return true @@ -517,7 +517,7 @@ function blizzard_query(components) query.slot = slot_index query.quality = item_info.quality else - for key in -temp-set('min_level', 'max_level', 'class', 'subclass', 'slot', 'usable', 'quality') do + for _, key in -temp-list('min_level', 'max_level', 'class', 'subclass', 'slot', 'usable', 'quality') do query[key] = index(filters[key], 2) end end