1
1
mirror of https://git.shadowkat.net/izaya/OC-PsychOS2.git synced 2024-11-14 00:47:00 -05:00
OC-PsychOS2/module/loadfile.lua

24 lines
850 B
Lua
Raw Normal View History

function loadfile(p) -- reads file *p* and returns a function if possible
2018-11-02 12:05:41 -04:00
local f = fs.open(p,"rb")
local c = f:read("*a")
f:close()
return load(c,p,"t")
end
function runfile(p,...) -- runs file *p* with arbitrary arguments in the current thread
return loadfile(p)(...)
2018-11-02 12:05:41 -04:00
end
2019-10-26 11:40:25 -04:00
function os.spawnfile(p,n) -- spawns a new process from file *p* with name *n*
2019-11-03 16:54:30 -05:00
return os.spawn(function() xpcall(loadfile(p),function(e) dprint(e.."\n"..debug.traceback()) end) end,n or p)
end
function require(f) -- searches for a library with name *f* and returns what the library returns, if possible
local lib = os.getenv("LIB") or "/boot/lib"
for d in lib:gmatch("[^\n]+") do
if fs.exists(d.."/"..f) then
return runfile(d.."/"..f)
elseif fs.exists(d.."/"..f..".lua") then
return runfile(d.."/"..f..".lua")
end
end
error("library not found: "..f)
2018-11-02 12:05:41 -04:00
end