refactoring
This commit is contained in:
+28
-25
@@ -2,7 +2,7 @@ if module then return end
|
||||
local typeof, tostring, setmetatable, setfenv, unpack, next, pcall, _G = type, tostring, setmetatable, setfenv, unpack, next, pcall, getfenv(0)
|
||||
|
||||
local PUBLIC, PRIVATE = 1, 2
|
||||
local GETTER, SETTER, FUNCTION = 1, 2, 3
|
||||
local FUNCTION, GETTER, SETTER = 1, 2, 3
|
||||
|
||||
local function error(msg, ...) return _G.error(format(msg or '', unpack(arg))..'\n'..debugstack(), 0) end
|
||||
local function import_error() error 'Invalid imports.' end
|
||||
@@ -14,15 +14,14 @@ local function const(v) return function() return v end end
|
||||
|
||||
local _state = {}
|
||||
|
||||
local declarator_mt, env_mt = {__metatable=false}, {__metatable=false}
|
||||
local declarator_mt = {__metatable=false}
|
||||
do
|
||||
local ACCESS, TYPE = {public=PUBLIC, private=PRIVATE}, {call=FUNCTION, get=GETTER, set=SETTER}
|
||||
local access, name
|
||||
local function extract(v)
|
||||
local call, get, set
|
||||
call, get, set, v.call, v.get, v.set = v.call, v.get, v.set, nil, nil, nil
|
||||
if next(v) or call ~= nil and get ~= nil then error() end
|
||||
return call, get, set
|
||||
local f, getter, setter
|
||||
f, getter, setter, v.call, v.get, v.set = v.call, v.get, v.set, nil, nil, nil
|
||||
if next(v) or f ~= nil and getter ~= nil then error() end
|
||||
return f, getter, setter
|
||||
end
|
||||
local function declare(state, access, name, data)
|
||||
state.access[name] = state.access[name] and collision_error(name) or access or state.default_access
|
||||
@@ -33,37 +32,38 @@ do
|
||||
state[type][name] = value
|
||||
end
|
||||
end
|
||||
|
||||
function declarator_mt:__index(key) local state=_state[self]
|
||||
if ACCESS[key] and not access then
|
||||
access = ACCESS[key]
|
||||
elseif not name or declaration_error() then
|
||||
name = key
|
||||
if ACCESS[key] and not state.declaration_access then
|
||||
state.declaration_access = ACCESS[key]
|
||||
elseif not state.declarator_name or declaration_error() then
|
||||
state.declarator_name = key
|
||||
end
|
||||
return self
|
||||
end
|
||||
function declarator_mt:__newindex(key, value) local state=_state[self]
|
||||
-- if module.access[key] then return end
|
||||
local type
|
||||
if name then
|
||||
if state.declarator_name then
|
||||
type = TYPE[key] or declaration_error()
|
||||
else
|
||||
type = typeof(value) == 'function' and FUNCTION or GETTER
|
||||
end
|
||||
declare(state, access, key, {[type]=value})
|
||||
access, name = nil, nil
|
||||
declare(state, state.declaration_access, key, {[type]=value})
|
||||
state.declaration_access, state.declarator_name = nil, nil
|
||||
end
|
||||
function declarator_mt:__call(value) local state=_state[self]
|
||||
if name then
|
||||
local success, call, get, set = pcall(extract, value)
|
||||
if state.declarator_name then
|
||||
local success, f, get, set = pcall(extract, value)
|
||||
if not success then declaration_error() end
|
||||
declare(state, access, name, {[FUNCTION]=call, [GETTER]=get, [SETTER]=set})
|
||||
elseif access or declaration_error() then
|
||||
state.default_access = access
|
||||
declare(state, state.declaration_access, state.declarator_name, {[FUNCTION]=f, [GETTER]=get, [SETTER]=set})
|
||||
elseif state.declaration_access or declaration_error() then
|
||||
state.default_access = state.declaration_access
|
||||
end
|
||||
access, name = nil, nil
|
||||
state.declaration_access, state.declarator_name = nil, nil
|
||||
end
|
||||
end
|
||||
|
||||
local env_mt = {__metatable=false}
|
||||
function env_mt:__index(key) local state=_state[self]
|
||||
local get = state[GETTER][key]
|
||||
if get then return get() end
|
||||
@@ -88,9 +88,12 @@ function interface_mt:__newindex(key, value) local state=_state[self]
|
||||
end
|
||||
|
||||
function module(...)
|
||||
local declarator, env, interface = setmetatable({}, declarator_mt), setmetatable({}, env_mt), setmetatable({}, interface_mt)
|
||||
local access = {error=PRIVATE, nop=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, public=PRIVATE, private=PRIVATE}
|
||||
local functions, getters, setters = {error=error, nop=nop}, {_G=const(_G), M=const(env), I=const(interface)}, {}
|
||||
local state, declarator, env, interface, access, functions, getters, setters
|
||||
declarator, env, interface = setmetatable({}, declarator_mt), setmetatable({}, env_mt), setmetatable({}, interface_mt)
|
||||
access = {error=PRIVATE, nop=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, public=PRIVATE, private=PRIVATE}
|
||||
functions = {error=error, nop=nop}
|
||||
getters = {_G=const(_G), M=const(env), I=const(interface), public=function() state.declaration_access = PUBLIC return declarator end, private=function() state.declaration_access = PRIVATE return declarator end}
|
||||
setters = {}
|
||||
for i=1,arg.n do
|
||||
local module = _state[arg[i] or import_error()] or import_error()
|
||||
local import_functions, import_getters, import_setters = module[FUNCTION], module[GETTER], module[SETTER]
|
||||
@@ -101,7 +104,7 @@ function module(...)
|
||||
end
|
||||
end
|
||||
end
|
||||
local state = {access=access, [FUNCTION]=functions, [GETTER]=getters, [SETTER]=setters, default_access=PRIVATE}
|
||||
state = {access=access, [FUNCTION]=functions, [GETTER]=getters, [SETTER]=setters, default_access=PRIVATE}
|
||||
_state[declarator], _state[env], _state[interface] = state, state, state
|
||||
setfenv(2, env)
|
||||
end
|
||||
Reference in New Issue
Block a user