tidy up env creation - round 1

This commit is contained in:
kepler155c@gmail.com 2020-05-05 17:25:58 -06:00
parent 4018d4bcfb
commit 3d3e5400cf
6 changed files with 18 additions and 24 deletions

View File

@ -1,6 +1,3 @@
-- Lua may be called from outside of shell - inject a require
_G.requireInjector(_ENV)
local History = require('opus.history')
local UI = require('opus.ui')
local Util = require('opus.util')

View File

@ -31,7 +31,7 @@ local function snmpConnection(socket)
socket:write('pong')
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)
if fn then
kernel.run({
@ -45,7 +45,7 @@ local function snmpConnection(socket)
elseif msg.type == 'scriptEx' then
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)
if not fn then
error(m)

View File

@ -6,7 +6,7 @@ local fs = _G.fs
local settings = _G.settings
local shell = _ENV.shell
local sandboxEnv = setmetatable({ }, { __index = _G })
local sandboxEnv = { }
for k,v in pairs(_ENV) do
sandboxEnv[k] = v
end
@ -47,10 +47,11 @@ local function tokenise( ... )
return tWords
end
local function run(env, ...)
local function run(...)
local args = tokenise(...)
local command = table.remove(args, 1) or error('No such program')
local isUrl = not not command:match("^(https?:)")
local env = shell.makeEnv()
local path, loadFn
if isUrl then
@ -92,10 +93,7 @@ function shell.run(...)
oldTitle = _ENV.multishell.getTitle(_ENV.multishell.getCurrent())
end
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
_G.requireInjector(env)
local r = { trace(run, env, ...) }
local r = { trace(run, ...) }
if _ENV.multishell then
_ENV.multishell.setTitle(_ENV.multishell.getCurrent(), oldTitle or 'shell')
@ -294,8 +292,8 @@ function shell.setEnv(name, value)
sandboxEnv[name] = value
end
function shell.getEnv()
local env = Util.shallowCopy(sandboxEnv)
function shell.makeEnv()
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
_G.requireInjector(env)
return env
end
@ -323,7 +321,7 @@ function shell.newTab(tabInfo, ...)
if path then
tabInfo.path = path
tabInfo.env = Util.shallowCopy(sandboxEnv)
tabInfo.env = shell.makeEnv()
tabInfo.args = args
tabInfo.title = fs.getName(path):match('([^%.]+)')
@ -336,16 +334,16 @@ function shell.newTab(tabInfo, ...)
return nil, 'No such program'
end
function shell.openTab( ... )
function shell.openTab(...)
-- needs to use multishell.launch .. so we can run with stock multishell
local tWords = tokenise( ... )
local sCommand = tWords[1]
if sCommand then
local sPath = shell.resolveProgram(sCommand)
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
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
@ -364,10 +362,7 @@ end
local tArgs = { ... }
if #tArgs > 0 then
local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
_G.requireInjector(env)
return run(env, ...)
return run(...)
end
local Config = require('opus.config')

View File

@ -139,6 +139,8 @@ function kernel.getShell()
return shell
end
kernel.makeEnv = shell.makeEnv
function kernel.newRoutine(args)
kernel.UID = kernel.UID + 1
@ -151,7 +153,7 @@ function kernel.newRoutine(args)
}, { __index = Routine })
Util.merge(routine, args)
routine.env = args.env or shell.getEnv()
routine.env = args.env or shell.makeEnv()
return routine
end

View File

@ -36,7 +36,7 @@ function Terminal.window(parent, sx, sy, w, h, isVisible)
local maxScroll = 100
local cx, cy = 1, 1
local blink = false
local _bg, _fg = parent.getBackgroundColor(), parent.getTextColor()
local _bg, _fg = colors.black, colors.white
win.canvas = Canvas({
x = sx,

View File

@ -119,7 +119,7 @@ function UI:init()
local event = currentPage:pointToChild(x, y)
_ENV.multishell.openTab({
path = 'sys/apps/Lua.lua',
args = { event.element },
args = { event.element, self, _ENV },
focused = true })
elseif ie and currentPage and currentPage.parent.device.side == side then