big refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-21 12:35:28 +02:00
parent c08c990ff3
commit e3ed0367ec
+26 -17
View File
@@ -1,8 +1,8 @@
local type, setmetatable, setfenv, unpack, mask, g = type, setmetatable, setfenv, unpack, bit.band, getfenv(0)
local PRIVATE, PUBLIC, GETTER, SETTER, MUTABLE = 0, 1, 2, 4, 8
local MODIFIER = {private=PRIVATE, public=PUBLIC, getter=GETTER, setter=SETTER, mutable=MUTABLE}
local MASK = {private=MUTABLE+GETTER+SETTER, public=MUTABLE+GETTER+SETTER, getter=PRIVATE+PUBLIC+SETTER, setter=PRIVATE+PUBLIC+GETTER, mutable=PRIVATE+PUBLIC}
local import, initialize_declarator, error, modules_mt, module_mt, env_mt, interface_mt, declarator_mt
local PRIVATE, PUBLIC, MUTABLE, PROPERTY = 0, 1, 2, 4
local MODIFIER = {private=PRIVATE, public=PUBLIC, mutable=MUTABLE, property=PROPERTY}
local MODIFIER_MASK, PROPERTY_MASK = {private=MUTABLE+PROPERTY, public=MUTABLE+PROPERTY, mutable=PRIVATE+PUBLIC}, PRIVATE+PUBLIC
local import, error, define_property, modules_mt, module_mt, env_mt, interface_mt, declarator_mt
local _state, _lock_metatables = {}, {}
function error(message, ...) g.error(format(message, unpack(arg))..'\n'..debugstack(3, 5, 0), 3) end
function import(imports, t) for k, v in t do imports[type(k) == 'number' and v or k] = v end end
@@ -12,24 +12,33 @@ function import(imports, t) for k, v in t do imports[type(k) == 'number' and v o
-- return mt
--end
declarator_mt = {__metatable=false}
do
local function define_property(self, key, t)
local state, getter, setter = _state[self], t.get, t.set
state.metadata[key] = mask(PRIVATE+PUBLIC, state.modifiers)
+ (getter ~= nil and (type(getter) == 'function' or error('Getter must be a function.')) and GETTER or 0)
+ (setter ~= nil and (type(setter) == 'function' or error('Setter must be function.')) and SETTER or 0)
state.getters[key], state.setters[key] = getter, setter
function declarator_mt.__call(self, t)
local state = _state[self]
local key = state.property
if not key then return end
for k, v in t do
if k == 'get' then
state.getters[key] = type(v) == 'function' and v or error('Getter must be function.')
elseif k == 'set' or error('Malformed property definition.') then
state.setters[key] = type(v) == 'function' and v or error('Setter must be function.')
end
end
function declarator_mt.__index(self, key)
local state, modifier = _state[self], MODIFIER[key]
if not modifier then return function(t) define_property(self, key, t) end end
state.modifiers = modifier + mask(MASK[key], state.modifiers)
end
function declarator_mt.__index(self, key)
local state, modifier = _state[self], MODIFIER[key]
if modifier then
state.modifiers = modifier + mask(MODIFIER_MASK[key], state.modifiers)
return self
else
state.modifiers = PROPERTY + mask(PROPERTY_MASK, state.modifiers)
state.property = key
return self
end
end
function declarator_mt.__newindex(self, key, value)
local state, modifiers
local state = _state[self]
local modifiers = state.modifiers
modifiers, state.modifiers = state.modifiers, PRIVATE
if modifiers then error('Field "%s" already exists.', key) end
state.metadata[key] = modifiers
if mask(GETTER+SETTER, modifiers) == 0 then
@@ -79,7 +88,7 @@ function g.aux_module(name)
local state, declarator, env, interface, imports
env, interface, declarator, imports = setmetatable({}, env_mt), setmetatable({}, interface_mt), setmetatable({}, declarator_mt), {}
state = {
name = name, env = env, interface = interface, declarator = declarator, imports = {}, declarator_state = 0,
name = name, env = env, interface = interface, declarator = declarator, imports = {}, declarator_state = PRIVATE,
metadata={_g=PRIVATE, _m=PRIVATE, _i=PRIVATE, import=PRIVATE, private=GETTER, public=GETTER, getter=GETTER, setter=GETTER, mutable=GETTER},
data = {_g=g, _m=env, _i=interface, import=function(t) import(imports, t) end},
getters = {