opus/sys/modules/opus/injector.lua

121 lines
3.1 KiB
Lua
Raw Normal View History

2020-05-26 03:48:37 +00:00
-- https://www.lua.org/manual/5.1/manual.html#pdf-require
2018-12-30 11:50:59 +00:00
local function split(str, pattern)
local t = { }
local function helper(line) table.insert(t, line) return "" end
helper((str:gsub(pattern, helper)))
return t
end
local hasMain
2018-12-30 11:50:59 +00:00
local luaPaths = package and package.path and split(package.path, '(.-);') or { }
for i = 1, #luaPaths do
2019-06-29 06:44:30 +00:00
if luaPaths[i] == '?' or luaPaths[i] == '?.lua' or luaPaths[i] == '?/init.lua' then
2018-12-30 11:50:59 +00:00
luaPaths[i] = nil
elseif string.find(luaPaths[i], '/rom/modules/main') then
hasMain = true
2018-12-30 11:50:59 +00:00
end
end
2019-01-27 03:58:02 +00:00
table.insert(luaPaths, 1, '?.lua')
table.insert(luaPaths, 2, '?/init.lua')
table.insert(luaPaths, 3, '/usr/modules/?.lua')
table.insert(luaPaths, 4, '/usr/modules/?/init.lua')
if not hasMain then
2019-06-29 06:44:30 +00:00
table.insert(luaPaths, 5, '/rom/modules/main/?')
table.insert(luaPaths, 6, '/rom/modules/main/?.lua')
table.insert(luaPaths, 7, '/rom/modules/main/?/init.lua')
end
2019-06-29 06:44:30 +00:00
table.insert(luaPaths, '/sys/modules/?.lua')
table.insert(luaPaths, '/sys/modules/?/init.lua')
local DEFAULT_PATH = table.concat(luaPaths, ';')
2016-12-23 04:22:04 +00:00
2018-12-23 22:32:06 +00:00
local fs = _G.fs
local os = _G.os
local string = _G.string
2017-10-08 21:45:01 +00:00
2018-01-20 12:18:13 +00:00
-- Add require and package to the environment
2018-12-23 22:32:06 +00:00
return function(env)
2019-01-27 03:58:02 +00:00
local function preloadSearcher(modname)
if env.package.preload[modname] then
return function()
return env.package.preload[modname](modname, env)
end
end
2020-06-06 03:38:26 +00:00
--end
2018-01-24 22:39:38 +00:00
2020-06-06 03:38:26 +00:00
--local function loadedSearcher(modname)
if env.package.loaded[modname] then
2019-01-27 03:58:02 +00:00
return function()
return env.package.loaded[modname]
2018-01-24 22:39:38 +00:00
end
end
end
local sentinel = { }
2018-01-24 22:39:38 +00:00
local function pathSearcher(modname)
if env.package.loaded[modname] == sentinel then
error("loop or previous error loading module '" .. modname .. "'", 0)
end
env.package.loaded[modname] = sentinel
2018-12-17 03:17:19 +00:00
local fname = modname:gsub('%.', '/')
2018-01-24 22:39:38 +00:00
2018-12-17 03:17:19 +00:00
for pattern in string.gmatch(env.package.path, "[^;]+") do
local sPath = string.gsub(pattern, "%?", fname)
2019-01-27 03:58:02 +00:00
-- TODO: if there's no shell, we should not be checking relative paths below
-- as they will resolve to root directory
2020-05-26 03:48:37 +00:00
if env.shell
and type(env.shell.getRunningProgram) == 'function'
and sPath:sub(1, 1) ~= "/" then
2018-01-24 22:39:38 +00:00
2019-06-29 06:44:30 +00:00
sPath = fs.combine(fs.getDir(env.shell.getRunningProgram() or ''), sPath)
2018-01-24 22:39:38 +00:00
end
2019-06-29 06:44:30 +00:00
if fs.exists(sPath) and not fs.isDir(sPath) then
2020-05-26 03:48:37 +00:00
return loadfile(fs.combine(sPath, ''), env)
2018-01-24 22:39:38 +00:00
end
end
end
-- place package and require function into env
env.package = {
2020-05-09 04:32:44 +00:00
path = env.LUA_PATH or _G.LUA_PATH or DEFAULT_PATH,
2018-01-24 22:39:38 +00:00
config = '/\n:\n?\n!\n-',
preload = { },
2018-01-24 22:39:38 +00:00
loaded = {
2018-12-23 22:32:06 +00:00
coroutine = coroutine,
io = io,
2018-01-24 22:39:38 +00:00
math = math,
2018-12-23 22:32:06 +00:00
os = os,
2018-01-24 22:39:38 +00:00
string = string,
table = table,
debug = debug,
2018-01-24 22:39:38 +00:00
},
loaders = {
2019-01-27 03:58:02 +00:00
preloadSearcher,
2020-06-06 03:38:26 +00:00
--loadedSearcher,
2018-01-24 22:39:38 +00:00
pathSearcher,
}
}
function env.require(modname)
for _,searcher in ipairs(env.package.loaders) do
local fn, msg = searcher(modname)
if fn then
2020-06-06 03:38:26 +00:00
local module = fn(modname, env) or true
2018-01-24 22:39:38 +00:00
env.package.loaded[modname] = module
return module
end
if msg then
error(msg, 2)
end
end
error('Unable to find module ' .. modname, 2)
2018-01-24 22:39:38 +00:00
end
return env.require -- backwards compatible
2016-12-11 19:24:52 +00:00
end