From f51d99134e30cd776e7d4e9742022be93f1e0e56 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Tue, 6 Sep 2016 19:27:25 +0200 Subject: [PATCH] refactoring --- aux-addon.lua | 4 ++-- core/tooltip.lua | 3 +-- libs/green_t.lua | 40 ++++++++++++++++++++++++++++++---------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/aux-addon.lua b/aux-addon.lua index eb16ce9..9233bc2 100644 --- a/aux-addon.lua +++ b/aux-addon.lua @@ -215,7 +215,7 @@ do end function ADDON_LOADED.Blizzard_CraftUI() hook('CraftFrame_SetSelection', function(...) auto[arg] = true - local ret = temp-A(orig.CraftFrame_SetSelection(unpack(arg))) + local ret = temp-A0(orig.CraftFrame_SetSelection(unpack(arg))) local id = GetCraftSelectionIndex() local reagent_count = GetCraftNumReagents(id) local total_cost = 0 @@ -242,7 +242,7 @@ do end function ADDON_LOADED.Blizzard_TradeSkillUI() hook('TradeSkillFrame_SetSelection', function(...) auto[arg] = true - local ret = temp-A(orig.TradeSkillFrame_SetSelection(unpack(arg))) + local ret = temp-A0(orig.TradeSkillFrame_SetSelection(unpack(arg))) local id = GetTradeSkillSelectionIndex() local reagent_count = GetTradeSkillNumReagents(id) local total_cost = 0 diff --git a/core/tooltip.lua b/core/tooltip.lua index 0873026..f6be614 100644 --- a/core/tooltip.lua +++ b/core/tooltip.lua @@ -29,14 +29,13 @@ function LOAD() end local orig = SetItemRef setglobal('SetItemRef', function(...) auto[arg] = true - local result = orig(unpack(arg)) local name, _, quality = GetItemInfo(arg[1]) if not IsShiftKeyDown() and not IsControlKeyDown() and name then local color_code = select(4, GetItemQualityColor(quality)) local link = color_code .. '|H' .. arg[1] .. '|h[' .. name .. ']|h' .. FONT_COLOR_CODE_CLOSE extend_tooltip(ItemRefTooltip, link, 1) end - return result + return orig(unpack(arg)) end) end diff --git a/libs/green_t.lua b/libs/green_t.lua index e3834e0..050802f 100644 --- a/libs/green_t.lua +++ b/libs/green_t.lua @@ -112,29 +112,43 @@ function public.pseudo_vararg_function(body, upvals) end do - local body = [[ - if arg100 ~= nil then error("Vararg overflow.") end + local MAXPARAMS = 100 + + local code = [[ + local f, setn, acquire_auto = f, setn, acquire_auto + return function( + ]] + for i = 1, MAXPARAMS - 1 do + code = code .. format('a%d,', i) + end + code = code .. [[ + overflow) + if overflow ~= nil then error("Vararg overflow.") end local n repeat ]] - for i = 99, 1, -1 do - body = body .. format('if a%1$d ~= nil then n = %1$d; break end;', i) + for i = MAXPARAMS - 1, 1, -1 do + code = code .. format('if a%1$d ~= nil then n = %1$d; break end;', i) end - body = body .. [[ + code = code .. [[ until true local t = acquire_auto() setn(t, n) repeat ]] - for i = 1, 99 do - body = body .. format('if %1$d > n then break end; t[%1$d] = a%1$d;', i) + for i = 1, MAXPARAMS - 1 do + code = code .. format('if %1$d > n then break end; t[%1$d] = a%1$d;', i) end - body = body .. [[ + code = code .. [[ until true return f(t) + end ]] + function public.vararg(f) - return pseudo_vararg_function(body, {f=f, error=error, setn=setn, acquire=acquire_auto, release=release}) + local chunk = loadstring(code) + setfenv(chunk, {f=f, setn=setn, acquire_auto=acquire_auto}) + return chunk() end end @@ -168,7 +182,13 @@ do local mt = {__call = pseudo_vararg_function(insert_chunk(mode) .. 'setmetatable(a1, nil); return a1', upvals)} return function() return setmetatable(acquire(), mt) end end - public.S.get = pseudo_table_literal('k') + public.S = vararg(function(arg) + local set = acquire() + for _, v in arg do + if v ~= nil then set[v] = true end + end + return set + end) public.A.get = pseudo_table_literal('v') public.A0.get = pseudo_table_literal('v0') public.T.get = pseudo_table_literal('kv')