refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-25 19:25:11 +02:00
parent db41eebbf2
commit fcd563fa76
2 changed files with 43 additions and 22 deletions
+2 -1
View File
@@ -34,7 +34,8 @@ do
return t return t
end end
function public.recycle(t) function public.recycle(t)
wipe(t) while getn(t) > 0 do tremove(t) end -- TODO or is setn enough?
if next(t) then return end -- no hashtables, they can't be shrunk -- TODO "n" field?
if getn(pool) < 50 then if getn(pool) < 50 then
tinsert(pool, t) tinsert(pool, t)
else else
+41 -21
View File
@@ -2,6 +2,17 @@ local type, setmetatable, setfenv, unpack, next, mask, combine, pcall, _G = type
local error, import_error, declaration_error, collision_error, mutability_error local error, import_error, declaration_error, collision_error, mutability_error
local pass, env_mt, interface_mt, declarator_mt, importer_mt local pass, env_mt, interface_mt, declarator_mt, importer_mt
module 'util' (
a,
b,
c
)
import: util (a, b, c)
from 'util' import ()
import 'util' as 'kek'
local PRIVATE, PUBLIC, MUTABLE, DYNAMIC, PROPERTY = 1, 2, 4, 8, 16 local PRIVATE, PUBLIC, MUTABLE, DYNAMIC, PROPERTY = 1, 2, 4, 8, 16
local _state, _modules = {}, {} local _state, _modules = {}, {}
@@ -15,8 +26,9 @@ mutability_error = function(key) error('Field "%s" is immutable.', key) end
pass = function() end pass = function() end
function get() generic_function_mt = {__metatable=false}
setfenv() function generic_function_mt:__call()
end end
do do
@@ -36,29 +48,33 @@ do
state[self] = key; return self state[self] = key; return self
end end
function importer_mt:__call(arg1, arg2, state) state=_state[self] function importer_mt:__call(arg1, arg2, state) state=_state[self]
local name, alias, module; name = arg2 or arg1 local name, alias, module; name = arg2 or arg1 or import_error()
alias, state[self] = state[self] or name, nil alias, state[self] = state[self] or name, nil
return pcall(import, state, _modules[name], alias) and self or import_error() return pcall(import, state, _modules[name], alias) and self or import_error()
end end
end end
do do
local declare local function dynamize(f)
do return function(...) setfenv(f, getfenv(2)); return f(unpack(arg)) end
local function unpack_property_value(t) end
local get, set; get, set, t.get, t.set = t.get or pass, t.set or pass, nil, nil local function unpack_property_value(t)
if next(t) or type(get) ~= 'function' or type(set) ~= 'function' then error() end local get, set; get, set, t.get, t.set = t.get or pass, t.set or pass, nil, nil
return get, set if next(t) or type(get) ~= 'function' or type(set) ~= 'function' then error() end
end return get, set
function declare(state, modifiers, key, value) end
local metadata = state.metadata local function declare(state, modifiers, key, value)
metadata[key] = metadata[key] and collision_error(key) or modifiers local metadata, dynamic, property = state.metadata, mask(DYNAMIC, modifiers) ~= 0, mask(PROPERTY, modifiers) ~= 0
if mask(PROPERTY, modifiers) == 0 then metadata[key] = metadata[key] and collision_error(key) or modifiers
state.data[key] = value if property then
else local success, getter, setter = pcall(unpack_property_value, value)
local success, getter, setter = pcall(unpack_property_value, value) if success or declaration_error() then
if success or declaration_error() then state.getters[key], state.setters[key] = getter, setter end if dynamic then getter, setter = dynamize(getter), dynamize(setter) end
state.getters[key], state.setters[key] = getter, setter
end end
else
if dynamic and (type(value) == 'function' or declaration_error()) then value = dynamize(value) end
state.data[key] = value
end end
end end
@@ -89,6 +105,7 @@ do
env_mt = {__metatable=false} env_mt = {__metatable=false}
function env_mt:__index(key, state) state=_state[self] function env_mt:__index(key, state) state=_state[self]
if state.intercept_index(key) then return end
if mask(PROPERTY, state.metadata[key] or 0) ~= 0 then if mask(PROPERTY, state.metadata[key] or 0) ~= 0 then
return state.getters[key]() return state.getters[key]()
else else
@@ -97,6 +114,7 @@ do
end end
end end
function env_mt:__newindex(key, value, state) state=_state[self] function env_mt:__newindex(key, value, state) state=_state[self]
if state.intercept_newindex(key, value) then return end
local modifiers = state.metadata[key] local modifiers = state.metadata[key]
if modifiers then if modifiers then
if mask(PROPERTY, modifiers) ~= 0 then if mask(PROPERTY, modifiers) ~= 0 then
@@ -108,6 +126,9 @@ do
declare(state, state.modifiers, key, value) declare(state, state.modifiers, key, value)
end end
end end
function env_mt:__call(key, ...)
_state[self].intercept_call(key, arg)
end
end end
interface_mt = {__metatable=false} interface_mt = {__metatable=false}
@@ -132,9 +153,8 @@ end
function module(name) function module(name)
if not _modules[name] then if not _modules[name] then
local state, env, interface, declarator, importer local env, interface, declarator, importer = setmetatable({}, env_mt), setmetatable({}, interface_mt), setmetatable({}, declarator_mt), setmetatable({}, importer_mt)
env, interface, declarator, importer = setmetatable({}, env_mt), setmetatable({}, interface_mt), setmetatable({}, declarator_mt), setmetatable({}, importer_mt) local state = {
state = {
env = env, env = env,
interface = interface, interface = interface,
modifiers = PRIVATE, modifiers = PRIVATE,