refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-26 05:14:43 +02:00
parent 8618798480
commit 8a29c726aa
+18 -22
View File
@@ -1,8 +1,8 @@
local type, setmetatable, getfenv, setfenv, unpack, next, mask, combine, pcall, _G = type, setmetatable, getfenv, setfenv, unpack, next, bit.band, bit.bor, pcall, getfenv(0) local type, setmetatable, setfenv, unpack, next, mask, combine, 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 error, import_error, declaration_error, collision_error, mutability_error
local pass, start_declaration, advance_declaration, env_mt, interface_mt, metadata_mt, property_mt local pass, start_declaration, advance_declaration, env_mt, interface_mt, metadata_mt, property_mt
local NULL, PRIVATE, PUBLIC, MUTABLE, PROPERTY = 0, 1, 2, 4, 8 local PRIVATE, PUBLIC, MUTABLE, PROPERTY = 0, 1, 2, 4
local INDEX, NEWINDEX, CALL = 1, 2, 4 local INDEX, NEWINDEX, CALL = 1, 2, 4
local _state = {} local _state = {}
@@ -37,7 +37,7 @@ do
do do
local MODIFIER = {private=PRIVATE, public=PUBLIC, mutable=MUTABLE, property=PROPERTY} local MODIFIER = {private=PRIVATE, public=PUBLIC, mutable=MUTABLE, property=PROPERTY}
local COMPATIBLE = {private=MUTABLE+PROPERTY, public=PROPERTY, mutable=PRIVATE, property=PRIVATE+PUBLIC} local COMPATIBLE = {private=MUTABLE+PROPERTY, public=PROPERTY, mutable=PRIVATE, property=PUBLIC}
local module, modifiers, property local module, modifiers, property
local function intercept(_, key, value) local function intercept(_, key, value)
if type == INDEX and (not property or declaration_error()) then if type == INDEX and (not property or declaration_error()) then
@@ -66,7 +66,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 self end if advance_declaration(INDEX, key) then return self 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
@@ -75,7 +75,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 if advance_declaration(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
@@ -88,7 +88,7 @@ do
end end
end end
function env_mt:__call(key, ...) function env_mt:__call(key, ...)
_state[self].intercept(CALL, key, arg) advance_declaration(CALL, key, arg)
end end
end end
@@ -102,30 +102,26 @@ function interface_mt:__index(key, state) state=_state[self]
end end
end end
function interface_mt:__newindex(key, value, state) state=_state[self] function interface_mt:__newindex(key, value, state) state=_state[self]
local modifiers = state.metadata[key] if mask(PUBLIC+PROPERTY, state.metadata[key] or 0) == PUBLIC+PROPERTY then
if modifiers then return state.setters[key](value)
if mask(PUBLIC+PROPERTY, modifiers) == PUBLIC+PROPERTY then -- elseif masked == PUBLIC and type(state.data[key]) == 'function' then
return state.setters[key](value) -- return state.data[key](value)
elseif mask(PUBLIC, modifiers) ~= 0 then
return state.data[key](value)
end
end end
end end
metadata_mt = {__index=function() return 0 end} property_mt = {__index=function() return pass end}
property_mt = {__index=function() return pass end }
local function create_module(...) local function module(...)
local state, env, interface local state, env, interface
env, interface = setmetatable({}, env_mt), setmetatable({}, interface_mt) env, interface = setmetatable({}, env_mt), setmetatable({}, interface_mt)
state = { state = {
metadata = {_=PROPERTY, _G=PRIVATE, M=PRIVATE, error=PRIVATE, private=PROPERTY, public=PROPERTY, mutable=PROPERTY, property=PROPERTY}, metadata = {_=PROPERTY, _G=PRIVATE, M=PRIVATE, I=PRIVATE, error=PRIVATE, private=PROPERTY, public=PROPERTY, mutable=PROPERTY, property=PROPERTY},
fields = {_G=_G, M=env, I=interface, error=error}, fields = {_G=_G, M=env, I=interface, error=error},
getters = setmetatable({ getters = setmetatable({
private = function() start_declaration(PRIVATE, state); return getfenv(2) end, private = function() start_declaration(PRIVATE, state); return env end,
public = function() start_declaration(PUBLIC, state); return getfenv(2) end, public = function() start_declaration(PUBLIC, state); return env end,
mutable = function() start_declaration(MUTABLE, state); return getfenv(2) end, mutable = function() start_declaration(MUTABLE, state); return env end,
property = function() start_declaration(PROPERTY, state); return getfenv(2) end, property = function() start_declaration(PROPERTY, state); return env end,
}, property_mt), }, property_mt),
setters = setmetatable({}, property_mt), setters = setmetatable({}, property_mt),
} }
@@ -138,5 +134,5 @@ local function create_module(...)
end end
end end
state[env], state[interface] = state, state state[env], state[interface] = state, state
return env, interface setfenv(2, env)
end end