2020-05-12 17:55:05 +10:00
function loadfile ( p ) -- string -- function -- reads file *p* and returns a function if possible
2020-03-16 17:30:22 +11:00
local f = io.open ( p , " rb " )
2018-11-03 03:05:41 +11:00
local c = f : read ( " *a " )
f : close ( )
return load ( c , p , " t " )
end
2020-05-12 17:55:05 +10:00
function runfile ( p , ... ) -- string -- -- runs file *p* with arbitrary arguments in the current thread
2019-01-08 18:01:07 +11:00
return loadfile ( p ) ( ... )
2018-11-03 03:05:41 +11:00
end
2020-06-29 15:26:27 +10:00
2020-03-18 01:11:53 +11:00
_G.package = { }
2020-07-01 14:31:00 +10:00
package.path = " /boot/lib/?.lua;/pkg/lib/?.lua;/boot/lib/?/init.lua;/pkg/lib/?/init.lua;./?;./?.lua;./?/init.lua "
package.loaded = { buffer = buffer , component = component , computer = computer , coroutine = coroutine , fs = fs , math = math , os = os , package = package , string = string , table = table }
package.alias = { filesystem = " fs " }
2020-05-12 17:55:05 +10:00
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
2020-07-01 14:31:00 +10:00
f = package.alias [ f ] or f
2020-04-12 01:59:20 +10:00
if not package.loaded [ f ] or force then
2020-06-25 16:29:12 +10:00
local ln = f : gsub ( " %. " , " / " )
2020-06-25 17:19:50 +10:00
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
2019-12-20 01:51:27 +11:00
end
2019-01-08 18:01:07 +11:00
end
end
2020-03-18 01:11:53 +11:00
if package.loaded [ f ] then
return package.loaded [ f ]
2019-12-19 16:14:48 +11:00
end
2019-12-20 01:51:27 +11:00
error ( " library not found: " .. f )
2018-11-03 03:05:41 +11:00
end
2020-05-12 17:55:05 +10:00
function reload ( f ) -- string -- table -- Reloads library *f* from disk into memory.
2020-04-12 01:59:20 +10:00
return require ( f , true )
end