require cleanup + compatibility fixes

This commit is contained in:
kepler155c@gmail.com 2020-06-11 13:28:43 -06:00
parent 1bf6daedff
commit a2af4405e7
4 changed files with 17 additions and 23 deletions

View File

@ -3,7 +3,7 @@ local Util = require('opus.util')
-- some programs expect to be run in the global scope -- some programs expect to be run in the global scope
-- ie. busted, moonscript -- ie. busted, moonscript
-- create a new sandbox mimicing pure lua -- create a new environment mimicing pure lua
local fs = _G.fs local fs = _G.fs
local shell = _ENV.shell local shell = _ENV.shell

View File

@ -36,24 +36,26 @@ local function tokenise( ... )
end end
local defaultHandlers = { local defaultHandlers = {
urlHandler = function(args, env) function(args, env)
return args[1]:match("^(https?:)") and { return args[1]:match("^(https?:)") and {
title = fs.getName(args[1]), title = fs.getName(args[1]),
path = table.remove(args, 1), path = table.remove(args, 1),
args = args, args = args,
load = Util.loadUrl, load = Util.loadUrl,
env = env, env = shell.makeEnv(env),
} }
end, end,
pathHandler = function(args, env) function(args, env)
local command = table.remove(args, 1) local command = shell.resolveProgram(table.remove(args, 1))
or error('No such program')
return { return {
title = fs.getName(command):match('([^%.]+)'), title = fs.getName(command):match('([^%.]+)'),
path = shell.resolveProgram(command) or error('No such program'), path = command,
args = args, args = args,
load = loadfile, load = loadfile,
env = env, env = shell.makeEnv(env, fs.getDir(command)),
} }
end, end,
} }
@ -86,7 +88,7 @@ local function run(...)
error('No such program') error('No such program')
end end
local pi = handleCommand(args, shell.makeEnv(_ENV)) local pi = handleCommand(args, _ENV)
local O_v_O, err = pi.load(pi.path, pi.env) local O_v_O, err = pi.load(pi.path, pi.env)
if not O_v_O then if not O_v_O then
@ -310,9 +312,9 @@ function shell.getRunningInfo()
end end
-- convenience function for making a runnable env -- convenience function for making a runnable env
function shell.makeEnv(env) function shell.makeEnv(env, dir)
env = setmetatable(Util.shallowCopy(env), { __index = _G }) env = setmetatable(Util.shallowCopy(env), { __index = _G })
_G.requireInjector(env) _G.requireInjector(env, dir)
return env return env
end end

View File

@ -165,9 +165,9 @@ function kernel.getShell()
end end
-- each routine inherits the parent's env -- each routine inherits the parent's env
function kernel.makeEnv(env) function kernel.makeEnv(env, dir)
env = setmetatable(Util.shallowCopy(env or _ENV), { __index = _G }) env = setmetatable(Util.shallowCopy(env or _ENV), { __index = _G })
_G.requireInjector(env) _G.requireInjector(env, dir)
return env return env
end end
@ -182,7 +182,7 @@ function kernel.newRoutine(env, args)
}, { __index = Routine }) }, { __index = Routine })
Util.merge(routine, args) Util.merge(routine, args)
routine.env = args.env or kernel.makeEnv(env) routine.env = args.env or kernel.makeEnv(env, routine.path and fs.getDir(routine.path))
routine.terminal = routine.terminal or routine.window routine.terminal = routine.terminal or routine.window
return routine return routine

View File

@ -38,7 +38,7 @@ do
end end
end end
local DEFAULT_PATH = table.concat(defaultPath, ';') local DEFAULT_PATH = table.concat(defaultPath, ';')
local fs = _G.fs local fs = _G.fs
local os = _G.os local os = _G.os
@ -59,15 +59,9 @@ return function(env, programDir)
for pattern in string.gmatch(env.package.path, "[^;]+") do for pattern in string.gmatch(env.package.path, "[^;]+") do
local sPath = string.gsub(pattern, "%?", fname) local sPath = string.gsub(pattern, "%?", fname)
-- TODO: if there's no shell, we should not be checking relative paths below
-- as they will resolve to root directory
if programDir and sPath:sub(1, 1) ~= "/" then if programDir and sPath:sub(1, 1) ~= "/" then
sPath = fs.combine(programDir, sPath) sPath = fs.combine(programDir, sPath)
elseif env.shell
and type(env.shell.getRunningProgram) == 'function'
and sPath:sub(1, 1) ~= "/" then
sPath = fs.combine(fs.getDir(env.shell.getRunningProgram() or ''), sPath)
end end
if fs.exists(sPath) and not fs.isDir(sPath) then if fs.exists(sPath) and not fs.isDir(sPath) then
return loadfile(fs.combine(sPath, ''), env) return loadfile(fs.combine(sPath, ''), env)
@ -131,6 +125,4 @@ return function(env, programDir)
end end
error('Unable to find module ' .. modname, 2) error('Unable to find module ' .. modname, 2)
end end
return env.require -- backwards compatible
end end