refactoring
This commit is contained in:
+11
-10
@@ -2,7 +2,7 @@ if getmetatable(getfenv(0)) == false then return end
|
|||||||
local tinsert, tremove, getn, setn, strfind, type, setmetatable, setfenv, _G = tinsert, tremove, getn, table.setn, strfind, type, setmetatable, setfenv, getfenv(0)
|
local tinsert, tremove, getn, setn, strfind, type, setmetatable, setfenv, _G = tinsert, tremove, getn, table.setn, strfind, type, setmetatable, setfenv, getfenv(0)
|
||||||
|
|
||||||
local PRIVATE, PUBLIC, FIELD, ACCESSOR, MUTATOR = 0, 1, 2, 4, 6
|
local PRIVATE, PUBLIC, FIELD, ACCESSOR, MUTATOR = 0, 1, 2, 4, 6
|
||||||
local TYPES = { FIELD, ACCESSOR, MUTATOR }
|
local MODES = { FIELD, ACCESSOR, MUTATOR }
|
||||||
local READ, WRITE = '', '='
|
local READ, WRITE = '', '='
|
||||||
local OPERATION = { [FIELD]=READ, [ACCESSOR]=READ, [MUTATOR]=WRITE }
|
local OPERATION = { [FIELD]=READ, [ACCESSOR]=READ, [MUTATOR]=WRITE }
|
||||||
local META = { get=ACCESSOR, set=MUTATOR }
|
local META = { get=ACCESSOR, set=MUTATOR }
|
||||||
@@ -25,21 +25,22 @@ function definition_helper_mt:__index(k)
|
|||||||
_name[self] = _name[self] and error('Invalid modifier "%s".', k) or k
|
_name[self] = _name[self] and error('Invalid modifier "%s".', k) or k
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
function definition_helper_mt:__newindex(k, v) local module = _module[self]
|
function definition_helper_mt:__newindex(k, v)
|
||||||
local name = META[k] and (_name[self] or error'Definition missing identifier.') or k
|
local module, name, mode
|
||||||
|
module, name = _module[self], _name[self]
|
||||||
|
if name then mode = META[k] or error('Invalid modifier "%s"', k) else name, mode = k, FIELD end
|
||||||
if type(name) ~= 'string' or not strfind(name, '^[_%a][_%w]*') then error('Invalid identifier "%s".', name) end
|
if type(name) ~= 'string' or not strfind(name, '^[_%a][_%w]*') then error('Invalid identifier "%s".', name) end
|
||||||
local type = META[k] or FIELD
|
module.defined[name .. OPERATION[mode]] = module.defined[name .. OPERATION[mode]] and error('Duplicate identifier "%s".', name) or true
|
||||||
module.defined[name .. OPERATION[type]] = module.defined[name .. OPERATION[type]] and error('Duplicate identifier "%s".', name) or true
|
module[mode][name], module[_access[self]+mode][name] = v, v
|
||||||
module[type][name], module[_access[self]+type][name] = v, v
|
|
||||||
_access[self], _name[self] = nil, nil
|
_access[self], _name[self] = nil, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local function include(self, interface)
|
local function include(self, interface)
|
||||||
local module = (interface < INTERFACE or error'Import error.') and _module[interface]
|
local module = (interface < INTERFACE or error'Import error.') and _module[interface]
|
||||||
for _, type in TYPES do
|
for _, mode in MODES do
|
||||||
for k, v in module[PUBLIC+type] do
|
for k, v in module[PUBLIC+mode] do
|
||||||
if not self.defined[k .. OPERATION[type]] then
|
if not self.defined[k .. OPERATION[mode]] then
|
||||||
self.defined[k .. OPERATION[type]], self[type][k] = true, v
|
self.defined[k .. OPERATION[mode]], self[mode][k] = true, v
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user