1
0
mirror of https://github.com/kepler155c/opus synced 2025-10-19 09:47:39 +00:00

require overhaul part 2

This commit is contained in:
kepler155c@gmail.com
2018-12-23 17:32:06 -05:00
parent 26564cbcc1
commit 1264b0149b
4 changed files with 37 additions and 137 deletions

View File

@@ -77,97 +77,6 @@ local function run(env, ...)
return table.unpack(r)
end
local function createShellEnv(sDir)
local tEnv = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
local package
package = {
loaded = {
_G = _G,
bit32 = bit32,
coroutine = coroutine,
math = math,
package = package,
string = string,
table = table,
},
path = _G.LUA_PATH,
config = "/\n;\n?\n!\n-",
preload = { },
loaders = {
function( name )
if package.preload[name] then
return package.preload[name]
else
return nil, "no field package.preload['" .. name .. "']"
end
end,
function( name )
local fname = string.gsub(name, "%.", "/")
local sError = ""
for pattern in string.gmatch(package.path, "[^;]+") do
local sPath = string.gsub(pattern, "%?", fname)
if sPath:sub(1,1) ~= "/" then
sPath = fs.combine(sDir, sPath)
end
if fs.exists(sPath) and not fs.isDir(sPath) then
local fnFile, sError = loadfile( sPath, tEnv )
if fnFile then
return fnFile, sPath
else
return nil, sError
end
else
if #sError > 0 then
sError = sError .. "\n"
end
sError = sError .. "no file '" .. sPath .. "'"
end
end
return nil, sError
end
}
}
local sentinel = {}
local function require( name )
if type( name ) ~= "string" then
error( "bad argument #1 (expected string, got " .. type( name ) .. ")", 2 )
end
if package.loaded[name] == sentinel then
error("Loop detected requiring '" .. name .. "'", 0)
end
if package.loaded[name] then
return package.loaded[name]
end
local sError = "Error loading module '" .. name .. "':"
for _,searcher in ipairs(package.loaders) do
local loader, err = searcher(name)
if loader then
package.loaded[name] = sentinel
local result = loader( err )
if result ~= nil then
package.loaded[name] = result
return result
else
package.loaded[name] = true
return true
end
else
sError = sError .. "\n" .. err
end
end
error(sError, 2)
end
tEnv["package"] = package
tEnv["require"] = require
return tEnv
end
-- Install shell API
function shell.run(...)
local oldTitle
@@ -176,8 +85,9 @@ function shell.run(...)
oldTitle = _ENV.multishell.getTitle(_ENV.multishell.getCurrent())
end
local env = createShellEnv(shell.dir())
-- local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
_G.requireInjector(env)
local r = { pcall(run, env, ...) }
if _ENV.multishell then
@@ -441,8 +351,9 @@ end
local tArgs = { ... }
if #tArgs > 0 then
local env = createShellEnv(shell.dir())
-- local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
_G.requireInjector(env)
return run(env, ...)
end