module refactoring

This commit is contained in:
Simon Hirsig
2017-11-01 15:01:41 +01:00
parent 9989ceb42f
commit 8823b34904
36 changed files with 669 additions and 646 deletions
+7 -10
View File
@@ -1,5 +1,4 @@
if defined 'T' then return end
module 'T'
if module 'T' then return end
local next, getn, setn, tremove, setmetatable = next, getn, table.setn, tremove, setmetatable
@@ -56,17 +55,15 @@ do
M.static = setmetatable({}, {__metatable=false, __newindex=nop, __call=f, __sub=f})
end
M.get.T = acquire
do
local function ret(t)
local function unpack(t)
if getn(t) > 0 then
return tremove(t, 1), ret(t)
return tremove(t, 1), unpack(t)
else
release(t)
end
end
M.ret = ret
M.unpack = unpack
end
M.empty = setmetatable({}, {__metatable=false, __newindex=nop})
@@ -118,18 +115,18 @@ do
})
end
M.A = vararg(function(arg)
M.list = vararg(function(arg)
auto_release[arg] = nil
return arg
end)
M.S = vararg(function(arg)
M.set = vararg(function(arg)
local t = acquire()
for _, v in pairs(arg) do
t[v] = true
end
return t
end)
M.O = vararg(function(arg)
M.map = vararg(function(arg)
local t = acquire()
for i = 1, getn(arg), 2 do
t[arg[i]] = arg[i + 1]
+26 -12
View File
@@ -1,26 +1,40 @@
if module then return end
local _G, setfenv, setmetatable = getfenv(0), setfenv, setmetatable
local loaded, defined = {}, {}
local loaded = {}
local function nop() end
local function create_module(name)
local private, public = setmetatable({}, {__index=_G}), {}
local modifier = setmetatable({}, {
local environment, public_fields = setmetatable({}, {__index=_G}), {}
local public_modifier = setmetatable({}, {
__metatable=false,
__newindex=function(_, k, v) private[k], public[k] = v, v end
__newindex=function(_, k, v)
environment[k], public_fields[k] = v, v
end,
})
private._G, private.nop, private._M, private.M = _G, nop, private, modifier
private.include = function(name)
environment._G, environment.nop, environment._M, environment.M = _G, nop, environment, public_modifier
environment.include = function(name)
local P = loaded[name] or error('No such module.', 2)
for k, v in P.public do private[k] = v end
for k, v in P.public_fields do
environment[k] = v
end
end
local interface = setmetatable({}, {__metatable=false, __index=public, __newindex=nop})
local P = {private=private, public=public, interface=interface}
local interface = setmetatable({}, {__metatable=false, __index=public_fields, __newindex=nop})
local P = {environment=environment, public_fields=public_fields, interface=interface}
loaded[name] = P
return P
end
function module(name) local P = loaded[name] or create_module(name); defined[name] = true; setfenv(2, P.private) end
function require(name) local P = loaded[name] or create_module(name); return P.interface end
function _G.defined(name) return defined[name] end
function module(name)
local defined = loaded[name] and true or false
if not defined then
create_module(name)
end
setfenv(2, loaded[name].environment)
return defined
end
function require(name)
local P = loaded[name]
return P and P.interface
end