refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-28 14:41:19 +02:00
parent c07c3dad6f
commit 60d4fc85b3
10 changed files with 74 additions and 66 deletions
+39 -37
View File
@@ -5,6 +5,12 @@ module()
public.version = '5.0.0' public.version = '5.0.0'
function public.log(...) --temp=arg
local msg = '[aux]'
for i = 1, arg.n do msg = msg..' '..tostring(arg[i]) end
DEFAULT_CHAT_FRAME:AddMessage(LIGHTYELLOW_FONT_COLOR_CODE..msg)
end
do do
local modules = {core={env=M, interface=I}} local modules = {core={env=M, interface=I}}
_G.aux = setmetatable({}, { _G.aux = setmetatable({}, {
@@ -29,14 +35,14 @@ function public.bids_loaded.get() return bids_loaded end
local current_owner_page local current_owner_page
function public.current_owner_page.get() return current_owner_page end function public.current_owner_page.get() return current_owner_page end
public.empty = setmetatable({}, {__metatable=false, __newindex=error}) function public.F() end
do do
local pool, overflow_pool, transient = {}, setmetatable({}, {__mode='v'}), {} local pool, overflow_pool, tmp = {}, setmetatable({}, {__mode='v'}), {}
CreateFrame'Frame':SetScript('OnUpdate', function() CreateFrame'Frame':SetScript('OnUpdate', function()
for t in transient do recycle(t) end for t in tmp do recycle(t) end
wipe(transient) wipe(tmp)
end) end)
function public.wipe(t) -- like with a cloth or something function public.wipe(t) -- like with a cloth or something
@@ -54,16 +60,15 @@ do
else else
tinsert(overflow_pool, t) tinsert(overflow_pool, t)
end end
-- log(getn(table_pool), '-', getn(weak_pool)) log(getn(pool), '-', getn(overflow_pool))
end end
function public.t.get() function public.t.get()
return tremove(pool) or tremove(overflow_pool, next(overflow_pool)) or {} return tremove(pool) or tremove(overflow_pool, next(overflow_pool)) or {}
end end
function public.tt.get() function public.tt.get()
local t = tremove(pool) or tremove(overflow_pool, next(overflow_pool)) or {} local t = tremove(pool) or tremove(overflow_pool, next(overflow_pool)) or {}
transient[t] = true tmp[t] = true
return t return t
end end
@@ -76,24 +81,20 @@ do
return {__unm=function(self) self.raw = true; return self end, __call=apply, __sub=apply} return {__unm=function(self) self.raw = true; return self end, __call=apply, __sub=apply}
end end
do do
local function make_transient(t) transient[t] = true; return t end local mt = operator_mt(function(t) tmp[t] = true; return t end)
local function make_persistent(t) transient[t] = nil; return t end public.temp
do {
local mt = operator_mt(make_transient) get = function() return setmetatable(t, mt) end,
public.temp set = function(t) tmp[t] = true end,
{ }
get = function() return setmetatable(t, mt) end, end
set = function(t) return make_transient(t) end, do
} local mt = operator_mt(function(t) tmp[t] = nil; return t end)
end public.perm
do {
local mt = operator_mt(make_persistent) get = function() return setmetatable(t, mt) end,
public.perm set = function(t) tmp[t] = nil end,
{ }
get = function() return setmetatable(t, mt) end,
set = function(t) return make_persistent(t) end,
}
end
end end
local function insert_keys(t,k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15,k16,k17,k18,k19,k20,overflow) local function insert_keys(t,k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15,k16,k17,k18,k19,k20,overflow)
@@ -162,19 +163,20 @@ do
return {__call=f, __unm=function(self) return setmetatable(self, nil) end} return {__call=f, __unm=function(self) return setmetatable(self, nil) end}
end end
local set_mt, list_mt, object_mt = collector_mt(insert_keys), collector_mt(insert_values), collector_mt(insert_pairs) local set_mt, list_mt, object_mt = collector_mt(insert_keys), collector_mt(insert_values), collector_mt(insert_pairs)
public()
if public then function set.get() return setmetatable(t, set_mt) end
function set.get() return setmetatable(t, set_mt) end function list.get() return setmetatable(t, list_mt) end
function list.get() return setmetatable(t, list_mt) end function object.get() return setmetatable(t, object_mt) end
function object.get() return setmetatable(t, object_mt) end private()
end
end end
function public.log(...) temp=arg --local wt = setmetatable(t, {__mode='kv'})
local msg = '[aux]' --local a, b = t, t
for i = 1, arg.n do msg = msg..' '..tostring(arg[i]) end --tinsert(wt, a)
DEFAULT_CHAT_FRAME:AddMessage(LIGHTYELLOW_FONT_COLOR_CODE..msg) --tinsert(wt, t)
end --tinsert(wt, b)
--collectgarbage()
--log (getn(wt))
local event_frame = CreateFrame('Frame') 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-set('ADDON_LOADED', 'VARIABLES_LOADED', 'PLAYER_LOGIN', 'AUCTION_HOUSE_SHOW', 'AUCTION_HOUSE_CLOSED', 'AUCTION_BIDDER_LIST_UPDATE', 'AUCTION_OWNED_LIST_UPDATE') do
@@ -206,7 +208,7 @@ tab_info = t
do do
for k, v in -temp-object('search_tab', 'Search', 'post_tab', 'Post', 'auctions_tab', 'Auctions', 'bids_tab', 'Bids') do for k, v in -temp-object('search_tab', 'Search', 'post_tab', 'Post', 'auctions_tab', 'Auctions', 'bids_tab', 'Bids') do
local tab = -object('name', v) local tab = -object('name', v)
local env = (function() aux(v) return M end)() local env = (function() aux(k) return M end)()
function env.private.OPEN.set(f) tab.OPEN = f end function env.private.OPEN.set(f) tab.OPEN = f end
function env.private.CLOSE.set(f) tab.CLOSE = f end function env.private.CLOSE.set(f) tab.CLOSE = f end
function env.private.USE_ITEM.set(f) tab.USE_ITEM = f end function env.private.USE_ITEM.set(f) tab.USE_ITEM = f end
+3 -3
View File
@@ -3,7 +3,7 @@ aux 'scan' local info, history = aux.info, aux.history
PAGE_SIZE = 50 PAGE_SIZE = 50
do do
local scan_states = {} local scan_states = t
function public.start(params) function public.start(params)
for old_state in present(scan_states[params.type]) do for old_state in present(scan_states[params.type]) do
@@ -18,7 +18,7 @@ do
end end
function public.abort(scan_id) function public.abort(scan_id)
local aborted = {} local aborted = t
for type, state in scan_states do for type, state in scan_states do
if not scan_id or state.id == scan_id then if not scan_id or state.id == scan_id then
kill_thread(state.id) kill_thread(state.id)
@@ -108,7 +108,7 @@ function submit_query()
elseif state.params.type == 'owner' then elseif state.params.type == 'owner' then
GetOwnerAuctionItems(state.page) GetOwnerAuctionItems(state.page)
else else
local blizzard_query = query.blizzard_query or {} local blizzard_query = query.blizzard_query or t
QueryAuctionItems( QueryAuctionItems(
blizzard_query.name, blizzard_query.name,
blizzard_query.min_level, blizzard_query.min_level,
+1 -1
View File
@@ -1,6 +1,6 @@
aux 'stack' local info = aux.info aux 'stack' local info = aux.info
local state = nil local state
function stack_size(slot) function stack_size(slot)
local container_item_info = info.container_item(unpack(slot)) local container_item_info = info.container_item(unpack(slot))
+9 -9
View File
@@ -14,14 +14,14 @@ function LOAD()
aux_background:SetAllPoints(DropDownList1Backdrop) aux_background:SetAllPoints(DropDownList1Backdrop)
blizzard_backdrop = DropDownList1Backdrop:GetBackdrop() blizzard_backdrop = DropDownList1Backdrop:GetBackdrop()
hook('ToggleDropDownMenu', function(...) temp=arg hook('ToggleDropDownMenu', function(...) temp=arg
local ret = {orig.ToggleDropDownMenu(unpack(arg)) } local ret = {orig.ToggleDropDownMenu(unpack(arg)) }
local dropdown = _G[arg[4] or ''] or this:GetParent() local dropdown = _G[arg[4] or ''] or this:GetParent()
if strfind(dropdown:GetName() or '', '^aux_frame%d+$') then if strfind(dropdown:GetName() or '', '^aux_frame%d+$') then
set_aux_dropdown_style(dropdown) set_aux_dropdown_style(dropdown)
else else
set_blizzard_dropdown_style() set_blizzard_dropdown_style()
end end
return unpack(ret) return unpack(ret)
end) end)
function set_aux_dropdown_style(dropdown) function set_aux_dropdown_style(dropdown)
@@ -203,7 +203,7 @@ function public.resize_tab(tab, width, padding)
end end
do do
local mt = {__index={}} local mt = {__index=t}
function mt.__index:create_tab(text) function mt.__index:create_tab(text)
local id = getn(self._tabs) + 1 local id = getn(self._tabs) + 1
+13 -8
View File
@@ -17,14 +17,14 @@ local state = {}
local declarator_mt = {__metatable=false} local declarator_mt = {__metatable=false}
do do
local ACCESS, EVENT = {public=PUBLIC, private=PRIVATE}, {call=CALL, get=INDEX, set=NEWINDEX} local ACCESS, EVENT = {public=PUBLIC, private=PRIVATE}, {call=CALL, get=INDEX, set=NEWINDEX}
local function extract(v) local function extract_handlers(v)
local f, getter, setter local f, getter, setter
f, getter, setter, v.call, v.get, v.set = v.call, v.get, v.set, nil, nil, nil f, getter, setter, v.call, v.get, v.set = v.call, v.get, v.set, nil, nil, nil
if next(v) or f ~= nil and getter ~= nil then error() end if next(v) or f ~= nil and getter ~= nil then error() end
return f, getter, setter return f, getter, setter
end end
local function declare(self, access, name, handlers) local function declare(self, access, name, handlers)
self.access[name] = self.access[name] and collision_error(name) or access or PRIVATE self.access[name] = self.access[name] and collision_error(name) or access or self.default_access
for event, handler in handlers do for event, handler in handlers do
if type(handler) ~= 'function' and (event == INDEX or declaration_error()) then if type(handler) ~= 'function' and (event == INDEX or declaration_error()) then
handler = const(handler == nil and tostring(name) or handler) handler = const(handler == nil and tostring(name) or handler)
@@ -33,7 +33,7 @@ do
end end
end end
function declarator_mt:__index(key) self=state[self] function declarator_mt:__index(key) self=state[self]
if ACCESS[key] and not self.declaration_access then if ACCESS[key] and (not (self.declaration_access or self.declaration_name) or declaration_error()) then
self.declaration_access = ACCESS[key] self.declaration_access = ACCESS[key]
elseif not self.declaration_name or declaration_error() then elseif not self.declaration_name or declaration_error() then
self.declaration_name = type(key) == 'string' and key or declaration_error() self.declaration_name = type(key) == 'string' and key or declaration_error()
@@ -48,20 +48,24 @@ do
name, event = key, type(value) == 'function' and CALL or INDEX name, event = key, type(value) == 'function' and CALL or INDEX
end end
declare(self, self.declaration_access, name, {[event]=value}) declare(self, self.declaration_access, name, {[event]=value})
self.declaration_access, self.declaration_name = nil, nil self.declaration_access, self.declaration_name = nil, nil
end end
function declarator_mt:__call(value) self=state[self] function declarator_mt:__call(value) self=state[self]
if self.declaration_name then if self.declaration_name then
local success, f, getter, setter = pcall(extract, value) local success, f, getter, setter = pcall(extract_handlers, value)
if not success then declaration_error() end if not success then declaration_error() end
declare(self, self.declaration_access, self.declaration_name, {[CALL]=f, [INDEX]=getter, [NEWINDEX]=setter}) declare(self, self.declaration_access, self.declaration_name, {[CALL]=f, [INDEX]=getter, [NEWINDEX]=setter})
elseif self.declaration_access or declaration_error() then
self.default_access = self.declaration_access
end end
self.declaration_access, self.declaration_name = nil, nil self.declaration_access, self.declaration_name = nil, nil
end end
end end
local env_mt = {__metatable=false} local env_mt = {__metatable=false}
function env_mt:__index(key) self=state[self] function env_mt:__index(key) self=state[self]
local quote = state.quote
if quote then return quote(key) end
local getter = self[INDEX][key] local getter = self[INDEX][key]
if getter then return getter() end if getter then return getter() end
return self[CALL][key] or _G[key] or self.declarator[key] return self[CALL][key] or _G[key] or self.declarator[key]
@@ -88,12 +92,13 @@ end
function module(...) function module(...)
local declarator, env, interface = setmetatable({}, declarator_mt), setmetatable({}, env_mt), setmetatable({}, interface_mt) local declarator, env, interface = setmetatable({}, declarator_mt), setmetatable({}, env_mt), setmetatable({}, interface_mt)
local self = { local self; self = {
access = {_=PRIVATE, error=PRIVATE, nop=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, public=PRIVATE, private=PRIVATE}, access = {_=PRIVATE, error=PRIVATE, nop=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, public=PRIVATE, private=PRIVATE},
[CALL] = {error=error, nop=nop}, [CALL] = {error=error, nop=nop},
[INDEX] = {_G=const(_G), M=const(env), I=const(interface), public=function() return declarator.public end, private=function() return declarator.private end}, [INDEX] = {_G=const(_G), M=const(env), I=const(interface), public=function() return declarator.public end, private=function() return declarator.private end},
[NEWINDEX] = {_=nop}, [NEWINDEX] = {_=nop, quote=function(enable) self.quote = enable and type(enable) == 'function' and enable or tostring end},
declarator = declarator, declarator = declarator,
default_access = PRIVATE,
} }
for i = 1, arg.n do for i = 1, arg.n do
local module = state[arg[i] or import_error()] local module = state[arg[i] or import_error()]
+2 -2
View File
@@ -588,8 +588,8 @@ function create_frames()
filter_display = scroll_child filter_display = scroll_child
end end
status_bars = {} status_bars = t
tables = {} tables = t
for _=1,5 do for _=1,5 do
local status_bar = gui.status_bar(frame) local status_bar = gui.status_bar(frame)
status_bar:SetAllPoints(status_bar_frame) status_bar:SetAllPoints(status_bar_frame)
+1
View File
@@ -50,6 +50,7 @@ do
end end
} }
public.color.get = color_accessor(function(color) public.color.get = color_accessor(function(color)
log'kek'
local r, g, b, a = unpack(color) local r, g, b, a = unpack(color)
return setmetatable({r/255, g/255, b/255, a}, mt) return setmetatable({r/255, g/255, b/255, a}, mt)
end) end)
+2 -2
View File
@@ -81,12 +81,12 @@ function public.modified.get() return IsShiftKeyDown() or IsControlKeyDown() or
--end --end
do do
local _state = setmetatable({}, {__mode='kv'}) local _state = setmetatable(t, -object('__mode', 'kv'))
local __index = function(self, key) local __index = function(self, key)
return _state[self].handler({public=self, private=_state[self].state}, key) return _state[self].handler({public=self, private=_state[self].state}, key)
end end
function public.index_function(state, handler) -- TODO rename table-accessor, use predicate to stop function public.index_function(state, handler) -- TODO rename table-accessor, use predicate to stop
local state, self = {handler=handler, state=state}, {} local state, self = {handler=handler, state=state}, t
_state[self] = state _state[self] = state
return setmetatable(self, {__metatable=false, __index=__index, state=state}) return setmetatable(self, {__metatable=false, __index=__index, state=state})
end end
+3 -3
View File
@@ -279,7 +279,7 @@ function parse_parameter(input_type, str)
end end
function public.parse_query_string(str) function public.parse_query_string(str)
local post_filter = {} local post_filter = t
local blizzard_filter_parser = blizzard_filter_parser() local blizzard_filter_parser = blizzard_filter_parser()
local parts = map(split(str, '/'), function(part) return strlower(trim(part)) end) local parts = map(split(str, '/'), function(part) return strlower(trim(part)) end)
@@ -533,11 +533,11 @@ function validator(components)
if components.blizzard.exact and strlower(info.item(record.item_id).name) ~= components.blizzard.name[2] then if components.blizzard.exact and strlower(info.item(record.item_id).name) ~= components.blizzard.name[2] then
return false return false
end end
local stack = {} local stack = tt
for i = getn(components.post), 1, -1 do for i = getn(components.post), 1, -1 do
local type, name, param = unpack(components.post[i]) local type, name, param = unpack(components.post[i])
if type == 'operator' then if type == 'operator' then
local args = {} local args = tt
while (not param or param > 0) and getn(stack) > 0 do while (not param or param > 0) and getn(stack) > 0 do
tinsert(args, tremove(stack)) tinsert(args, tremove(stack))
param = param and param - 1 param = param and param - 1
+1 -1
View File
@@ -79,7 +79,7 @@ end
function public.write_record(schema, record) function public.write_record(schema, record)
local separator = schema[2] local separator = schema[2]
local parts = tt local parts = tt
for i = 3, getn(schema) do for i = 3 , getn(schema) do
local key, type = next(schema[i]) local key, type = next(schema[i])
tinsert(parts, write(type, record[key])) tinsert(parts, write(type, record[key]))
end end