Improve modules and sandbox system

This commit is contained in:
Eduardo Bart
2012-07-23 21:22:38 -03:00
parent 61d64c7417
commit 1c3e630237
12 changed files with 238 additions and 145 deletions

View File

@@ -110,6 +110,46 @@ function extends(base)
return derived
end
function runinsandbox(func, ...)
if type(func) == 'string' then
func, err = loadfile(resolvepath(func, 2))
if not func then
error(err)
end
end
local env = { }
local oldenv = getfenv(0)
setmetatable(env, { __index = oldenv } )
setfenv(0, env)
func(...)
setfenv(0, oldenv)
return env
end
function loadasmodule(name, file)
file = file or resolvepath(name, 2)
if package.loaded[name] then
return package.loaded[name]
end
local env = runinsandbox(file)
package.loaded[name] = env
return env
end
local function module_loader(modname)
local module = g_modules.getModule(modname)
if not module then
return '\n\tno module \'' .. modname .. '\''
end
return function()
if not module:load() then
error('unable to load required module ' .. modname)
end
return module:getSandbox()
end
end
table.insert(package.loaders, 1, module_loader)
function export(what, key)
if key ~= nil then
_G[key] = what
@@ -130,17 +170,6 @@ function unexport(key)
end
end
function sandbox(what)
what = what or 2
setfenv(what, newenv())
end
function newenv()
local env = { }
setmetatable(env, { __index = getfenv() } )
return env
end
function getfsrcpath(depth)
depth = depth or 2
local info = debug.getinfo(1+depth, "Sn")