From 9576b9fe3113463c66020ef58ca8fa266542114c Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Mon, 5 Sep 2016 08:07:40 +0200 Subject: [PATCH] refactoring --- aux-addon.lua | 10 +++++----- aux-addon.toc | 1 + libs/green_t.lua | 18 ++++++++---------- libs/module.lua | 17 ----------------- util/color.lua | 2 +- 5 files changed, 15 insertions(+), 33 deletions(-) diff --git a/aux-addon.lua b/aux-addon.lua index 3b18120..50636cd 100644 --- a/aux-addon.lua +++ b/aux-addon.lua @@ -39,15 +39,15 @@ end ADDON_LOADED = t do local handlers, handlers2 = t, t - function public.LOAD.set(f) tinsert(handlers, f) end - function public.LOAD2.set(f) tinsert(handlers2, f) end + function public.LOAD.set(f) handlers[f] = true end + function public.LOAD2.set(f) handlers2[f] = true end event_frame:SetScript('OnEvent', function() if event == 'ADDON_LOADED' then - if ADDON_LOADED[arg1] then ADDON_LOADED[arg1]() end + (ADDON_LOADED[arg1] or nop)() elseif event == 'VARIABLES_LOADED' then - for _, f in handlers do f() end + for f in handlers do f() end elseif event == 'PLAYER_LOGIN' then - for _, f in handlers2 do f() end + for f in handlers2 do f() end print('v' .. version .. ' loaded.') else M[event]() diff --git a/aux-addon.toc b/aux-addon.toc index b01ae18..4b163d5 100644 --- a/aux-addon.toc +++ b/aux-addon.toc @@ -4,6 +4,7 @@ ## SavedVariablesPerCharacter: aux_auto_buy_filter, aux_tooltip_value, aux_tooltip_daily, aux_tooltip_vendor_buy, aux_tooltip_vendor_sell, aux_tooltip_disenchant_value, aux_tooltip_disenchant_distribution, aux_tooltip_disenchant_source ## SavedVariables: aux_datasets, aux_items, aux_item_ids, aux_auctionable_items, aux_merchant_buy, aux_merchant_sell, aux_characters, aux_recent_searches, aux_favorite_searches, aux_ignore_owner, aux_price_per_unit +libs\inspect.lua libs\module.lua libs\green_t.lua diff --git a/libs/green_t.lua b/libs/green_t.lua index 1fe2358..183cd22 100644 --- a/libs/green_t.lua +++ b/libs/green_t.lua @@ -90,15 +90,15 @@ function public.pseudo_vararg_function(body, upvals) end local function insert_chunk(mode) - local body = '' + local body = 'repeat ' if mode == 'k' then for i = 2, 99 do - body = body .. format('if a%1$d == nil then return end; a1[a%1$d] = true;', i) + body = body .. format('if a%1$d == nil then break end; a1[a%1$d] = true;', i) end elseif mode == 'v' then - body = body .. 'if a2 == nil then return end; a1[1] = a2;' + body = body .. 'if a2 == nil then break end; a1[1] = a2;' for i = 3, 99 do - body = body .. format('if a%1$d == nil then setn(a1, %d); return end; a1[%d] = a%1$d;', i, i-2, i-1) + body = body .. format('if a%1$d == nil then setn(a1, %d); break end; a1[%d] = a%1$d;', i, i-2, i-1) end elseif mode == 'v0' then body = body .. 'setn(a1, 98);' @@ -107,19 +107,16 @@ local function insert_chunk(mode) end elseif mode == 'kv' then for i = 2, 98, 2 do - body = body .. format('if a%1$d == nil then return end; a1[a%1$d] = a%d;', i, i+1) + body = body .. format('if a%1$d == nil then break end; a1[a%1$d] = a%d;', i, i+1) end end - return body .. 'if a100 ~= nil then error"Overflow." end;' + return body .. 'if a100 ~= nil then error"Overflow." end until false;' end do local function pseudo_literal(mode) local upvals = {setmetatable=setmetatable, setn=table.setn, error=error} - local mt = { - __newindex = nop, - __call = pseudo_vararg_function(insert_chunk(mode) .. 'setmetatable(a1, nil); return a1', upvals) - } + local mt = {__call = pseudo_vararg_function(insert_chunk(mode) .. 'setmetatable(a1, nil); return a1', upvals)} return function() return setmetatable(acquire_temp(), mt) end end public.S.get = pseudo_literal'k' @@ -128,6 +125,7 @@ do public.T.get = pseudo_literal'kv' end +p = T('koo', 1, 5, 'mooo') do local body = '' for i = 2, 100 do diff --git a/libs/module.lua b/libs/module.lua index 5902d7a..91ed4f8 100644 --- a/libs/module.lua +++ b/libs/module.lua @@ -4,23 +4,6 @@ local setfenv, getmetatable, setmetatable, type, unpack, next, _G = setfenv, get local PUBLIC, PRIVATE = 1, 2 local CALL, INDEX, NEWINDEX = 1, 2, 3 -local inspect -do local function print(msg) DEFAULT_CHAT_FRAME:AddMessage(RED_FONT_COLOR_CODE .. msg) end - local mt = {__metatable=false, __pow=function(self, v) self(v); return v end} - function mt:__call(...) - for i = 1, arg.n do - print('arg' .. i..' = ' .. (type(arg[i]) == 'string' and '"' .. arg[i] .. '"' or tostring(arg[i]))) - if type(arg[i]) == 'table' and next(arg[i]) then - print('{') - for k, v in arg[i] do print(' ' .. (type(k) == 'string' and k or '[' .. tostring(k) .. ']') .. ' = ' .. (type(v) == 'string' and '"' .. v..'"' or tostring(v))) end - print('}') - end - end - return unpack(arg) - end - inspect = setmetatable({}, mt) -end - local function error(msg, ...) return _G.error(format(msg or '', unpack(arg)) .. '\n' .. debugstack(), 0) end local function import_error() error('Import error.') end local function declaration_error() error('Declaration error.') end diff --git a/util/color.lua b/util/color.lua index f36d824..b622c87 100644 --- a/util/color.lua +++ b/util/color.lua @@ -33,7 +33,7 @@ do end function color_accessor(callback) return function() - return index_function(__(tt) :callback(callback) :table(COLORS), index_handler) + return index_function({callback=callback, table=COLORS}, index_handler) end end end