From e5312ca6abf739e9d8bad61dbb6627d6ff5ae802 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Sun, 17 Mar 2019 23:54:44 -0400 Subject: [PATCH] better errors + modules as devices (try #2) + pretty lua output --- sys/apis/event.lua | 2 +- sys/apis/injector.lua | 2 ++ sys/apps/Lua.lua | 39 ++++++++++++++++++++++++------------ sys/extensions/3.modules.lua | 13 +++++++----- sys/kernel.lua | 2 +- 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/sys/apis/event.lua b/sys/apis/event.lua index a71ace8..b563f2f 100644 --- a/sys/apis/event.lua +++ b/sys/apis/event.lua @@ -67,7 +67,7 @@ function Routine:resume(event, ...) end if not s and event ~= 'terminate' then - error('\n' .. (m or 'Error processing event')) + error(m or 'Error processing event', -1) end return s, m diff --git a/sys/apis/injector.lua b/sys/apis/injector.lua index 2eb14dc..5497b24 100644 --- a/sys/apis/injector.lua +++ b/sys/apis/injector.lua @@ -29,6 +29,7 @@ local http = _G.http local os = _G.os local string = _G.string +--[[ if not http._patched then -- fix broken http get (http.get is not coroutine safe) local syncLocks = { } @@ -65,6 +66,7 @@ if not http._patched then return s, m end end +--]] local function loadUrl(url) local c diff --git a/sys/apps/Lua.lua b/sys/apps/Lua.lua index a1a3385..12bc072 100644 --- a/sys/apps/Lua.lua +++ b/sys/apps/Lua.lua @@ -20,6 +20,7 @@ _G.requireInjector(sandboxEnv) UI:configure('Lua', ...) local command = '' +local counter = 1 local history = History.load('usr/.lua_history', 25) local page = UI.Page { @@ -209,10 +210,6 @@ end function page:setResult(result) local t = { } - local oterm = term.redirect(self.output.win) - Util.print(result) - term.redirect(oterm) - local function safeValue(v) if type(v) == 'string' or type(v) == 'number' then return v @@ -297,24 +294,39 @@ end function page:rawExecute(s) local fn, m + local wrapped fn = load('return (' ..s.. ')', 'lua', nil, sandboxEnv) if fn then fn = load('return {' ..s.. '}', 'lua', nil, sandboxEnv) + wrapped = true end if fn then fn, m = pcall(fn) - if #m == 1 then + if #m <= 1 and wrapped then m = m[1] end - return fn, m + else + fn, m = load(s, 'lua', nil, sandboxEnv) + if fn then + fn, m = pcall(fn) + end end - fn, m = load(s, 'lua', nil, sandboxEnv) if fn then - fn, m = pcall(fn) + if m or wrapped then + local bg, fg = term.getBackgroundColor(), term.getTextColor() + term.setTextColor(colors.cyan) + term.setBackgroundColor(colors.black) + term.write(string.format('out [%d]: ', counter)) + term.setBackgroundColor(bg) + term.setTextColor(fg) + Util.print(m or 'nil') + end + else + _G.printError(m) end return fn, m @@ -331,17 +343,18 @@ function page:executeStatement(statement) self.output.win.scrollBottom() local bg, fg = term.getBackgroundColor(), term.getTextColor() term.setBackgroundColor(colors.black) - term.setTextColor(colors.yellow) - print('> ' .. tostring(statement)) + term.setTextColor(colors.green) + term.write(string.format('in [%d]: ', counter)) term.setBackgroundColor(bg) term.setTextColor(fg) + print(tostring(statement)) + pcall(function() s, m = self:rawExecute(command) end) - if not s then - _G.printError(m) - end + term.redirect(oterm) + counter = counter + 1 if s and m then self:setResult(m) diff --git a/sys/extensions/3.modules.lua b/sys/extensions/3.modules.lua index 4e53d41..6eb8c57 100644 --- a/sys/extensions/3.modules.lua +++ b/sys/extensions/3.modules.lua @@ -10,8 +10,6 @@ local containers = { neuralInterface = true, } -if true then return end - local function getModules(dev, side) local list = { } @@ -30,7 +28,10 @@ for _,v in pairs(device) do if containers[v.type] then local list = getModules(v, v.side) for k, dev in pairs(list) do - device[k] = dev + -- neural and attached modules have precedence over manipulator modules + if not device[k] or v.type ~= 'manipulator' then + device[k] = dev + end end end end @@ -42,8 +43,10 @@ kernel.hook('device_attach', function(_, eventData) if dev and containers[dev.type] then local list = getModules(peripheral.wrap(dev.side), dev.side) for k,v in pairs(list) do - device[k] = v - os.queueEvent('device_attach', k, v) + if not device[k] or dev.type ~= 'manipulator' then + device[k] = v + os.queueEvent('device_attach', k, v) + end end end end) diff --git a/sys/kernel.lua b/sys/kernel.lua index bad6b82..b81fd9e 100644 --- a/sys/kernel.lua +++ b/sys/kernel.lua @@ -92,7 +92,7 @@ function Routine:resume(event, ...) term.redirect(previousTerm) if not ok and self.haltOnError then - error(result) + error(result, -1) end if coroutine.status(self.co) == 'dead' then Util.removeByValue(kernel.routines, self)