1
0
mirror of https://github.com/kepler155c/opus synced 2025-10-17 16:57:39 +00:00

update shell with require

This commit is contained in:
kepler155c@gmail.com
2018-12-16 22:17:19 -05:00
parent 0aa0841b76
commit 7d64b0c6db
5 changed files with 143 additions and 13 deletions

View File

@@ -77,6 +77,107 @@ local function run(env, ...)
return table.unpack(r)
end
local function createShellEnv(sDir)
local tEnv = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
--[[
package.path = "?;?.lua;?/init.lua;/rom/modules/main/?;/rom/modules/main/?.lua;/rom/modules/main/?/init.lua"
if turtle then
package.path = package.path..";/rom/modules/turtle/?;/rom/modules/turtle/?.lua;/rom/modules/turtle/?/init.lua"
elseif command then
package.path = package.path..";/rom/modules/command/?;/rom/modules/command/?.lua;/rom/modules/command/?/init.lua"
end
]]
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 )
_G._p = _ENV
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
@@ -85,7 +186,8 @@ function shell.run(...)
oldTitle = _ENV.multishell.getTitle(_ENV.multishell.getCurrent())
end
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
local env = createShellEnv(shell.dir())
-- local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
local r = { pcall(run, env, ...) }
if _ENV.multishell then
@@ -349,7 +451,8 @@ end
local tArgs = { ... }
if #tArgs > 0 then
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
local env = createShellEnv(shell.dir())
-- local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
return run(env, ...)
end