working on module refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-10-08 19:11:23 +02:00
parent ab475bb503
commit 86c107efba
5 changed files with 58 additions and 60 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
aux = module module 'aux'
include (green_t) use 'green_t'
--aux_account_settings = {} -- TODO clean up the mess of savedvariables --aux_account_settings = {} -- TODO clean up the mess of savedvariables
--aux_character_settings = {} --aux_character_settings = {}
+4 -4
View File
@@ -1,8 +1,8 @@
aux_control = module module 'aux_control'
include (green_t) use 'green_t'
include (aux) use 'aux'
include (aux_util) use 'aux_util'
local event_frame = CreateFrame('Frame') local event_frame = CreateFrame('Frame')
+8 -6
View File
@@ -1,10 +1,12 @@
aux_cache = module module 'aux_cache'
include (green_t) import 'aux.*'
include (aux) import 'aux_util.*'
include (aux_util) import 'aux_control.*'
include (aux_control) import 'aux_util_color.*'
include (aux_util_color)
import 'aux_info'
import 'aux_persistence'
local info, persistence = aux_info, aux_persistence local info, persistence = aux_info, aux_persistence
+7 -1
View File
@@ -1,4 +1,10 @@
aux_frame = module module 'aux.frame'
use 'aux.frame'
use 'aux_init'
local gui = aux_gui local gui = aux_gui
+37 -47
View File
@@ -1,4 +1,4 @@
if getmetatable(getfenv(0)) == false then return end if module then return end
local strfind, type, setmetatable, setfenv, _G = strfind, type, setmetatable, setfenv, getfenv(0) local strfind, type, setmetatable, setfenv, _G = 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
@@ -11,14 +11,11 @@ local function error(msg, ...) return _G.error(format(msg or '', unpack(arg)) ..
local nop, id = function() end, function(v) return v end local nop, id = function() end, function(v) return v end
local interface_lt = function() return true end local function proxy_mt(fields, mutators)
local INTERFACE = setmetatable({}, { __lt=interface_lt }) return { __metatable=false, __index=fields, __newindex=function(_, k, v) return mutators[k](v) end }
local function proxy_mt(fields, mutators, lt)
return { __metatable=false, __index=fields, __newindex=function(_, k, v) return mutators[k](v) end, __lt=lt }
end end
local _module, _access, _name = {}, {}, {} local _interface, _module, _access, _name = {}, {}, {}, {}
local definition_helper_mt = { __metatable=false } local definition_helper_mt = { __metatable=false }
function definition_helper_mt:__index(k) function definition_helper_mt:__index(k)
@@ -31,14 +28,15 @@ function definition_helper_mt:__newindex(k, v)
if name then mode = META[k] or error('Invalid modifier "%s"', k) else name, mode = k, FIELD end 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
module.defined[name .. OPERATION[mode]] = module.defined[name .. OPERATION[mode]] and error('Duplicate identifier "%s".', name) or true module.defined[name .. OPERATION[mode]] = module.defined[name .. OPERATION[mode]] and error('Duplicate identifier "%s".', name) or true
module[mode][name], module[_access[self]+mode][name] = v, v module[mode][name], module[_access[self] + mode][name] = v, v
_access[self], _name[self] = nil, nil _access[self], _name[self] = nil, nil
end end
local function include(self, interface) local function import(self, path)
local module = (interface < INTERFACE or error'Include error.') and _module[interface] local _, _, module_name, name = strfind(path, '([^.]*)%.?(.*)')
local module = _module[module_name] or error('No module "%s".', module_name)
for _, mode in MODES do for _, mode in MODES do
for k, v in module[PUBLIC+mode] do for k, v in module[PUBLIC + mode] do
if not self.defined[k .. OPERATION[mode]] then if not self.defined[k .. OPERATION[mode]] then
self.defined[k .. OPERATION[mode]], self[mode][k] = true, v self.defined[k .. OPERATION[mode]], self[mode][k] = true, v
end end
@@ -48,39 +46,31 @@ end
local nop_default_mt = { __index=function() return nop end } local nop_default_mt = { __index=function() return nop end }
local env = setmetatable({}, { function module(name)
__metatable = false, if _interface[name] then _G.error(nil) end
__newindex = function(_, k) local module, environment, interface, definition_helper, modifiers, accessors, mutators, fields, public_accessors, public_mutators, public_fields
if _G[k] then _G.error(nil) end environment, interface, definition_helper = {}, {}, setmetatable({}, definition_helper_mt)
local module, environment, interface, definition_helper, modifiers, accessors, mutators, fields, public_accessors, public_mutators, public_fields accessors = { private=function() _access[definition_helper] = PRIVATE; return definition_helper end, public=function() _access[definition_helper] = PUBLIC; return definition_helper end }
environment, interface, definition_helper = {}, {}, setmetatable({}, definition_helper_mt) mutators = setmetatable({ _=nop }, { __index=function(_, k) return function(v) _G[k] = v end end })
accessors = { private=function() _access[definition_helper] = PRIVATE; return definition_helper end, public=function() _access[definition_helper] = PUBLIC; return definition_helper end } fields = setmetatable(
mutators = setmetatable({ _=nop }, { __index=function(_, k) return function(v) _G[k] = v end end }) { _M=environment, _G=_G, import=function(interface) import(module, interface) end, error=error, nop=nop, id=id },
fields = setmetatable( { __index=function(_, k) local accessor = accessors[k]; if accessor then return accessor() else return _G[k] end end }
{ _M=environment, _G=_G, include=function(interface) include(module, 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)
) public_mutators = setmetatable({}, nop_default_mt)
public_accessors = setmetatable({}, nop_default_mt) public_fields = setmetatable({}, { __index=function(_, k) return public_accessors[k]() end })
public_mutators = setmetatable({}, nop_default_mt) setmetatable(environment, proxy_mt(fields, mutators))
public_fields = setmetatable({}, { __index=function(_, k) return public_accessors[k]() end }) setmetatable(interface, proxy_mt(public_fields, public_mutators))
setmetatable(environment, proxy_mt(fields, mutators)) module = {
setmetatable(interface, proxy_mt(public_fields, public_mutators, interface_lt)) defined = { _M=true, _G=true, import=true, error=true, nop=true, id=true, public=true, private=true, ['_=']=true },
module = { [ACCESSOR] = accessors,
defined = { _M=true, _G=true, include=true, error=true, nop=true, id=true, public=true, private=true, ['_=']=true }, [MUTATOR] = mutators,
[ACCESSOR] = accessors, [FIELD] = fields,
[MUTATOR] = mutators, [PUBLIC + ACCESSOR] = public_accessors,
[FIELD] = fields, [PUBLIC + MUTATOR] = public_mutators,
[PUBLIC+ACCESSOR] = public_accessors, [PUBLIC + FIELD] = public_fields,
[PUBLIC+MUTATOR] = public_mutators, }
[PUBLIC+FIELD] = public_fields, _module[name], _module[definition_helper] = module, module
} _interface[name] = interface
_module[definition_helper], _module[interface] = module, module setfenv(2, environment)
_G[k] = interface end
setfenv(2, environment)
end,
})
setmetatable(_G, {
__metatable = false,
__index = function(_, k) if k == 'module' then setfenv(2, env) end end,
})