refactoring
This commit is contained in:
+25
-21
@@ -43,29 +43,30 @@ public.empty = setmetatable({}, {__metatable=false, __newindex=error})
|
||||
do
|
||||
local pool, overflow_pool, transient = {}, setmetatable({}, {__mode='v'}), {}
|
||||
CreateFrame'Frame':SetScript('OnUpdate', function()
|
||||
-- local t = tremove(transient)
|
||||
for _, t in transient do recycle(t) end
|
||||
for t in transient do recycle(t) end
|
||||
wipe(transient)
|
||||
end)
|
||||
function public.wipe(t) -- like with a cloth or something
|
||||
for k in t do t[k] = nil end
|
||||
t.reset = 1
|
||||
t.reset = nil
|
||||
table.setn(t, 0)
|
||||
return t
|
||||
return setmetatable(t, nil)
|
||||
end
|
||||
function public.recycle(t)
|
||||
wipe(t)
|
||||
if getn(pool) < 50 then
|
||||
tinsert(pool, t)
|
||||
else
|
||||
tinsert(weak_pool, t)
|
||||
overflow_pool[t] = true
|
||||
end
|
||||
log(getn(table_pool), '-', getn(weak_pool))
|
||||
end
|
||||
function public.t.get()
|
||||
return tremove(pool) or tremove(weak_pool) or {}
|
||||
return tremove(pool) or tremove(overflow_pool, next(overflow_pool)) or {}
|
||||
end
|
||||
function public.tt.get()
|
||||
local t = tremove(pool) or tremove(weak_pool) or {}
|
||||
local t = tremove(pool) or tremove(overflow_pool, next(overflow_pool)) or {}
|
||||
transient[t] = true
|
||||
return t
|
||||
end
|
||||
@@ -78,21 +79,24 @@ do
|
||||
return {__unm=function(self) self.raw = true end, __call=apply, __sub=apply}
|
||||
end
|
||||
do
|
||||
local make_transient, make_persistent, temp_mt, perm_mt
|
||||
function make_transient(t) transient[t] = true return t end
|
||||
function make_persistent(t) transient[t] = nil return t end
|
||||
temp_mt = operator_mt(make_transient)
|
||||
perm_mt = operator_mt(make_persistent)
|
||||
public.temp
|
||||
{
|
||||
get = function() return setmetatable(t, temp_mt) end,
|
||||
set = function(t) return make_transient(t) end,
|
||||
}
|
||||
public.perm
|
||||
{
|
||||
get = function() return setmetatable(t, perm_mt) end,
|
||||
set = function(t) return make_persistent(t) end,
|
||||
}
|
||||
local function make_transient(t) transient[t] = true return t end
|
||||
local function make_persistent(t) transient[t] = nil return t end
|
||||
do
|
||||
local mt = operator_mt(make_transient)
|
||||
public.temp
|
||||
{
|
||||
get = function() return setmetatable(t, mt) end,
|
||||
set = function(t) return make_transient(t) end,
|
||||
}
|
||||
end
|
||||
do
|
||||
local mt = operator_mt(make_persistent)
|
||||
public.perm
|
||||
{
|
||||
get = function() return setmetatable(t, mt) end,
|
||||
set = function(t) return make_persistent(t) end,
|
||||
}
|
||||
end
|
||||
end
|
||||
local function insert_keys(t,k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15,k16,k17,k18,k19,k20,overflow)
|
||||
if overflow ~= nil then error 'Overflow.' end
|
||||
|
||||
+23
-21
@@ -1,10 +1,10 @@
|
||||
if module then return end
|
||||
local type, setmetatable, setfenv, unpack, next, intersection, union, pcall, _G = type, setmetatable, setfenv, unpack, next, bit.band, bit.bor, pcall, getfenv(0)
|
||||
local error, import_error, declaration_error, collision_error, mutability_error
|
||||
local pass, start_declaration, advance_declaration, env_mt, interface_mt, metadata_mt, property_mt
|
||||
local noop, start_declaration, advance_declaration, env_mt, interface_mt, property_mt
|
||||
|
||||
local PRIVATE, PUBLIC, MUTABLE, PROPERTY = 0, 1, 2, 4
|
||||
local INDEX, NEWINDEX, CALL = 1, 2, 4
|
||||
local INDEX, NEWINDEX, CALL = 1, 2, 3
|
||||
local _state = {}
|
||||
|
||||
function error(message, ...)
|
||||
@@ -15,7 +15,7 @@ declaration_error = function() error 'Invalid declaration.' end
|
||||
collision_error = function(key) error('Field "%s" already exists.', key) end
|
||||
mutability_error = function(key) error('Field "%s" is immutable.', key) end
|
||||
|
||||
pass = function() end
|
||||
noop = function() end
|
||||
|
||||
do
|
||||
local function unpack_property_value(t)
|
||||
@@ -28,7 +28,7 @@ do
|
||||
local metadata = state.metadata
|
||||
metadata[key] = metadata[key] and collision_error(key) or modifiers
|
||||
if intersection(PROPERTY, modifiers) == 0 then
|
||||
state.data[key] = value
|
||||
state.fields[key] = value
|
||||
else
|
||||
local success, getter, setter = pcall(unpack_property_value, value)
|
||||
if success or declaration_error() then
|
||||
@@ -37,6 +37,7 @@ do
|
||||
end
|
||||
end
|
||||
|
||||
advance_declaration = noop
|
||||
do
|
||||
local MODIFIER = {private=PRIVATE, public=PUBLIC, mutable=MUTABLE, property=PROPERTY}
|
||||
local state, modifiers, property
|
||||
@@ -49,16 +50,16 @@ do
|
||||
elseif type == NEWINDEX then
|
||||
if property then key, value = property, {[key]=value} end
|
||||
declare(state, union(modifiers, state.modifiers), key, value)
|
||||
advance_declaration = pass
|
||||
advance_declaration = noop
|
||||
return true
|
||||
elseif type == CALL then
|
||||
if property then declare(state, union(modifiers, state.modifiers), property, value) else state.modifiers = modifiers end
|
||||
advance_declaration = pass
|
||||
advance_declaration = noop
|
||||
return true
|
||||
end
|
||||
end
|
||||
function start_declaration(state, modifier)
|
||||
if advance_declaration ~= pass then declaration_error() end
|
||||
if advance_declaration ~= noop then declaration_error() end
|
||||
advance_declaration, state, modifiers, property = intercept, state, modifier, nil
|
||||
end
|
||||
end
|
||||
@@ -69,7 +70,7 @@ do
|
||||
if intersection(PROPERTY, state.metadata[key] or 0) ~= 0 then
|
||||
return state.getters[key]()
|
||||
else
|
||||
local value = state.data[key]
|
||||
local value = state.fields[key]
|
||||
if value ~= nil then return value else return _G[key] end
|
||||
end
|
||||
end
|
||||
@@ -80,7 +81,7 @@ do
|
||||
if intersection(PROPERTY, modifiers) ~= 0 then
|
||||
state.setters[key](value)
|
||||
elseif intersection(MUTABLE, modifiers) ~= 0 or mutability_error(key) then
|
||||
state.data[key] = value
|
||||
state.fields[key] = value
|
||||
end
|
||||
else
|
||||
declare(state, state.modifiers, key, value)
|
||||
@@ -97,41 +98,42 @@ function interface_mt:__index(key, state) state=_state[self]
|
||||
if masked == PUBLIC+PROPERTY then
|
||||
return state.getters[key]()
|
||||
elseif masked == PUBLIC then
|
||||
return state.data[key]
|
||||
return state.fields[key]
|
||||
end
|
||||
end
|
||||
function interface_mt:__newindex(key, value, state) state=_state[self]
|
||||
if intersection(PUBLIC+PROPERTY, state.metadata[key] or 0) == PUBLIC+PROPERTY then
|
||||
return state.setters[key](value)
|
||||
-- elseif masked == PUBLIC and type(state.data[key]) == 'function' then
|
||||
-- return state.data[key](value)
|
||||
-- elseif masked == PUBLIC and type(state.fields[key]) == 'function' then
|
||||
-- return state.fields[key](value)
|
||||
end
|
||||
end
|
||||
|
||||
property_mt = {__index=function() return pass end}
|
||||
property_mt = {__index=function() return noop end}
|
||||
|
||||
local function module(...)
|
||||
function module(...)
|
||||
local state, env, interface
|
||||
env, interface = setmetatable({}, env_mt), setmetatable({}, interface_mt)
|
||||
state = {
|
||||
metadata = {_=PROPERTY, error=PRIVATE, pass=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, private=PROPERTY, public=PROPERTY, mutable=PROPERTY, property=PROPERTY},
|
||||
metadata = {_=PROPERTY, error=PRIVATE, noop=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, private=PROPERTY, public=PROPERTY, mutable=PROPERTY, property=PROPERTY},
|
||||
fields = {_G=_G, M=env, I=interface, error=error},
|
||||
getters = setmetatable({
|
||||
private = function() start_declaration(PRIVATE, state); return env end,
|
||||
public = function() start_declaration(PUBLIC, state); return env end,
|
||||
mutable = function() start_declaration(MUTABLE, state); return env end,
|
||||
property = function() start_declaration(PROPERTY, state); return env end,
|
||||
private = function() start_declaration(state, PRIVATE); return env end,
|
||||
public = function() start_declaration(state, PUBLIC); return env end,
|
||||
mutable = function() start_declaration(state, MUTABLE); return env end,
|
||||
property = function() start_declaration(state, PROPERTY); return env end,
|
||||
}, property_mt),
|
||||
setters = setmetatable({}, property_mt),
|
||||
modifiers = PRIVATE,
|
||||
}
|
||||
for i=1,arg.n do
|
||||
local module = state[arg[i] or import_error()] or import_error()
|
||||
for k, v in module.metadata do
|
||||
if intersection(PUBLIC, v) ~= 0 and (not state.metadata[k] or import_error()) then
|
||||
state.metadata[k], state.data[k], state.getters[k], state.setters[k] = v, module.data[k], module.getters[k], module.setters[k]
|
||||
state.metadata[k], state.fields[k], state.getters[k], state.setters[k] = v, module.fields[k], module.getters[k], module.setters[k]
|
||||
end
|
||||
end
|
||||
end
|
||||
state[env], state[interface] = state, state
|
||||
_state[env], _state[interface] = state, state
|
||||
setfenv(2, env)
|
||||
end
|
||||
Reference in New Issue
Block a user