2019-01-08 02:01:07 -05:00
|
|
|
function loadfile(p) -- reads file *p* and returns a function if possible
|
2020-03-16 02:30:22 -04:00
|
|
|
local f = io.open(p,"rb")
|
2018-11-02 12:05:41 -04:00
|
|
|
local c = f:read("*a")
|
|
|
|
f:close()
|
|
|
|
return load(c,p,"t")
|
|
|
|
end
|
2019-01-08 02:01:07 -05:00
|
|
|
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-12-15 22:37:29 -05:00
|
|
|
function os.spawnfile(p,n,...) -- spawns a new process from file *p* with name *n*, with arguments following *n*.
|
|
|
|
local tA = {...}
|
2020-03-16 02:30:22 -04:00
|
|
|
return os.spawn(function() local res={pcall(loadfile(p), table.unpack(tA))} computer.pushSignal("process_finished", os.pid(), table.unpack(res)) dprint(table.concat(res)) end,n or p)
|
2019-01-08 02:01:07 -05:00
|
|
|
end
|
2019-12-19 00:14:48 -05:00
|
|
|
_G.libs = {computer=computer,component=component}
|
2019-01-08 02:01:07 -05:00
|
|
|
function require(f) -- searches for a library with name *f* and returns what the library returns, if possible
|
2019-12-19 09:51:27 -05:00
|
|
|
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
|
2019-01-08 02:01:07 -05:00
|
|
|
end
|
|
|
|
end
|
2019-12-19 00:14:48 -05:00
|
|
|
if _G.libs[f] then
|
|
|
|
return _G.libs[f]
|
|
|
|
end
|
2019-12-19 09:51:27 -05:00
|
|
|
error("library not found: "..f)
|
2018-11-02 12:05:41 -04:00
|
|
|
end
|