better errors + modules as devices (try #2) + pretty lua output

This commit is contained in:
kepler155c@gmail.com 2019-03-17 23:54:44 -04:00
parent 340636806c
commit e5312ca6ab
5 changed files with 38 additions and 20 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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)