working on module refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-10-09 15:25:12 +02:00
parent 867f12d66e
commit f8e64deeed
17 changed files with 757 additions and 799 deletions
+14 -13
View File
@@ -15,7 +15,7 @@ local function proxy_mt(fields, mutators)
return { __metatable=false, __index=fields, __newindex=function(_, k, v) return mutators[k](v) end }
end
local loaded, _module, _access, _name = {}, {}, {}, {}
local loaded, _interface, _environment, _access, _name = {}, {}, {}, {}, {}
local definition_helper_mt = { __metatable=false }
function definition_helper_mt:__index(k)
@@ -24,7 +24,7 @@ function definition_helper_mt:__index(k)
end
function definition_helper_mt:__newindex(k, v)
local module, name, mode
module, name = _module[self], _name[self]
module, name = loaded[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
module.defined[name .. OPERATION[mode]] = module.defined[name .. OPERATION[mode]] and error('Duplicate identifier "%s".', name) or true
@@ -32,10 +32,10 @@ function definition_helper_mt:__newindex(k, v)
_access[self], _name[self] = nil, nil
end
local function require(name) return loaded[name] end
local function require(name) return _interface[name] end
local function include(self, name)
local module = name and _module[name] or error('No module "%s".', name)
local module = name and loaded[name] or error('No module "%s".', name)
for _, mode in MODES do
for k, v in module[PUBLIC + mode] do
if not self.defined[k .. OPERATION[mode]] or error('Import conflict for "%s".', k) then
@@ -47,14 +47,13 @@ end
local nop_default_mt = { __index=function() return nop end }
function module(name)
-- if loaded[name] then _G.error(nil) end
local module, environment, interface, definition_helper, modifiers, accessors, mutators, fields, public_accessors, public_mutators, public_fields
local function create_module(name)
local P, environment, interface, definition_helper, modifiers, accessors, mutators, fields, public_accessors, public_mutators, public_fields
environment, interface, definition_helper = {}, {}, setmetatable({}, definition_helper_mt)
accessors = { private=function() _access[definition_helper] = PRIVATE; return definition_helper end, public=function() _access[definition_helper] = PUBLIC; return definition_helper end }
mutators = setmetatable({ _=nop }, { __index=function(_, k) return function(v) _G[k] = v end end })
fields = setmetatable(
{ _M=environment, _G=_G, require=require, include=function(interface) include(module, interface) end, error=error, nop=nop, id=id },
{ _M=environment, _G=_G, require=require, include=function(interface) include(P, interface) end, error=error, nop=nop, id=id },
{ __index=function(_, k) local accessor = accessors[k]; if accessor then return accessor() else return _G[k] end end }
)
public_accessors = setmetatable({}, nop_default_mt)
@@ -62,7 +61,7 @@ function module(name)
public_fields = setmetatable({}, { __index=function(_, k) return public_accessors[k]() end })
setmetatable(environment, proxy_mt(fields, mutators))
setmetatable(interface, proxy_mt(public_fields, public_mutators))
module = {
P = {
defined = { _M=true, _G=true, require=true, include=true, error=true, nop=true, id=true, public=true, private=true, ['_=']=true },
[ACCESSOR] = accessors,
[MUTATOR] = mutators,
@@ -71,7 +70,9 @@ function module(name)
[PUBLIC + MUTATOR] = public_mutators,
[PUBLIC + FIELD] = public_fields,
}
_module[name], _module[definition_helper] = module, module
loaded[name] = interface
setfenv(2, environment)
end
_environment[name], _interface[name] = environment, interface
loaded[name], loaded[definition_helper] = P, P
end
function module(name) if not loaded[name] then create_module(name) end setfenv(2, _environment[name]) end
function library(name) if loaded[name] then _G.error(nil) end create_module(name) setfenv(2, _environment[name]) end