From 5fc37fe404260fbe813833d7255cbbf9a54b0094 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Mon, 5 Sep 2016 09:08:15 +0200 Subject: [PATCH] refactoring --- aux-addon.lua | 10 ++++++---- libs/green_t.lua | 1 - libs/inspect.lua | 31 +++++++++++++++++++++++++++++++ libs/module.lua | 33 ++++++++++++++++----------------- 4 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 libs/inspect.lua diff --git a/aux-addon.lua b/aux-addon.lua index 50636cd..bc3b4a7 100644 --- a/aux-addon.lua +++ b/aux-addon.lua @@ -9,6 +9,7 @@ do local modules = {} modules[name] = module and M import (green_t) private.aux = setmetatable(t, mt) + private.p.set = inspect end)() end modules[name].import (modules.core.I) @@ -32,6 +33,7 @@ local current_owner_page function public.current_owner_page.get() return current_owner_page end local event_frame = CreateFrame('Frame') + for event in temp-S('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 @@ -39,13 +41,13 @@ end ADDON_LOADED = t do local handlers, handlers2 = t, t - function public.LOAD.set(f) handlers[f] = true end + function public.LOAD.set(f) tinsert(handlers, f) end function public.LOAD2.set(f) handlers2[f] = true end event_frame:SetScript('OnEvent', function() if event == 'ADDON_LOADED' then (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 print('v' .. version .. ' loaded.') @@ -151,7 +153,7 @@ end function public.is_player(name, current) local realm = GetCVar('realmName') - return not current and index(_G.aux_characters, realm, name) or UnitName 'player' == name + return not current and index(_G.aux_characters, realm, name) or UnitName('player') == name end function public.neutral_faction() @@ -269,6 +271,6 @@ end function AuctionFrameAuctions_OnEvent(...) temp=arg if AuctionFrameAuctions:IsVisible() then - return orig.AuctionFrameAuctions_OnEvent(unpack(arg)) + return orig.AuctionFrameAuctions_OnEvent(unpack(arg)) end end \ No newline at end of file diff --git a/libs/green_t.lua b/libs/green_t.lua index 183cd22..9f8afc3 100644 --- a/libs/green_t.lua +++ b/libs/green_t.lua @@ -125,7 +125,6 @@ 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/inspect.lua b/libs/inspect.lua new file mode 100644 index 0000000..efb5ea3 --- /dev/null +++ b/libs/inspect.lua @@ -0,0 +1,31 @@ +local function print(msg) + DEFAULT_CHAT_FRAME:AddMessage(RED_FONT_COLOR_CODE .. msg) +end + +local function format_key(k) + return type(k) == 'string' and k or '[' .. tostring(k) .. ']' +end +local function format_value(v) + return type(v) == 'string' and '"' .. v .. '"' or tostring(v) +end + +function inspect(...) + for i = 1, arg.n do + print('arg' .. i..' = ' .. format_value(arg[i])) + if type(arg[i]) == 'table' and next(arg[i]) then + print('{') + for k, v in arg[i] do + print(' ' .. format_key(k) .. ' = ' .. format_value(v)) + end + print('}') + end + end + return unpack(arg) +end + +p = setmetatable({}, { + __metatable = false, + __pow = function(self, v) self(v); return v end, + __sub = function(self, v) self(v); return v end, + __call = function(_, ...) return inspect(unpack(arg)) end, +}) \ No newline at end of file diff --git a/libs/module.lua b/libs/module.lua index 91ed4f8..047f19d 100644 --- a/libs/module.lua +++ b/libs/module.lua @@ -68,7 +68,7 @@ do elseif access or nil_error() then self.default_access = access end self.declaration_access, self.declaration_name = nil, nil end - DECLARATOR.__tostring = function() return 'NIL' end + DECLARATOR.__tostring = function() return 'nil table' end end local function import(self, interface) @@ -80,19 +80,18 @@ local function import(self, interface) end end -setmetatable(_G, { - __index = function(_, key) - if key ~= 'module' then return nil end - local interface, environment, declarator = INTERFACE {}, ENVIRONMENT {}, DECLARATOR {} - local self; self = { - access = {_G=PRIVATE, I=PRIVATE, M=PRIVATE, public=PRIVATE, private=PRIVATE, import=PRIVATE, _=PRIVATE, p=PRIVATE, error=PRIVATE, nop=PRIVATE}, - [CALL] = {_G=_G, I=interface, M=environment, import=function(interface) import(self, interface) end, p=inspect, error=error, nop=nop}, - [INDEX] = {public=function() return declarator.public end, private=function() return declarator.private end}, - [NEWINDEX] = {p=inspect, _=nop}, - declarator = declarator, - } - state[interface], state[environment], state[declarator] = self, self, self - setfenv(2, environment) - return interface - end, -}) \ No newline at end of file +local mt = {}; setmetatable(_G, mt) +function mt:__index(key) + if key ~= 'module' then return nil end + local interface, environment, declarator = INTERFACE {}, ENVIRONMENT {}, DECLARATOR {} + self = { + access = {_G=PRIVATE, I=PRIVATE, M=PRIVATE, public=PRIVATE, private=PRIVATE, import=PRIVATE, _=PRIVATE, error=PRIVATE, nop=PRIVATE}, + [CALL] = {_G=_G, I=interface, M=environment, import=function(interface) import(self, interface) end, error=error, nop=nop}, + [INDEX] = {public=function() return declarator.public end, private=function() return declarator.private end}, + [NEWINDEX] = {_=nop}, + declarator = declarator, + } + state[interface], state[environment], state[declarator] = self, self, self + setfenv(2, environment) + return interface +end \ No newline at end of file