From a49e9f07bc885736d63b2f561d29b72785b74f7c Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Mon, 5 Sep 2016 11:53:46 +0200 Subject: [PATCH] refactoring --- core/scan.lua | 3 +- libs/green_t.lua | 125 ++++++++++++++++++++++++++++------------------- 2 files changed, 75 insertions(+), 53 deletions(-) diff --git a/core/scan.lua b/core/scan.lua index edceb15..29c106c 100644 --- a/core/scan.lua +++ b/core/scan.lua @@ -38,8 +38,7 @@ do end function private.state.get() - local _, state = next(filter(copy(scan_states), function(state) return state.id == thread_id end)) - return state + for _, state in scan_states do if state.id == thread_id then return state end end end end diff --git a/libs/green_t.lua b/libs/green_t.lua index 7b10a45..3960e7a 100644 --- a/libs/green_t.lua +++ b/libs/green_t.lua @@ -64,12 +64,12 @@ do local f = function(_, v) if type(v) == 'table' then auto_release[v] = nil en public.perm {get=function() return setmetatable(acquire_temp(), mt) end, set=function(t) auto_release[t] = nil end} 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 -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 +--end local function arg_chunk(k, n) k = k or 1 @@ -79,6 +79,65 @@ local function arg_chunk(k, n) return str end + + +--local function insert_chunk(mode) +-- local body = 'repeat ' +-- if mode == 'k' then +-- for i = 2, 99 do +-- body = body .. format('if a%1$d == nil then break end; a1[a%1$d] = true;', i) +-- end +-- elseif mode == 'v' then +-- body = body .. 'if a2 == nil then break end; a1[1] = a2;' +-- for i = 3, 99 do +-- body = body .. format('if a%1$d == nil then setn(a1, %d); break end; a1[%d] = a%1$d;', i, i-2, i-1) +-- end +-- elseif mode == 'v0' then +-- body = body .. 'setn(a1, 98);' +-- for i = 2, 99 do +-- body = body .. format('a1[%d] = a%d;', i-1, i) +-- end +-- elseif mode == 'kv' then +-- for i = 2, 98, 2 do +-- 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 false;' +--end + +do local function constructor(type) + local chunk = 'local setn, error = table.setn, error; return function(' + for i = 1, 99 do chunk = chunk .. 'a' .. i..',' end + chunk = chunk .. 'overflow) local t = t' + if type == 'set' then + for i = 1, 99 do chunk = format('%s; if a%d == nil then return t end; t[a%d] = true', chunk, 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 chunk = format('%s; if a%d == nil then setn(t, %d); return t end; t[%d] = a%d', chunk, i, i - 1, i, i) end + elseif type == 'array0' then + chunk = chunk .. '; setn(t, 99)' + for i = 1, 99 do chunk = format('%s; t[%d] = a%d', chunk, i, i) end + elseif type == 'table' then + for i = 1, 97, 2 do chunk = format('%s; if a%d == nil then return t end; t[a%d] = a%d', chunk, i, i, i + 1) end + end + chunk = chunk .. '; return (overflow == nil or error "Overflow.") and t end' + local f = loadstring(chunk); setfenv(f, M); return f() +end +public(); S, A, A0, T = constructor'set', constructor'array', constructor 'array0', constructor 'table' +end +--do +-- local function pseudo_literal(mode) +-- local upvals = {setmetatable=setmetatable, setn=table.setn, error=error} +-- 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' +--end + + function public.pseudo_vararg_function(body, upvals) local upval_chunk = '' for k in upvals or empty() do @@ -86,54 +145,18 @@ function public.pseudo_vararg_function(body, upvals) end local f = loadstring(format('%s return function(%s) %s end', upval_chunk, arg_chunk(), body)) or error() setfenv(f, upvals or empty()) - return (f or error())() -end - -local function insert_chunk(mode) - local body = 'repeat ' - if mode == 'k' then - for i = 2, 99 do - body = body .. format('if a%1$d == nil then break end; a1[a%1$d] = true;', i) - end - elseif mode == 'v' then - body = body .. 'if a2 == nil then break end; a1[1] = a2;' - for i = 3, 99 do - body = body .. format('if a%1$d == nil then setn(a1, %d); break end; a1[%d] = a%1$d;', i, i-2, i-1) - end - elseif mode == 'v0' then - body = body .. 'setn(a1, 98);' - for i = 2, 99 do - body = body .. format('a1[%d] = a%d;', i-1, i) - end - elseif mode == 'kv' then - for i = 2, 98, 2 do - 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 false;' + return f() end do - local function pseudo_literal(mode) - local upvals = {setmetatable=setmetatable, setn=table.setn, error=error} - local mt = {__call = pseudo_vararg_function(insert_chunk(mode) .. 'setmetatable(a1, nil); return a1', upvals)} - return function() return setmetatable(acquire(), mt) end + local body = '' + for i = 2, 100 do + body = body .. format('if a1 == %d then return %s end;', i, arg_chunk(i-1, 100)) 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.select = pseudo_vararg_function(body) end ---do --- local body = '' --- for i = 2, 100 do --- body = body .. format('if a1 == %d then return %s end;', i, arg_chunk(i, 100)) --- end --- public.select = pseudo_vararg_function(body) ---end - -function public.select(i, ...) temp=arg - while i > 1 do i = i - 1; tremove(arg, i) end - return tremove(arg, 1), unpack(arg) -end \ No newline at end of file +--function public.select(i, ...) temp=arg +-- while i > 1 do i = i - 1; tremove(arg, i) end +-- return tremove(arg, 1), unpack(arg) +--end \ No newline at end of file