big refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-20 21:33:28 +02:00
parent ca5fd63fc6
commit e94eaac52c
2 changed files with 14 additions and 11 deletions
+9 -5
View File
@@ -7,7 +7,7 @@ end
public.version = '5.0.0' public.version = '5.0.0'
local temp, recycle local t, temp, recycle
do do
local table_pool, locked = {}, {} local table_pool, locked = {}, {}
local function wipe(t) -- like with a cloth or something local function wipe(t) -- like with a cloth or something
@@ -17,19 +17,23 @@ do
end end
function recycle(t) function recycle(t)
locked[t] = nil locked[t] = nil
tinsert(table_pool, t) tinsert(table_pool, wipe(t))
log(getn(table_pool)) log(getn(table_pool))
end end
public.recycle = recycle public.recycle = recycle
temp = setmetatable({}, { temp = setmetatable({}, {
__call = function() __call = function()
local t = tremove(table_pool) local t = tremove(table_pool) or {}
t = t and wipe(t) or {}
locked[t] = true locked[t] = true
return t
end, end,
__sub = function(_, t) locked[t] = true return t end, __sub = function(_, t) locked[t] = true return t end,
}) })
public.temp = temp public.temp = temp
function t()
return tremove(table_pool) or {}
end
public.accessor.t = t
end end
do do
@@ -47,7 +51,7 @@ do
end end
local event_frame = CreateFrame 'Frame' local event_frame = CreateFrame 'Frame'
for event in temp-set{'VARIABLES_LOADED', 'ADDON_LOADED', 'AUCTION_HOUSE_SHOW', 'AUCTION_HOUSE_CLOSED', 'AUCTION_BIDDER_LIST_UPDATE', 'AUCTION_OWNED_LIST_UPDATE'} do for _, event in {'VARIABLES_LOADED', 'ADDON_LOADED', 'AUCTION_HOUSE_SHOW', 'AUCTION_HOUSE_CLOSED', 'AUCTION_BIDDER_LIST_UPDATE', 'AUCTION_OWNED_LIST_UPDATE'} do
event_frame:RegisterEvent(event) event_frame:RegisterEvent(event)
end end
+5 -6
View File
@@ -1,6 +1,7 @@
local setmetatable, unpack, mask, add, g = setmetatable, unpack, bit.band, bit.bor, getfenv(0) local setmetatable, unpack, mask, add, g = setmetatable, unpack, bit.band, bit.bor, getfenv(0)
local PRIVATE, PUBLIC, MUTABLE, ACCESSOR, MUTATOR = 1, 2, 4, 8, 16 local PRIVATE, PUBLIC, MUTABLE, ACCESSOR, MUTATOR = 1, 2, 4, 8, 16
local MODIFIER = {private=PRIVATE, public=PUBLIC, mutable=MUTABLE, accessor=ACCESSOR, mutator=MUTATOR} local MODIFIER = {private=PRIVATE, public=PUBLIC, mutable=MUTABLE, accessor=ACCESSOR, mutator=MUTATOR}
local MASK = {private=MUTABLE+ACCESSOR+MUTATOR, public=MUTABLE+ACCESSOR+MUTATOR, mutable=PRIVATE+PUBLIC, accessor=PRIVATE+PUBLIC+MUTATOR, mutator=PRIVATE+PUBLIC+ACCESSOR}
local _10, _01, tf, ft = {[true]=1, [false]=0}, {[true]=0, [false]=1}, {true, false}, {false, true} local _10, _01, tf, ft = {[true]=1, [false]=0}, {[true]=0, [false]=1}, {true, false}, {false, true}
local importer_mt, declarator_mt, env_mt, interface_mt, lock_mt local importer_mt, declarator_mt, env_mt, interface_mt, lock_mt
local define_property, declarator_accessor, private_accessor, public_accessor, mutable_accessor, accessor_accessor, mutator_accessor, index, error local define_property, declarator_accessor, private_accessor, public_accessor, mutable_accessor, accessor_accessor, mutator_accessor, index, error
@@ -17,21 +18,19 @@ end
private_accessor, public_accessor, mutable_accessor, accessor_accessor, mutator_accessor = declarator_accessor(0), declarator_accessor(PUBLIC), declarator_accessor(MUTABLE), declarator_accessor(ACCESSOR), declarator_accessor(MUTATOR) private_accessor, public_accessor, mutable_accessor, accessor_accessor, mutator_accessor = declarator_accessor(0), declarator_accessor(PUBLIC), declarator_accessor(MUTABLE), declarator_accessor(ACCESSOR), declarator_accessor(MUTATOR)
function define_property(self, key, t) function define_property(self, key, t)
local accessor, mutator = t.get, t.set local accessor, mutator = t.get, t.set
_declarator_state[self] = add(_01[not accessor]*ACCESSOR, _01[not mutator]*MUTATOR, _declarator_state[self]) _declarator_state[self] = _01[not accessor]*ACCESSOR + _01[not mutator]*MUTATOR + mask(PRIVATE+PUBLIC, _declarator_state[self])
_accessors[self][key], _mutators[self][key] = accessor, mutator _accessors[self][key], _mutators[self][key] = accessor, mutator
end end
declarator_mt = {__metatable=false} declarator_mt = {__metatable=false}
function declarator_mt.__index(self, key) function declarator_mt.__index(self, key)
local modifier = MODIFIER[key] local modifier = MODIFIER[key]
if not modifier then return function(t) define_property(self, key, t) end end if not modifier then return function(t) define_property(self, key, t) end end
_declarator_state[self] = add(modifier, _declarator_state[self]) _declarator_state[self] = modifier + mask(MASK[key], _declarator_state[self])
return self return self
end end
function declarator_mt.__newindex(self, key, value) function declarator_mt.__newindex(self, key, value)
if _metadata[self][key] then error('Field "%s" already exists.', key) end if _metadata[self][key] then error('Field "%s" already exists.', key) end
local modifiers = _declarator_state[self] _metadata[self][key], _data[self][key], _accessors[self][key], _mutators[self][key] = _declarator_state[self], value, value, value
if mask(MUTABLE, modifiers) * mask(ACCESSOR+MUTABLE) ~= 0 then error('Invalid modifiers.') end
_metadata[self][key], _data[self][key], _accessors[self][key], _mutators[self][key] = modifiers, value, value, value
end end
function index(access, default) function index(access, default)
return function(self, key) return function(self, key)
@@ -39,7 +38,7 @@ function index(access, default)
if mask(access+ACCESSOR+MUTATOR, modifiers) == access then if mask(access+ACCESSOR+MUTATOR, modifiers) == access then
return _data[self][key] return _data[self][key]
elseif mask(access+ACCESSOR, modifiers) == access+ACCESSOR then elseif mask(access+ACCESSOR, modifiers) == access+ACCESSOR then
return _accessors[self]() return _accessors[self][key]()
else else
return default[key] or error('No field "%s".', key) return default[key] or error('No field "%s".', key)
end end