From be3d3c207fd7a433c99db67337eed42049154a7f Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Thu, 25 Jun 2020 17:19:50 +1000 Subject: [PATCH] rewrote require() to support package.path and submodules --- module/loadfile.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/module/loadfile.lua b/module/loadfile.lua index ff606d8..1b37155 100644 --- a/module/loadfile.lua +++ b/module/loadfile.lua @@ -12,16 +12,16 @@ function os.spawnfile(p,n,...) -- string string -- number -- spawns a new proces 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) end _G.package = {} -package.loaded = {computer=computer,component=component,fs=fs,buffer=buffer,table=table,string=string,package=package,os=os,math=math,coroutine=coroutine} +package.path="./?;./?.lua;/boot/lib/?.lua;/pkg/lib/?.lua;/boot/lib/?/init.lua;/pkg/lib/?/init.lua" +package.loaded = {computer=computer,component=component,fs=fs,buffer=buffer} function require(f,force) -- string boolean -- table -- searches for a library with name *f* and returns what the library returns, if possible. if *force* is set, loads the library even if it is cached if not package.loaded[f] or force then - local lib = os.getenv("LIB") or "/boot/lib\n/pkg/lib" local ln = f:gsub("%.","/") - for d in lib:gmatch("[^\n]+") do - if fs.isDirectory(d.."/"..ln) then - package.loaded[f] = runfile(d.."/"..ln.."/init.lua") - elseif fs.exists(d.."/"..ln..".lua") then - package.loaded[f] = runfile(d.."/"..ln..".lua") + for d in package.path:gmatch("[^;]+") do + local p = d:gsub("%?",ln) + if fs.exists(p) and not fs.isDirectory(p) then + package.loaded[f] = runfile(p) + break end end end