From 3d3e5400cfd3d40853dc3fe6a6491232c8368524 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Tue, 5 May 2020 17:25:58 -0600 Subject: [PATCH] tidy up env creation - round 1 --- sys/apps/Lua.lua | 3 --- sys/apps/network/snmp.lua | 4 ++-- sys/apps/shell.lua | 27 +++++++++++---------------- sys/kernel.lua | 4 +++- sys/modules/opus/terminal.lua | 2 +- sys/modules/opus/ui.lua | 2 +- 6 files changed, 18 insertions(+), 24 deletions(-) diff --git a/sys/apps/Lua.lua b/sys/apps/Lua.lua index 557573c..8371d06 100644 --- a/sys/apps/Lua.lua +++ b/sys/apps/Lua.lua @@ -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') diff --git a/sys/apps/network/snmp.lua b/sys/apps/network/snmp.lua index 0fc9a34..18a4143 100644 --- a/sys/apps/network/snmp.lua +++ b/sys/apps/network/snmp.lua @@ -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) diff --git a/sys/apps/shell.lua b/sys/apps/shell.lua index 91228fb..bc3707f 100644 --- a/sys/apps/shell.lua +++ b/sys/apps/shell.lua @@ -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') diff --git a/sys/kernel.lua b/sys/kernel.lua index 4838892..b7ea08b 100644 --- a/sys/kernel.lua +++ b/sys/kernel.lua @@ -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 diff --git a/sys/modules/opus/terminal.lua b/sys/modules/opus/terminal.lua index 7b04a42..7d3339c 100644 --- a/sys/modules/opus/terminal.lua +++ b/sys/modules/opus/terminal.lua @@ -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, diff --git a/sys/modules/opus/ui.lua b/sys/modules/opus/ui.lua index 36afdc2..fb67603 100644 --- a/sys/modules/opus/ui.lua +++ b/sys/modules/opus/ui.lua @@ -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