refactoring

This commit is contained in:
Manuel Simon Hirsig
2017-11-07 22:01:30 +01:00
parent 064c03927c
commit 3dbeb1e679
3 changed files with 13 additions and 17 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ end
do do
local characters = T.empty local characters = T.empty
function M.is_player(name) function M.is_player(name)
return characters[name] and true or false return not not characters[name]
end end
function handle.LOAD2() function handle.LOAD2()
characters = realm_data'characters' characters = realm_data'characters'
+11 -15
View File
@@ -1,40 +1,36 @@
if module then return end if module then return end
local _G, setfenv, setmetatable = getfenv(0), setfenv, setmetatable local _G, setfenv, setmetatable = getfenv(0), setfenv, setmetatable
local loaded = {} local environments, exports_data, interfaces = {}, {}, {}
local function nop() end local function nop() end
local function create_module(name) local function create_module(name)
local environment, public_fields = setmetatable({}, {__index=_G}), {} local environment, exports = setmetatable({_G=_G, nop=nop}, {__index=_G}), {}
local public_modifier = setmetatable({}, { environment.M = setmetatable({}, {
__metatable=false, __metatable=false,
__newindex=function(_, k, v) __newindex=function(_, k, v)
environment[k], public_fields[k] = v, v environment[k], exports[k] = v, v
end, end,
}) })
environment._G, environment.nop, environment._M, environment.M = _G, nop, environment, public_modifier
environment.include = function(name) environment.include = function(name)
local P = loaded[name] or error('No such module.', 2) for k, v in exports_data[name] or error('No such module.', 2) do
for k, v in P.public_fields do
environment[k] = v environment[k] = v
end end
end end
local interface = setmetatable({}, {__metatable=false, __index=public_fields, __newindex=nop}) environment._M = environment
local P = {environment=environment, public_fields=public_fields, interface=interface} interfaces[name] = setmetatable({}, {__metatable=false, __index=exports, __newindex=nop})
loaded[name] = P environments[name], exports_data[name] = environment, exports
return P
end end
function module(name) function module(name)
local defined = loaded[name] and true or false local defined = not not environments[name]
if not defined then if not defined then
create_module(name) create_module(name)
end end
setfenv(2, loaded[name].environment) setfenv(2, environments[name])
return defined return defined
end end
function require(name) function require(name)
local P = loaded[name] return interfaces[name]
return P and P.interface
end end
+1 -1
View File
@@ -525,7 +525,7 @@ function validator(filter)
tinsert(stack, any(args)) tinsert(stack, any(args))
end end
elseif type == 'filter' then elseif type == 'filter' then
tinsert(stack, validators[i](record) and true or false) tinsert(stack, not not validators[i](record))
end end
end end
return all(stack) return all(stack)