From d9cc184f848178e051bcaeba9cc46cd49adee7d2 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Tue, 8 Jan 2019 18:01:07 +1100 Subject: [PATCH] commented loadfile-related functions, added require() --- module/loadfile.lua | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/module/loadfile.lua b/module/loadfile.lua index 843af4d..5b24783 100644 --- a/module/loadfile.lua +++ b/module/loadfile.lua @@ -1,12 +1,23 @@ -function loadfile(p) +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,...) - loadfile(p)(...) +function runfile(p,...) -- runs file *p* with arbitrary arguments in the current thread + return loadfile(p)(...) end -function spawnfile(p,n) - os.spawn(loadfile(p),n) +function spawnfile(p,n) -- spawns a new process from file *p* with name *n* + return os.spawn(loadfile(p),n) +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) end