diff --git a/aux-addon.lua b/aux-addon.lua index 8947cb3..3d41dca 100644 --- a/aux-addon.lua +++ b/aux-addon.lua @@ -6,8 +6,7 @@ do aux = function(name) if not modules[name] then (function() - _ = module - modules[name] = M + modules[name] = module and M import (green_t) private.aux = setmetatable({}, {__metatable=false, __index=function(_, key) return modules[key].I end, __newindex=error}) end)() @@ -262,7 +261,7 @@ do end function ADDON_LOADED.Blizzard_CraftUI() hook('CraftFrame_SetSelection', function(...) temp=arg - local results = temp-{orig.CraftFrame_SetSelection(unpack(arg))} + local ret = temp-A(orig.CraftFrame_SetSelection(unpack(arg))) local id = GetCraftSelectionIndex() local reagent_count = GetCraftNumReagents(id) local total_cost = 0 @@ -284,12 +283,12 @@ do end end CraftReagentLabel:SetText(SPELL_REAGENTS..' '..cost_label(total_cost)) - return unpack(results) + return unpack(ret) end) end function ADDON_LOADED.Blizzard_TradeSkillUI() hook('TradeSkillFrame_SetSelection', function(...) temp=arg - local results = temp-{orig.TradeSkillFrame_SetSelection(unpack(arg))} + local ret = temp-A(orig.TradeSkillFrame_SetSelection(unpack(arg))) local id = GetTradeSkillSelectionIndex() local reagent_count = GetTradeSkillNumReagents(id) local total_cost = 0 @@ -311,7 +310,7 @@ do end end TradeSkillReagentLabel:SetText(SPELL_REAGENTS..' '..cost_label(total_cost)) - return unpack(results) + return unpack(ret) end) end end diff --git a/libs/green_t.lua b/libs/green_t.lua index ff67239..a807557 100644 --- a/libs/green_t.lua +++ b/libs/green_t.lua @@ -13,11 +13,12 @@ function wipe(t) setmetatable(t, nil); setn(t, 0) for k in t do t[k] = nil end t.reset, t.reset = nil, 1 + return t end public.wipe = wipe function recycle(t) - wipe(t); tmp[wipe(t)] = nil + wipe(t); tmp[t] = nil if pool_size < 50 then pool_size = pool_size + 1 pool[pool_size] = t @@ -41,38 +42,38 @@ public.t.get = table function temp_table() local t = table(); tmp[t] = true; return t end public.tt.get = temp_table -do local mt = {__metatable=false, __newindex=error} - function public.empty.get() return setmetatable(temp_table, mt) end +do local mt = {__newindex=nop} + function public.empty.get() return setmetatable(temp_table(), mt) end end do local f, mt f = function(_, v) if type(v) == 'table' then tmp[v] = true end return v end - mt = {__metatable=false, __call=f, __sub=f} + mt = {__call=f, __sub=f} public.temp {get=function() return setmetatable(temp_table(), mt) end, set=function(t) tmp[t] = true end} end do local f, mt f = function(_, v) if type(v) == 'table' then tmp[v] = nil end return v end - mt = {__metatable=false, __call=f, __sub=f} + mt = {__call=f, __sub=f} public.perm {get=function() return setmetatable(temp_table(), mt) end, set=function(t) tmp[t] = nil end} end do local SET, ARRAY, ARRAY0, TABLE = 1, 2, 3, 4 local function insert(type) - local chunk = 'setmetatable(t, nil); local setn, error = table.setn, error; return function(t' + local chunk = 'local setmetatable, setn, error = setmetatable, table.setn, error; return function(t' for i = 1, 98 do chunk = chunk..',a'..i end - chunk = chunk..',overflow)' + chunk = chunk..',overflow) setmetatable(t, nil);' if type == SET then for i = 1, 98 do chunk = chunk..format('if a%d == nil then return t end t[a%d] = true;', i, i) end elseif type == ARRAY then - chunk = chunk..'setn(t, 99); if a1 == nil then return t end t[1] = a1;' + chunk = chunk..'setn(t, 98); if a1 == nil then return t end t[1] = a1;' for i = 2, 98 do chunk = chunk..format('if a%d == nil then setn(t, %d); return t end t[%d] = a%d;', i, i - 1, i, i) end elseif type == ARRAY0 then - chunk = chunk..'setn(t, 99);' + chunk = chunk..'setn(t, 98);' for i = 1, 98 do chunk = chunk..format('t[%d] = a%d;', i, i) end elseif type == TABLE then for i = 1, 97, 2 do @@ -83,9 +84,9 @@ do local f = loadstring(chunk); setfenv(f, M); return f() end local function getter(type) - local mt = {__metatable=false, __newindex=error, __call=insert(type)} - function mt:__index(t) recycle(self); return setmetatable(mt, t) end + local mt = {__newindex=nop, __call=insert(type)} + function mt:__index(t) recycle(self); return setmetatable(t, mt) end return function() return setmetatable(t, mt) end end - public(); S, A, A0, T = {get=getter(SET)}, {get=getter(ARRAY)}, {get=getter(ARRAY0)}, {get=getter(TABLE)} + public(); S.get = getter(SET); A.get = getter(ARRAY); A0.get = getter(ARRAY0); T.get = getter(TABLE) end \ No newline at end of file diff --git a/libs/module.lua b/libs/module.lua index c9da810..9d0c75b 100644 --- a/libs/module.lua +++ b/libs/module.lua @@ -2,7 +2,7 @@ if module then return end local setfenv, getmetatable, setmetatable, type, unpack, next, _G = setfenv, getmetatable, setmetatable, type, unpack, next, getfenv(0) local PUBLIC, PRIVATE = 1, 2 -local FIELD, INDEX, NEWINDEX = 1, 2, 3 +local CALL, INDEX, NEWINDEX = 1, 2, 3 local inspect do local function print(msg) DEFAULT_CHAT_FRAME:AddMessage(RED_FONT_COLOR_CODE..msg) end @@ -21,7 +21,6 @@ do local function print(msg) DEFAULT_CHAT_FRAME:AddMessage(RED_FONT_COLOR_CODE.. __pow=function(self, v) self(v); return v end, }) end -p = inspect -- TODO local function error(msg, ...) return _G.error(format(msg or '', unpack(arg))..'\n'..debugstack(), 0) end local function import_error() error('Import error.') end @@ -40,14 +39,16 @@ local INTERFACE, ENVIRONMENT, DECLARATOR = prototype(), prototype(), prototype() local state = {} function INTERFACE:__index(key) self=state[self] - return (self.access[key] == PUBLIC or nil) and self[FIELD][key] or (self[INDEX][key] or nop)() + if self.access[key] == PUBLIC then return self[CALL][key] or (self[INDEX][key] or nop)() end end function INTERFACE:__newindex(key, value) self=state[self] - return (self.access[key] == PUBLIC or nil) and (self[NEWINDEX][key] or nop)(value) + if self.access[key] == PUBLIC then (self[NEWINDEX][key] or nop)(value) end end -function ENVIRONMENT:__index(key) self=state[self] local getter = self[INDEX][key] - return self[FIELD][key] or getter and getter() or not getter and (_G[key] or self.declarator[key]) +function ENVIRONMENT:__index(key) self=state[self] + local f = self[CALL][key]; if f then return f end + local getter = self[INDEX][key]; if getter then return getter() end + return _G[key] or self.declarator[key] end function ENVIRONMENT:__newindex(key, value) self=state[self] if self.access[key] then (self[NEWINDEX][key] or collision_error(key))(value) else self.declarator[key] = value end @@ -55,11 +56,11 @@ end ENVIRONMENT.__call = nop -- TODO do - local ACCESS, EVENT = {public=PUBLIC, private=PRIVATE}, {value=FIELD, get=INDEX, set=NEWINDEX} - local function declare(self, access, name, value) + local ACCESS, EVENT = {public=PUBLIC, private=PRIVATE}, {call=CALL, get=INDEX, set=NEWINDEX} + local function declare(self, access, name, handlers) self.access[name] = (not self.access[name] or collision_error(name)) and access or self.default_access or declaration_error() - for k, v in value do - if v and (k == FIELD or type(v) == 'function') or declaration_error() then self[k][name] = v end + for event, handler in handlers do local handler + self[event][name] = type(handler) == 'function' and handler or event == INDEX and function() return handler end or declaration_error() end end function DECLARATOR:__index(key) self=state[self]; local name, access = self.declaration_name, self.declaration_access @@ -72,16 +73,16 @@ do end function DECLARATOR:__newindex(key, value) self=state[self]; local name, access = self.declaration_name, self.declaration_access if name then declare(self, access, name, {[EVENT[key] or nil_error(name)]=value}) - elseif access or self.default_access then declare(self, access, key, {[FIELD]=value}) - else _G[key] = (not _G[key] or collision_error(key)) and value end -- G.error(nil) TODO silent error? + elseif access or self.default_access then declare(self, access, key, {[type(value) == 'function' and CALL or INDEX]=value}) + else _G[key] = (_G[key] == nil or collision_error(key)) and value end -- G.error(nil) TODO silent error? self.declaration_access, self.declaration_name = nil, nil end function DECLARATOR:__call(v) self=state[self]; local name, access = self.declaration_name, self.declaration_access if name then if type(v) ~= 'table' or getmetatable(v) ~= nil then (access and declaration_error() or nil_error)(name) end - local value, getter, setter; value, getter, setter, v.value, v.get, v.set = v.value, v.get, v.set, nil, nil, nil - if next(v) or value ~= nil and getter ~= nil then (access and declaration_error() or nil_error)(name) end - declare(self, access, name, {[FIELD]=value, [INDEX]=getter, [NEWINDEX]=setter}) + local f, getter, setter; 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 (access and declaration_error() or nil_error)(name) end + declare(self, access, name, {[CALL]=f, [INDEX]=getter, [NEWINDEX]=setter}) elseif access or nil_error() then self.default_access = access end self.declaration_access, self.declaration_name = nil, nil end @@ -92,7 +93,7 @@ local function import(self, interface) local module = (interface == INTERFACE or import_error()) and state[interface] for k, v in module.access do if v == PUBLIC and not self.access[k] then - self.access[k], self[FIELD][k], self[INDEX][k], self[NEWINDEX][k] = PRIVATE, module[FIELD][k], module[INDEX][k], module[NEWINDEX][k] + self.access[k], self[CALL][k], self[INDEX][k], self[NEWINDEX][k] = PRIVATE, module[CALL][k], module[INDEX][k], module[NEWINDEX][k] end end end @@ -102,8 +103,8 @@ setmetatable(_G, { 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}, - [FIELD] = {_G=_G, I=interface, M=environment, import=function(interface) import(self, interface) end, p=inspect, error=error, nop=nop}, + access = {pp=PRIVATE, _G=PRIVATE, I=PRIVATE, M=PRIVATE, public=PRIVATE, private=PRIVATE, import=PRIVATE, _=PRIVATE, p=PRIVATE, error=PRIVATE, nop=PRIVATE}, + [CALL] = {pp=function(o) inspect(o == ENVIRONMENT) end, _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,