LuPPC/src/lua/core/init.lua

148 lines
3.2 KiB
Lua
Raw Normal View History

2016-01-07 02:58:05 +11:00
function checkArg(n, have, ...)
2016-01-06 08:29:49 +11:00
have = type(have)
local function check(want, ...)
if not want then
return false
else
return have == want or check(...)
end
end
if not check(...) then
local msg = string.format("bad argument #%d (%s expected, got %s)",
n, table.concat({...}, " or "), have)
error(msg, 3)
end
end
-------------------------------------------------------------------------------
2016-02-29 00:31:55 +11:00
local argv = {...}
function hasOpt(o, n, ...)
for _, v in pairs(argv) do
if v == o then
return true
end
end
if n then
return hasOpt(n, ...)
end
return false
end
if hasOpt("-h", "--help") then
print(moduleCode.help)
os.exit(0)
end
-------------------------------------------------------------------------------
2016-01-14 12:13:00 +11:00
math.randomseed(native.uptime())--TODO: Make it better?
2016-01-06 08:29:49 +11:00
2016-02-10 03:40:51 +11:00
lprint = function (...)
print(...)
native.log(table.concat({...}, " "))
end
lprint("LuPI L1 INIT")
2016-01-05 04:20:40 +11:00
modules = {}
2016-01-10 05:39:48 +11:00
deadhooks = {}
2016-01-05 04:20:40 +11:00
2016-02-29 00:31:55 +11:00
function loadModule(name)
if modules[name] then
return
end
2016-02-10 03:40:51 +11:00
lprint("LuPI L1 INIT > Load module > " .. name)
2016-01-14 11:10:04 +11:00
io.flush()
2016-01-05 04:20:40 +11:00
--TODO: PRERELEASE: Module sandboxing, preferably secure-ish
--TODO: ASAP: Handle load errors
if not moduleCode[name] then
error("No code for module " .. tostring(name))
end
2016-01-10 05:39:48 +11:00
local code, reason = load(moduleCode[name], "=Module "..name)
2016-01-06 08:29:49 +11:00
if not code then
2016-02-10 03:40:51 +11:00
lprint("Failed loading module " .. name .. ": " .. reason)
2016-01-14 11:10:04 +11:00
io.flush()
2016-01-06 08:29:49 +11:00
else
modules[name] = code()
end
2016-01-05 04:20:40 +11:00
end
2016-01-10 05:39:48 +11:00
function main()
--Load modules
--Utils
2016-02-10 03:40:51 +11:00
if native.debug then
loadModule("debug")
end
2016-01-10 05:39:48 +11:00
loadModule("random")
loadModule("color")
2016-02-09 00:01:10 +11:00
loadModule("buffer")
2016-01-07 02:58:05 +11:00
2016-01-14 12:13:00 +11:00
modules.address = modules.random.uuid() --TODO: PREALPHA: Make constant
2016-01-10 05:39:48 +11:00
--Core
loadModule("component")
loadModule("computer")
2016-01-07 02:58:05 +11:00
2016-01-10 05:39:48 +11:00
--Components
loadModule("eeprom")
2016-02-12 22:21:22 +11:00
loadModule("gpio")
2016-01-20 04:04:12 +11:00
2016-02-29 00:31:55 +11:00
loadModule("gpudetect")
2016-01-10 05:39:48 +11:00
loadModule("filesystem")
loadModule("internet")
2016-01-07 02:58:05 +11:00
2016-01-10 05:39:48 +11:00
--Userspace
loadModule("sandbox")
loadModule("boot")
2016-01-05 04:20:40 +11:00
2016-01-10 05:39:48 +11:00
--Setup core modules
modules.component.prepare()
modules.computer.prepare()
2016-01-15 08:44:49 +11:00
_G.pushEvent = modules.computer.api.pushSignal
2016-01-05 04:20:40 +11:00
2016-01-10 05:39:48 +11:00
modules.eeprom.register()
2016-02-12 22:21:22 +11:00
modules.gpio.register()
modules.internet.start()
2016-01-10 05:39:48 +11:00
modules.filesystem.register("root")
if native.debug and native.platform():match("unix") then
2016-02-12 07:45:46 +11:00
modules.filesystem.register("/", "11111111-1111-1111-1111-111111111111")
end
if native.platform():match("unix") then
modules.computer.tmp = modules.filesystem.register("/tmp/lupi-" .. modules.random.uuid())
else
native.fs_mkdir("tmp")
modules.computer.tmp = modules.filesystem.register("tmp/lupi-" .. modules.random.uuid())
--TODO: cleaning hook or something
end
2016-01-20 04:04:12 +11:00
2016-02-29 00:31:55 +11:00
modules.gpudetect.run()
2016-01-07 07:32:53 +11:00
2016-02-10 03:40:51 +11:00
if native.debug then
modules.debug.hook()
end
2016-01-10 05:39:48 +11:00
modules.boot.boot()
end
2016-02-10 03:40:51 +11:00
local tb = ""
local state, cause = xpcall(main, function(e)
tb = debug.traceback(e, 2)
end)
2016-01-10 05:39:48 +11:00
2016-02-10 03:40:51 +11:00
lprint("Running shutdown hooks")
2016-01-10 05:39:48 +11:00
for k, hook in ipairs(deadhooks) do
local state, cause = pcall(hook)
if not state then
2016-02-10 03:40:51 +11:00
lprint("Shutdown hook with following error:")
lprint(cause)
2016-01-10 05:39:48 +11:00
end
2016-01-20 05:46:40 +11:00
end
2016-01-20 04:04:12 +11:00
2016-02-10 03:40:51 +11:00
lprint("Hooks executed: " .. #deadhooks)
if not state then
lprint("LuPI finished with following error:")
lprint(tb)
2016-01-20 04:04:12 +11:00
end