31 lines
1.0 KiB
Lua
31 lines
1.0 KiB
Lua
function loadfile(p) -- reads file *p* and returns a function if possible
|
|
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)(...)
|
|
end
|
|
function os.spawnfile(p,n,...) -- spawns a new process from file *p* with name *n*, with arguments following *n*.
|
|
local tA = {...}
|
|
return os.spawn(function() computer.pushSignal("process_finished", os.pid(), pcall(loadfile(p), table.unpack(tA))) end,n or p)
|
|
end
|
|
_G.libs = {computer=computer,component=component}
|
|
function require(f) -- searches for a library with name *f* and returns what the library returns, if possible
|
|
if not _G.libs[f] then
|
|
local lib = os.getenv("LIB") or "/boot/lib"
|
|
for d in lib:gmatch("[^\n]+") do
|
|
if fs.exists(d.."/"..f) then
|
|
_G.libs[f] = runfile(d.."/"..f)
|
|
elseif fs.exists(d.."/"..f..".lua") then
|
|
_G.libs[f] = runfile(d.."/"..f..".lua")
|
|
end
|
|
end
|
|
end
|
|
if _G.libs[f] then
|
|
return _G.libs[f]
|
|
end
|
|
error("library not found: "..f)
|
|
end
|