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
-- ie. busted, moonscript
-- create a new sandbox mimicing pure lua
-- create a new environment mimicing pure lua
local fs = _G.fs
local shell = _ENV.shell

View File

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

View File

@ -165,9 +165,9 @@ function kernel.getShell()
end
-- 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 })
_G.requireInjector(env)
_G.requireInjector(env, dir)
return env
end
@ -182,7 +182,7 @@ function kernel.newRoutine(env, args)
}, { __index = Routine })
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
return routine

View File

@ -38,7 +38,7 @@ do
end
end
local DEFAULT_PATH = table.concat(defaultPath, ';')
local DEFAULT_PATH = table.concat(defaultPath, ';')
local fs = _G.fs
local os = _G.os
@ -59,15 +59,9 @@ return function(env, programDir)
for pattern in string.gmatch(env.package.path, "[^;]+") do
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
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
if fs.exists(sPath) and not fs.isDir(sPath) then
return loadfile(fs.combine(sPath, ''), env)
@ -131,6 +125,4 @@ return function(env, programDir)
end
error('Unable to find module ' .. modname, 2)
end
return env.require -- backwards compatible
end