From 00a242e0e2f270086ee26e2682a83a181402c16e Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Tue, 6 Sep 2016 04:48:17 +0200 Subject: [PATCH] refactoring --- gui/core.lua | 6 ++-- libs/green_t.lua | 84 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 60 insertions(+), 30 deletions(-) diff --git a/gui/core.lua b/gui/core.lua index 10fb81f..526e8a3 100644 --- a/gui/core.lua +++ b/gui/core.lua @@ -302,10 +302,10 @@ function public.editbox(parent) editbox:SetTextInsets(1.5, 1.5, 3, 3) editbox:SetMaxLetters(nil) editbox:SetHeight(24) --- editbox:SetTextColor(0, 0, 0, 0) + editbox:SetTextColor(0, 0, 0, 0) set_content_style(editbox) - local function colorize() end -- this.display:SetText((this.colorizer or id)(this:GetText())) end - local function format() end-- this.display:SetText((this.formatter or this.colorizer or id)(this:GetText())) end + local function colorize() this.display:SetText((this.colorizer or id)(this:GetText())) end + local function format() this.display:SetText((this.formatter or this.colorizer or id)(this:GetText())) end editbox:SetScript('OnEscapePressed', function() this:ClearFocus() ;(this.escape or nop)() diff --git a/libs/green_t.lua b/libs/green_t.lua index 8cdd5bd..a3932c6 100644 --- a/libs/green_t.lua +++ b/libs/green_t.lua @@ -2,7 +2,7 @@ green_t = module --setglobal('green_t', green_t and error(nil) or module) local next, setn, type, setmetatable = next, table.setn, type, setmetatable -local wipe, release, acquire, acquire_temp, empty +local wipe, release, acquire, acquire_auto, empty -- TODO mandatory operation table mandate + comply/fulfill functions local pool, pool_size, overflow_pool, auto_release = {}, 0, setmetatable({}, { __mode='k' }), {} @@ -33,14 +33,6 @@ function release(t) end public.release = release -function public.ret(t) - if getn(t) > 0 then - return tremove(t, 1), ret(t) - else - release(t) - end -end - function acquire() if pool_size > 0 then pool_size = pool_size - 1 @@ -55,36 +47,47 @@ function acquire() end public.t.get = acquire -function acquire_temp() +function acquire_auto() local t = acquire() auto_release[t] = true return t end -public.tt.get = acquire_temp +public.tt.get = acquire_auto + +function public.ret(t) + if getn(t) > 0 then + return tremove(t, 1), ret(t) + else + release(t) + end +end do local mt = { __newindex=nop } - function empty() return setmetatable(acquire_temp(), mt) end + function empty() return setmetatable(acquire_auto(), mt) end public.empty.get = empty end do - local f = function(_, v) if type(v) == 'table' then auto_release[v] = true end return v end - local mt = { __call=f, __sub=f } - public.temp {get=function() return setmetatable(acquire_temp(), mt) end, set=function(t) auto_release[t] = true end} -end -do - local f = function(_, v) if type(v) == 'table' then auto_release[v] = nil end return v end - local mt = { __call=f, __sub=f } - public.perm { get=function() return setmetatable(acquire_temp(), mt) end, set=function(t) auto_release[t] = nil end } + local function apply(v, enable) + if type(v) ~= 'table' then return end + auto_release[v] = enable and true or nil + end + local function define_modifier(name, enable) + local f = function(_, v) apply(v, enable); return v end + local mt = { __call=f, __sub=f } + public[name] { get=function() return setmetatable(acquire_auto(), mt) end, set=function(v) apply(v, enable) end } + end + define_modifier('temp', true) + define_modifier('perm', false) end do local mt, key = {}, nil function mt:__unm() local temp = mt.__index; mt.__index = nil; return temp end function mt:__index(k) key = k; return self end - function mt:__call(v) self[key] = v; key = nil return self end - function public.__(t) mt.__newindex = wipe(t); return setmetatable(acquire_temp(), mt) end + function mt:__call(v) self[key] = v; key = nil; return self end + function public.__(t) mt.__newindex = wipe(t); return setmetatable(acquire_auto(), mt) end end local function arg_chunk(k, n) @@ -105,6 +108,33 @@ function public.pseudo_vararg_function(body, upvals) return f() end +do + local body = [[ + if arg100 ~= nil then error("Vararg overflow.") end + local n + repeat + ]] + for i = 99, 1, -1 do + body = body .. format('if a%1$d ~= nil then n = %1$d; break end;', i) + end + body = body .. [[ + until true + local t = acquire_auto() + setn(t, n) + repeat + ]] + for i = 1, 99 do + body = body .. format('if %1$d > n then break end; t[%1$d] = a%1$d;', i) + end + body = body .. [[ + until true + return f(t) + ]] + function public.vararg(f) + return pseudo_vararg_function(body, {f=f, error=error, setn=setn, acquire=acquire_auto, release=release}) + end +end + local function insert_chunk(mode) local body = 'repeat ' if mode == 'k' then @@ -126,7 +156,7 @@ local function insert_chunk(mode) body = body .. format('if a%1$d == nil then break end; a1[a%1$d] = a%d;', i, i+1) end end - return body .. 'if a100 ~= nil then error"Overflow." end until true;' + return body .. 'if a100 ~= nil then error("Vararg overflow.") end until true;' end do @@ -135,10 +165,10 @@ do local mt = {__call = pseudo_vararg_function(insert_chunk(mode) .. 'setmetatable(a1, nil); return a1', upvals)} return function() return setmetatable(acquire(), mt) end end - public.S.get = pseudo_literal'k' - public.A.get = pseudo_literal'v' - public.A0.get = pseudo_literal'v0' - public.T.get = pseudo_literal'kv' + public.S.get = pseudo_literal('k') + public.A.get = pseudo_literal('v') + public.A0.get = pseudo_literal('v0') + public.T.get = pseudo_literal('kv') end do