mirror of https://github.com/kepler155c/opus
tidy up env creation - round 1
This commit is contained in:
parent
4018d4bcfb
commit
3d3e5400cf
|
@ -1,6 +1,3 @@
|
||||||
-- Lua may be called from outside of shell - inject a require
|
|
||||||
_G.requireInjector(_ENV)
|
|
||||||
|
|
||||||
local History = require('opus.history')
|
local History = require('opus.history')
|
||||||
local UI = require('opus.ui')
|
local UI = require('opus.ui')
|
||||||
local Util = require('opus.util')
|
local Util = require('opus.util')
|
||||||
|
|
|
@ -31,7 +31,7 @@ local function snmpConnection(socket)
|
||||||
socket:write('pong')
|
socket:write('pong')
|
||||||
|
|
||||||
elseif msg.type == 'script' then
|
elseif msg.type == 'script' then
|
||||||
local env = setmetatable(Util.shallowCopy(_ENV), { __index = _G })
|
local env = kernel.makeEnv()
|
||||||
local fn, err = load(msg.args, 'script', nil, env)
|
local fn, err = load(msg.args, 'script', nil, env)
|
||||||
if fn then
|
if fn then
|
||||||
kernel.run({
|
kernel.run({
|
||||||
|
@ -45,7 +45,7 @@ local function snmpConnection(socket)
|
||||||
|
|
||||||
elseif msg.type == 'scriptEx' then
|
elseif msg.type == 'scriptEx' then
|
||||||
local s, m = pcall(function()
|
local s, m = pcall(function()
|
||||||
local env = setmetatable(Util.shallowCopy(_ENV), { __index = _G })
|
local env = kernel.makeEnv()
|
||||||
local fn, m = load(msg.args, 'script', nil, env)
|
local fn, m = load(msg.args, 'script', nil, env)
|
||||||
if not fn then
|
if not fn then
|
||||||
error(m)
|
error(m)
|
||||||
|
|
|
@ -6,7 +6,7 @@ local fs = _G.fs
|
||||||
local settings = _G.settings
|
local settings = _G.settings
|
||||||
local shell = _ENV.shell
|
local shell = _ENV.shell
|
||||||
|
|
||||||
local sandboxEnv = setmetatable({ }, { __index = _G })
|
local sandboxEnv = { }
|
||||||
for k,v in pairs(_ENV) do
|
for k,v in pairs(_ENV) do
|
||||||
sandboxEnv[k] = v
|
sandboxEnv[k] = v
|
||||||
end
|
end
|
||||||
|
@ -47,10 +47,11 @@ local function tokenise( ... )
|
||||||
return tWords
|
return tWords
|
||||||
end
|
end
|
||||||
|
|
||||||
local function run(env, ...)
|
local function run(...)
|
||||||
local args = tokenise(...)
|
local args = tokenise(...)
|
||||||
local command = table.remove(args, 1) or error('No such program')
|
local command = table.remove(args, 1) or error('No such program')
|
||||||
local isUrl = not not command:match("^(https?:)")
|
local isUrl = not not command:match("^(https?:)")
|
||||||
|
local env = shell.makeEnv()
|
||||||
|
|
||||||
local path, loadFn
|
local path, loadFn
|
||||||
if isUrl then
|
if isUrl then
|
||||||
|
@ -92,10 +93,7 @@ function shell.run(...)
|
||||||
oldTitle = _ENV.multishell.getTitle(_ENV.multishell.getCurrent())
|
oldTitle = _ENV.multishell.getTitle(_ENV.multishell.getCurrent())
|
||||||
end
|
end
|
||||||
|
|
||||||
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
|
local r = { trace(run, ...) }
|
||||||
_G.requireInjector(env)
|
|
||||||
|
|
||||||
local r = { trace(run, env, ...) }
|
|
||||||
|
|
||||||
if _ENV.multishell then
|
if _ENV.multishell then
|
||||||
_ENV.multishell.setTitle(_ENV.multishell.getCurrent(), oldTitle or 'shell')
|
_ENV.multishell.setTitle(_ENV.multishell.getCurrent(), oldTitle or 'shell')
|
||||||
|
@ -294,8 +292,8 @@ function shell.setEnv(name, value)
|
||||||
sandboxEnv[name] = value
|
sandboxEnv[name] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
function shell.getEnv()
|
function shell.makeEnv()
|
||||||
local env = Util.shallowCopy(sandboxEnv)
|
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
|
||||||
_G.requireInjector(env)
|
_G.requireInjector(env)
|
||||||
return env
|
return env
|
||||||
end
|
end
|
||||||
|
@ -323,7 +321,7 @@ function shell.newTab(tabInfo, ...)
|
||||||
|
|
||||||
if path then
|
if path then
|
||||||
tabInfo.path = path
|
tabInfo.path = path
|
||||||
tabInfo.env = Util.shallowCopy(sandboxEnv)
|
tabInfo.env = shell.makeEnv()
|
||||||
tabInfo.args = args
|
tabInfo.args = args
|
||||||
tabInfo.title = fs.getName(path):match('([^%.]+)')
|
tabInfo.title = fs.getName(path):match('([^%.]+)')
|
||||||
|
|
||||||
|
@ -336,16 +334,16 @@ function shell.newTab(tabInfo, ...)
|
||||||
return nil, 'No such program'
|
return nil, 'No such program'
|
||||||
end
|
end
|
||||||
|
|
||||||
function shell.openTab( ... )
|
function shell.openTab(...)
|
||||||
-- needs to use multishell.launch .. so we can run with stock multishell
|
-- needs to use multishell.launch .. so we can run with stock multishell
|
||||||
local tWords = tokenise( ... )
|
local tWords = tokenise( ... )
|
||||||
local sCommand = tWords[1]
|
local sCommand = tWords[1]
|
||||||
if sCommand then
|
if sCommand then
|
||||||
local sPath = shell.resolveProgram(sCommand)
|
local sPath = shell.resolveProgram(sCommand)
|
||||||
if sPath == "sys/apps/shell.lua" then
|
if sPath == "sys/apps/shell.lua" then
|
||||||
return _ENV.multishell.launch(Util.shallowCopy(sandboxEnv), sPath, table.unpack(tWords, 2))
|
return _ENV.multishell.launch(shell.makeEnv(), sPath, table.unpack(tWords, 2))
|
||||||
else
|
else
|
||||||
return _ENV.multishell.launch(Util.shallowCopy(sandboxEnv), "sys/apps/shell.lua", sCommand, table.unpack(tWords, 2))
|
return _ENV.multishell.launch(shell.makeEnv(), "sys/apps/shell.lua", sCommand, table.unpack(tWords, 2))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -364,10 +362,7 @@ end
|
||||||
|
|
||||||
local tArgs = { ... }
|
local tArgs = { ... }
|
||||||
if #tArgs > 0 then
|
if #tArgs > 0 then
|
||||||
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
|
return run(...)
|
||||||
_G.requireInjector(env)
|
|
||||||
|
|
||||||
return run(env, ...)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local Config = require('opus.config')
|
local Config = require('opus.config')
|
||||||
|
|
|
@ -139,6 +139,8 @@ function kernel.getShell()
|
||||||
return shell
|
return shell
|
||||||
end
|
end
|
||||||
|
|
||||||
|
kernel.makeEnv = shell.makeEnv
|
||||||
|
|
||||||
function kernel.newRoutine(args)
|
function kernel.newRoutine(args)
|
||||||
kernel.UID = kernel.UID + 1
|
kernel.UID = kernel.UID + 1
|
||||||
|
|
||||||
|
@ -151,7 +153,7 @@ function kernel.newRoutine(args)
|
||||||
}, { __index = Routine })
|
}, { __index = Routine })
|
||||||
|
|
||||||
Util.merge(routine, args)
|
Util.merge(routine, args)
|
||||||
routine.env = args.env or shell.getEnv()
|
routine.env = args.env or shell.makeEnv()
|
||||||
|
|
||||||
return routine
|
return routine
|
||||||
end
|
end
|
||||||
|
|
|
@ -36,7 +36,7 @@ function Terminal.window(parent, sx, sy, w, h, isVisible)
|
||||||
local maxScroll = 100
|
local maxScroll = 100
|
||||||
local cx, cy = 1, 1
|
local cx, cy = 1, 1
|
||||||
local blink = false
|
local blink = false
|
||||||
local _bg, _fg = parent.getBackgroundColor(), parent.getTextColor()
|
local _bg, _fg = colors.black, colors.white
|
||||||
|
|
||||||
win.canvas = Canvas({
|
win.canvas = Canvas({
|
||||||
x = sx,
|
x = sx,
|
||||||
|
|
|
@ -119,7 +119,7 @@ function UI:init()
|
||||||
local event = currentPage:pointToChild(x, y)
|
local event = currentPage:pointToChild(x, y)
|
||||||
_ENV.multishell.openTab({
|
_ENV.multishell.openTab({
|
||||||
path = 'sys/apps/Lua.lua',
|
path = 'sys/apps/Lua.lua',
|
||||||
args = { event.element },
|
args = { event.element, self, _ENV },
|
||||||
focused = true })
|
focused = true })
|
||||||
|
|
||||||
elseif ie and currentPage and currentPage.parent.device.side == side then
|
elseif ie and currentPage and currentPage.parent.device.side == side then
|
||||||
|
|
Loading…
Reference in New Issue