mirror of
https://github.com/kepler155c/opus
synced 2025-01-19 03:42:51 +00:00
better errors + modules as devices (try #2) + pretty lua output
This commit is contained in:
parent
340636806c
commit
e5312ca6ab
@ -67,7 +67,7 @@ function Routine:resume(event, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if not s and event ~= 'terminate' then
|
if not s and event ~= 'terminate' then
|
||||||
error('\n' .. (m or 'Error processing event'))
|
error(m or 'Error processing event', -1)
|
||||||
end
|
end
|
||||||
|
|
||||||
return s, m
|
return s, m
|
||||||
|
@ -29,6 +29,7 @@ local http = _G.http
|
|||||||
local os = _G.os
|
local os = _G.os
|
||||||
local string = _G.string
|
local string = _G.string
|
||||||
|
|
||||||
|
--[[
|
||||||
if not http._patched then
|
if not http._patched then
|
||||||
-- fix broken http get (http.get is not coroutine safe)
|
-- fix broken http get (http.get is not coroutine safe)
|
||||||
local syncLocks = { }
|
local syncLocks = { }
|
||||||
@ -65,6 +66,7 @@ if not http._patched then
|
|||||||
return s, m
|
return s, m
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
--]]
|
||||||
|
|
||||||
local function loadUrl(url)
|
local function loadUrl(url)
|
||||||
local c
|
local c
|
||||||
|
@ -20,6 +20,7 @@ _G.requireInjector(sandboxEnv)
|
|||||||
UI:configure('Lua', ...)
|
UI:configure('Lua', ...)
|
||||||
|
|
||||||
local command = ''
|
local command = ''
|
||||||
|
local counter = 1
|
||||||
local history = History.load('usr/.lua_history', 25)
|
local history = History.load('usr/.lua_history', 25)
|
||||||
|
|
||||||
local page = UI.Page {
|
local page = UI.Page {
|
||||||
@ -209,10 +210,6 @@ end
|
|||||||
function page:setResult(result)
|
function page:setResult(result)
|
||||||
local t = { }
|
local t = { }
|
||||||
|
|
||||||
local oterm = term.redirect(self.output.win)
|
|
||||||
Util.print(result)
|
|
||||||
term.redirect(oterm)
|
|
||||||
|
|
||||||
local function safeValue(v)
|
local function safeValue(v)
|
||||||
if type(v) == 'string' or type(v) == 'number' then
|
if type(v) == 'string' or type(v) == 'number' then
|
||||||
return v
|
return v
|
||||||
@ -297,24 +294,39 @@ end
|
|||||||
|
|
||||||
function page:rawExecute(s)
|
function page:rawExecute(s)
|
||||||
local fn, m
|
local fn, m
|
||||||
|
local wrapped
|
||||||
|
|
||||||
fn = load('return (' ..s.. ')', 'lua', nil, sandboxEnv)
|
fn = load('return (' ..s.. ')', 'lua', nil, sandboxEnv)
|
||||||
|
|
||||||
if fn then
|
if fn then
|
||||||
fn = load('return {' ..s.. '}', 'lua', nil, sandboxEnv)
|
fn = load('return {' ..s.. '}', 'lua', nil, sandboxEnv)
|
||||||
|
wrapped = true
|
||||||
end
|
end
|
||||||
|
|
||||||
if fn then
|
if fn then
|
||||||
fn, m = pcall(fn)
|
fn, m = pcall(fn)
|
||||||
if #m == 1 then
|
if #m <= 1 and wrapped then
|
||||||
m = m[1]
|
m = m[1]
|
||||||
end
|
end
|
||||||
return fn, m
|
else
|
||||||
|
fn, m = load(s, 'lua', nil, sandboxEnv)
|
||||||
|
if fn then
|
||||||
|
fn, m = pcall(fn)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
fn, m = load(s, 'lua', nil, sandboxEnv)
|
|
||||||
if fn then
|
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
|
end
|
||||||
|
|
||||||
return fn, m
|
return fn, m
|
||||||
@ -331,17 +343,18 @@ function page:executeStatement(statement)
|
|||||||
self.output.win.scrollBottom()
|
self.output.win.scrollBottom()
|
||||||
local bg, fg = term.getBackgroundColor(), term.getTextColor()
|
local bg, fg = term.getBackgroundColor(), term.getTextColor()
|
||||||
term.setBackgroundColor(colors.black)
|
term.setBackgroundColor(colors.black)
|
||||||
term.setTextColor(colors.yellow)
|
term.setTextColor(colors.green)
|
||||||
print('> ' .. tostring(statement))
|
term.write(string.format('in [%d]: ', counter))
|
||||||
term.setBackgroundColor(bg)
|
term.setBackgroundColor(bg)
|
||||||
term.setTextColor(fg)
|
term.setTextColor(fg)
|
||||||
|
print(tostring(statement))
|
||||||
|
|
||||||
pcall(function()
|
pcall(function()
|
||||||
s, m = self:rawExecute(command)
|
s, m = self:rawExecute(command)
|
||||||
end)
|
end)
|
||||||
if not s then
|
|
||||||
_G.printError(m)
|
|
||||||
end
|
|
||||||
term.redirect(oterm)
|
term.redirect(oterm)
|
||||||
|
counter = counter + 1
|
||||||
|
|
||||||
if s and m then
|
if s and m then
|
||||||
self:setResult(m)
|
self:setResult(m)
|
||||||
|
@ -10,8 +10,6 @@ local containers = {
|
|||||||
neuralInterface = true,
|
neuralInterface = true,
|
||||||
}
|
}
|
||||||
|
|
||||||
if true then return end
|
|
||||||
|
|
||||||
local function getModules(dev, side)
|
local function getModules(dev, side)
|
||||||
local list = { }
|
local list = { }
|
||||||
|
|
||||||
@ -30,7 +28,10 @@ for _,v in pairs(device) do
|
|||||||
if containers[v.type] then
|
if containers[v.type] then
|
||||||
local list = getModules(v, v.side)
|
local list = getModules(v, v.side)
|
||||||
for k, dev in pairs(list) do
|
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
|
end
|
||||||
end
|
end
|
||||||
@ -42,8 +43,10 @@ kernel.hook('device_attach', function(_, eventData)
|
|||||||
if dev and containers[dev.type] then
|
if dev and containers[dev.type] then
|
||||||
local list = getModules(peripheral.wrap(dev.side), dev.side)
|
local list = getModules(peripheral.wrap(dev.side), dev.side)
|
||||||
for k,v in pairs(list) do
|
for k,v in pairs(list) do
|
||||||
device[k] = v
|
if not device[k] or dev.type ~= 'manipulator' then
|
||||||
os.queueEvent('device_attach', k, v)
|
device[k] = v
|
||||||
|
os.queueEvent('device_attach', k, v)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
@ -92,7 +92,7 @@ function Routine:resume(event, ...)
|
|||||||
term.redirect(previousTerm)
|
term.redirect(previousTerm)
|
||||||
|
|
||||||
if not ok and self.haltOnError then
|
if not ok and self.haltOnError then
|
||||||
error(result)
|
error(result, -1)
|
||||||
end
|
end
|
||||||
if coroutine.status(self.co) == 'dead' then
|
if coroutine.status(self.co) == 'dead' then
|
||||||
Util.removeByValue(kernel.routines, self)
|
Util.removeByValue(kernel.routines, self)
|
||||||
|
Loading…
Reference in New Issue
Block a user