forked from Hyperion/HyperionOS
super dupper system update (it runs)
This commit is contained in:
@@ -1,56 +1,42 @@
|
||||
local args = {...}
|
||||
local kernel = args[1]
|
||||
-- List of search paths
|
||||
local paths = {
|
||||
local kernel = ...
|
||||
local cache = {}
|
||||
local searchpaths = {
|
||||
"/lib/?.lua",
|
||||
"/lib/?",
|
||||
"/usr/lib/?.lua",
|
||||
"/usr/lib/?",
|
||||
"/usr/local/lib/?"
|
||||
"/usr/local/lib/?.lua",
|
||||
"/usr/local/lib/?",
|
||||
"?.lua",
|
||||
"?"
|
||||
}
|
||||
|
||||
-- Custom require implementation
|
||||
function require(module)
|
||||
-- Return cached module if it already exists
|
||||
if kernel.cache.preload[module]~=nil then
|
||||
return kernel.cache.preload[module]
|
||||
function require(module,...)
|
||||
if cache[module] then
|
||||
return cache[module]
|
||||
end
|
||||
|
||||
local err = {}
|
||||
|
||||
for _, path in ipairs(paths) do
|
||||
-- Replace "?" with module name
|
||||
local filePath = string.replace(path, "?", module)
|
||||
|
||||
-- Try to open file
|
||||
local file = kernel.fs.isFile(filePath)
|
||||
if file then
|
||||
local content = kernel.fs.readAllText(filePath)
|
||||
|
||||
-- Load the module as Lua code
|
||||
local chunk, loadErr = load(content, filePath)
|
||||
if not chunk then
|
||||
table.insert(err, "Error loading: " .. filePath .. ": " .. loadErr)
|
||||
local modpath = module:gsub("%.", "/")
|
||||
local failed = {}
|
||||
for _, path in ipairs(searchpaths) do
|
||||
local full_path = string.gsub(path, "%?", modpath)
|
||||
if full_path:sub(1,1)~="/" then
|
||||
full_path=kernel.currentTask.cwd..full_path
|
||||
end
|
||||
if kernel.vfs.exists(full_path) then
|
||||
if kernel.vfs.type(full_path)=="directory" then
|
||||
full_path = full_path .. "/init"
|
||||
end
|
||||
if kernel.vfs.exists(full_path) then
|
||||
local handle = kernel.vfs.open(full_path, "r")
|
||||
local file_content = kernel.vfs.read(handle, 1024 * 1024 * 4)
|
||||
kernel.vfs.close(handle)
|
||||
return assert(load(file_content, full_path, "t", kernel._U))(...)
|
||||
else
|
||||
-- Execute the module. If the module returns a value, cache it.
|
||||
local ok, result = xpcall(chunk, debug.traceback)
|
||||
if not ok then
|
||||
table.insert(err, "Error executing: "..filePath..": "..tostring(result))
|
||||
else
|
||||
if result ~= nil then
|
||||
kernel.cache.preload[module] = result
|
||||
return result
|
||||
end
|
||||
|
||||
-- If module doesn't return anything, cache `true` like Lua does
|
||||
kernel.cache.preload[module] = true
|
||||
return true
|
||||
end
|
||||
table.insert(failed, full_path)
|
||||
end
|
||||
else
|
||||
table.insert(err, "Module not found: " .. filePath)
|
||||
table.insert(failed, full_path)
|
||||
end
|
||||
end
|
||||
|
||||
-- If nothing worked, raise an error with all reasons
|
||||
error("Unable to require module '" .. module .. "':\n" .. table.concat(err, "\n"))
|
||||
end
|
||||
kernel.log("Created require")
|
||||
error("Module not found: " .. module .. " (searched paths: " .. table.concat(failed, ", ") .. ")")
|
||||
end
|
||||
Reference in New Issue
Block a user