This commit is contained in:
Manuel Simon Hirsig
2016-09-03 18:35:41 +02:00
parent 9826acf9bc
commit 05fd1587cd
37 changed files with 140 additions and 201 deletions
+50 -97
View File
@@ -1,138 +1,91 @@
--__.green_t = function()
green_t = module
if module 'green_t' then return end
local next, setn, type, setmetatable = next, table.setn, type, setmetatable
local wipe, recycle, table, temp_table
local wipe_chunk = [[
local pool, pool_size, overflow_pool, tmp = {}, 0, setmetatable({}, {__mode='k'}), {}
CreateFrame('Frame'):SetScript('OnUpdate', function()
for t in tmp do recycle(t) end; wipe(tmp)
end)
function wipe(t)
setmetatable(t, nil); setn(t, 0)
for k in t do t[k] = nil end
t.reset, t.reset = nil, 1
setn(t, 0)
setmetatable(t, nil)
]]
end
public.wipe = wipe
local recycle_chunk = wipe_chunk..[[
function recycle(t)
wipe(t); tmp[wipe(t)] = nil
if pool_size < 50 then
pool_size = pool_size + 1
pool[pool_size] = t
else
overflow_pool[t] = true
end
]]
end
public.recycle = recycle
local acquire_chunk = [[
local t
function table()
if pool_size > 0 then
t = pool[pool_size]
pool_size = pool_size - 1
else
t = next(overflow_pool)
if t then
overflow_pool[t] = nil
else
t = {}
end
return pool[pool_size + 1]
end
]]
local t = next(overflow_pool)
if t then overflow_pool[t] = nil; return t end
return {}
end
public.t.get = table
pool, pool_size, overflow_pool, tmp = {}, 0, setmetatable({}, {__mode='k'}), {}
function temp_table() local t = table(); tmp[t] = true; return t end
public.tt.get = temp_table
public.empty = setmetatable({}, {__metatable=false, __newindex=nop})
--select(1, 2, 3, END, kek()) -- TODO
do
local f = loadstring([[
local setmetatable, setn = setmetatable, table.setn
return function(t)
]]..wipe_chunk..'return t end')
setfenv(f, M)
public.wipe = f()
do local mt = {__metatable=false, __newindex=error}
function public.empty.get() return setmetatable(temp_table, mt) end
end
do
local f = loadstring([[
local setmetatable, setn, pool, pool_size, overflow_pool = setmetatable, table.setn, pool, pool_size, overflow_pool
return function(t)
]]..recycle_chunk..'end')
setfenv(f, M)
public.recycle = f()
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}
public.temp {get=function() return setmetatable(temp_table(), mt) end, set=function(t) tmp[t] = true end}
end
do
local f = loadstring(format([[
local setmetatable, setn, pool, pool_size, overflow_pool, t = setmetatable, table.setn, pool, pool_size, overflow_pool, tmp
return function() for t in t do %s end %s end
]], recycle_chunk, wipe_chunk))
setfenv(f, M)
CreateFrame'Frame':SetScript('OnUpdate', f())
end
do
local f = loadstring(format([[
local next, pool, pool_size, overflow_pool, tmp = next, pool, pool_size, overflow_pool, tmp
return function() %s; tmp[t] = true; return t end
]], acquire_chunk))
setfenv(f, M)
public.tt.get = f()
end
do local next, pool, pool_size, overflow_pool = next, pool, pool_size, overflow_pool
function public.t.get()
if pool_size > 0 then
pool_size = pool_size - 1
return pool[pool_size + 1]
end
local t = next(overflow_pool)
if t then
overflow_pool[t] = nil
return t
end
return {}
end
end
do local setmetatable, type, tmp = setmetatable, type, tmp
do local f, mt
f = function(_, v) if type(v) == 'table' then tmp[v] = true end return v end
mt = {__call=f, __sub=f}
public.temp {get=function() return setmetatable(tt, 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 = {__call=f, __sub=f}
public.perm {get=function() return setmetatable(tt, mt) end, set=function(t) tmp[t] = nil 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}
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 = 'local setn, error = table.setn, error; return function('
for i = 1, 99 do chunk = chunk..'a'..i..',' end
chunk = chunk..'overflow)'..acquire_chunk
local chunk = 'setmetatable(t, nil); local setn, error = table.setn, error; return function(t'
for i = 1, 98 do chunk = chunk..',a'..i end
chunk = chunk..',overflow)'
if type == SET then
for i = 1, 99 do
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;'
for i = 2, 99 do
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);'
for i = 1, 99 do
chunk = chunk..format('t[%d] = a%d;', i, i)
end
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
chunk = chunk..format('if a%d == nil then return t end t[a%d] = a%d;', i, i, i + 1)
end
end
chunk = chunk..'return overflow ~= nil and error("Overflow.") or t end'
local f = loadstring(chunk)
setfenv(f, M)
return f()
local f = loadstring(chunk); setfenv(f, M); return f()
end
public(); S, A, A0, T = insert(SET), insert(ARRAY), insert(ARRAY0), insert(TABLE)
end
--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
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)}
end
+49 -65
View File
@@ -1,20 +1,19 @@
if module then return end
local type, getmetatable, setmetatable, setfenv, unpack, next, _G = type, getmetatable, setmetatable, setfenv, unpack, next, getfenv(0)
local setfenv, getmetatable, setmetatable, type, unpack, next, _G = setfenv, getmetatable, setmetatable, type, unpack, next, getfenv(0)
local PUBLIC, PRIVATE = 1, 2
local CALL, INDEX, NEWINDEX = 1, 2, 3
local FIELD, INDEX, NEWINDEX = 1, 2, 3
do
local function print(msg) DEFAULT_CHAT_FRAME:AddMessage(RED_FONT_COLOR_CODE..msg) end
p = setmetatable({}, {
local inspect
do local function print(msg) DEFAULT_CHAT_FRAME:AddMessage(RED_FONT_COLOR_CODE..msg) end
inspect = setmetatable({}, {
__call=function(_, ...)
for i = 1, arg.n do
if type(arg[i]) == 'table' then
print('arg'..i..' = {')
for k, v in arg[i] do print(format(' %s: %s = %s', type(k) == 'string' and k or '['..tostring(k)..']', type(v), tostring(v))) end
print('arg'..i..' = '..(type(arg[i]) == 'string' and '"'..arg[i]..'"' or tostring(arg[i])))
if type(arg[i]) == 'table' and next(arg[i]) then
print('{')
for k, v in arg[i] do print(' '..(type(k) == 'string' and k or '['..tostring(k)..']')..' = '..(type(v) == 'string' and '"'..v..'"' or tostring(v))) end
print('}')
else
print(format('arg%d: %s = %s', i, type(arg[i]), tostring(arg[i])))
end
end
return unpack(arg)
@@ -22,6 +21,7 @@ do
__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
@@ -29,9 +29,9 @@ local function declaration_error() error('Declaration error.') end
local function collision_error(key) error('"%s" already exists.', key) end
local function nil_error(key) if key then error('"%s" is nil.', key) else error('Callee is nil') end end
local nop, const = function() end, function(v) return function() return v end end
local nop = function() end
local function prototype() local eq = const(true)
local function prototype() local eq = function() return true end
return setmetatable({__metatable=false, __eq=eq}, {__metatable=false, __eq=eq, __call=function(self, t) return setmetatable(t, self) end})
end
@@ -40,38 +40,26 @@ local INTERFACE, ENVIRONMENT, DECLARATOR = prototype(), prototype(), prototype()
local state = {}
function INTERFACE:__index(key) self=state[self]
if self.access[key] == PUBLIC then
local getter = self[INDEX][key]
if getter then return getter() else return self[CALL][key] end
end
return (self.access[key] == PUBLIC or nil) and self[FIELD][key] or (self[INDEX][key] or nop)()
end
function INTERFACE:__newindex(key, value) self=state[self]
if self.access[key] == PUBLIC then (self[NEWINDEX][key] or nop)(value) end
return (self.access[key] == PUBLIC or nil) and (self[NEWINDEX][key] or nop)(value)
end
function ENVIRONMENT:__index(key) self=state[self]
local getter = self[INDEX][key]
if getter then return getter() end
return self[CALL][key] or _G[key] or self.declarator[key]
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])
end
function ENVIRONMENT:__newindex(key, value) self=state[self]
if self.access[key] then
local setter = self[NEWINDEX][key] or collision_error(key)
setter(value)
else
self.declarator[key] = value
end
if self.access[key] then (self[NEWINDEX][key] or collision_error(key))(value) else self.declarator[key] = value end
end
ENVIRONMENT.__call = nop -- TODO
do
local ACCESS, EVENT = {public=PUBLIC, private=PRIVATE}, {call=CALL, get=INDEX, set=NEWINDEX}
local function declare(self, access, name, handlers)
self.access[name] = self.access[name] and collision_error(name) or access or self.default_access
for event, handler in handlers do
if type(handler) ~= 'function' and (event == INDEX or declaration_error()) then
handler = const(handler)
end
self[event][name] = handler
local ACCESS, EVENT = {public=PUBLIC, private=PRIVATE}, {value=FIELD, get=INDEX, set=NEWINDEX}
local function declare(self, access, name, value)
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
end
end
function DECLARATOR:__index(key) self=state[self]; local name, access = self.declaration_name, self.declaration_access
@@ -82,50 +70,46 @@ do
end
return self.declarator
end
function DECLARATOR:__newindex(key, value) self=state[self]
local name, event = self.declaration_name, nil
if name then
event = EVENT[key] or nil_error(name)
else
name, event = key, type(value) == 'function' and CALL or INDEX
end
declare(self, self.declaration_access, name, {[event]=value})
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?
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 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
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})
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
end
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[CALL][k], self[INDEX][k], self[NEWINDEX][k] = PRIVATE, module[CALL][k], module[INDEX][k], module[NEWINDEX][k]
self.access[k], self[FIELD][k], self[INDEX][k], self[NEWINDEX][k] = PRIVATE, module[FIELD][k], module[INDEX][k], module[NEWINDEX][k]
end
end
end
function module(name)
if name and _G[name] then return true end
local interface, env, declarator = INTERFACE {}, ENVIRONMENT {}, DECLARATOR {}
local self; self = {
access = {p=PRIVATE, _=PRIVATE, error=PRIVATE, nop=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, public=PRIVATE, private=PRIVATE},
[CALL] = {p=p, import=function(interface) import(self, interface) end, error=error, nop=nop},
[INDEX] = {_G=const(_G), M=const(env), I=const(interface), public=function() return declarator.public end, private=function() return declarator.private end},
[NEWINDEX] = {p=p, _=nop},
declarator = declarator,
default_access = PRIVATE,
}
state[interface], state[env], state[declarator] = self, self, self
if name then _G[name] = interface end
setfenv(2, env)
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},
[FIELD] = {_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,
})