Files
HyperionOS/Src/Hyperion-kernel/data/lib/modules/hyperion/11_ld.kmod
T

47 lines
1.7 KiB
Plaintext

--:Minify:--
local kernel = ...
local cache = {}
kernel.searchpaths = {
"?", "?.lua", "/lib/?", "/lib/?.lua", "/usr/lib/?", "/usr/lib/?.lua",
"/usr/local/lib/?", "/usr/local/lib/?.lua"
}
kernel.reqcache = cache
local function require(module, ...)
if cache[module] then return cache[module].ret end
local args = {...}
for _, path in ipairs(kernel.searchpaths) do
local filepath = path:gsub("?", module)
if kernel.vfs.exists(filepath) then
if kernel.vfs.type(filepath) == "directory" then
filepath = filepath .. "/init.lua"
if kernel.vfs.type(filepath) == "directory" then
error("Module not found: "..module)
end
end
local fd = kernel.vfs.open(filepath, "r")
local chunks = {}
while true do
local chunk = kernel.vfs.read(fd, 4096)
if not chunk or chunk == "" then break end
chunks[#chunks + 1] = chunk
end
kernel.vfs.close(fd)
local data = table.concat(chunks)
local func, err = load(data, "@"..module, "t", kernel._U)
if not func then
error("Error loading module "..module..": "..tostring(err))
end
local ok, ret = xpcall(func, debug.traceback, table.unpack(args))
if not ok then
error("Error running module "..module..": "..tostring(ret))
end
cache[module] = {ret = ret, expires = kernel.EFI:getEpochMs() + 120000}
return ret
end
coroutine.yield()
end
error("Module not found: "..module)
end
_G.require = function(...) return require(...) end