wrapped some more functions to avoid LuaErrors being pushed to lua (i.e. userdata); added an init signal that's pushed after components are initialized, queuing autoruns until that arrives, makes autorun scripts less weird when rebooting (not having to do timer stuff in there e.g.)

This commit is contained in:
Florian Nücke 2014-01-27 17:58:21 +01:00
parent fa6dae03eb
commit 16a6f69853
6 changed files with 87 additions and 25 deletions

View File

@ -180,7 +180,17 @@ sandbox = {
pi = math.pi, pi = math.pi,
pow = math.pow, pow = math.pow,
rad = math.rad, rad = math.rad,
random = math.random, random = function(low, high)
if low then
checkArg(1, low, "number")
if high then
checkArg(1, high, "number")
return math.random(low, high)
end
return math.random(low)
end
return math.random()
end,
randomseed = function(seed) randomseed = function(seed)
checkArg(1, seed, "number") checkArg(1, seed, "number")
math.randomseed(seed) math.randomseed(seed)
@ -321,12 +331,38 @@ local libcomputer = {
} }
local libunicode = { local libunicode = {
char = unicode.char, char = function(...)
len = unicode.len, local args = table.pack(...)
lower = unicode.lower, for i = 1, args.n do
reverse = unicode.reverse, checkArg(i, args[i], "number")
sub = unicode.sub, end
upper = unicode.upper return unicode.char(...)
end,
len = function(s)
checkArg(1, s, "string")
return unicode.len(s)
end,
lower = function(s)
checkArg(1, s, "string")
return unicode.lower(s)
end,
reverse = function(s)
checkArg(1, s, "string")
return unicode.reverse(s)
end,
sub = function(s, i, j)
checkArg(1, s, "string")
checkArg(2, i, "number")
checkArg(3, j, "number", "nil")
if j then
return unicode.sub(s, i, j)
end
return unicode.sub(s, i)
end,
upper = function(s)
checkArg(1, s, "string")
return unicode.upper(s)
end
} }
------------------------------------------------------------------------------- -------------------------------------------------------------------------------

View File

@ -3,6 +3,23 @@ local event = require("event")
local fs = require("filesystem") local fs = require("filesystem")
local shell = require("shell") local shell = require("shell")
local isInitialized, pendingAutoruns = false, {}
local function onInit()
isInitialized = true
for _, run in ipairs(pendingAutoruns) do
local result, reason = pcall(run)
if not result then
local log = io.open("/tmp/event.log", "a")
if log then
log:write(reason .. "\n")
log:close()
end
end
end
pendingAutoruns = nil
end
local function onComponentAdded(_, address, componentType) local function onComponentAdded(_, address, componentType)
if componentType == "filesystem" then if componentType == "filesystem" then
local proxy = component.proxy(address) local proxy = component.proxy(address)
@ -16,11 +33,18 @@ local function onComponentAdded(_, address, componentType)
name = fs.concat("/mnt", name) name = fs.concat("/mnt", name)
fs.mount(proxy, name) fs.mount(proxy, name)
if fs.isAutorunEnabled() then if fs.isAutorunEnabled() then
local function run()
local result, reason = shell.execute(fs.concat(name, "autorun"), _ENV, proxy) local result, reason = shell.execute(fs.concat(name, "autorun"), _ENV, proxy)
if not result and reason ~= "file not found" then if not result and reason ~= "file not found" then
error(reason) error(reason)
end end
end end
if isInitialized then
run()
else
table.insert(pendingAutoruns, run)
end
end
end end
end end
end end
@ -34,5 +58,6 @@ local function onComponentRemoved(_, address, componentType)
end end
end end
event.listen("init", onInit)
event.listen("component_added", onComponentAdded) event.listen("component_added", onComponentAdded)
event.listen("component_removed", onComponentRemoved) event.listen("component_removed", onComponentRemoved)

View File

@ -8,6 +8,7 @@ os.sleep(0.5) -- Allow signal processing by libraries.
require("term").clear() require("term").clear()
computer.pushSignal("init") -- so libs know components are initialized.
while true do while true do
local result, reason = os.execute("/bin/sh -v") local result, reason = os.execute("/bin/sh -v")
if not result then if not result then

View File

@ -109,7 +109,7 @@ function event.listen(name, callback)
end end
function event.onError(message) function event.onError(message)
local log = io.open("tmp/event.log", "a") local log = io.open("/tmp/event.log", "a")
if log then if log then
log:write(message .. "\n") log:write(message .. "\n")
log:close() log:close()

View File

@ -162,12 +162,12 @@ chip1 {
} }
chip2 { chip2 {
input: [[nuggetGold, {item=dyePowder, subID=4} , nuggetGold] input: [[nuggetGold, {item=dyePowder, subID=4} , nuggetGold]
["oc:circuitTier1", diamond, "oc:circuitTier1"] ["oc:circuitTier1", netherquartz, "oc:circuitTier1"]
[nuggetGold, {item=dyePowder, subID=4}, nuggetGold]] [nuggetGold, {item=dyePowder, subID=4}, nuggetGold]]
} }
chip3 { chip3 {
input: [[yellowDust, comparator, yellowDust] input: [[yellowDust, comparator, yellowDust]
["oc:circuitTier2", emerald, "oc:circuitTier2"] ["oc:circuitTier2", diamond, "oc:circuitTier2"]
[yellowDust, comparator, yellowDust]] [yellowDust, comparator, yellowDust]]
} }
alu { alu {

View File

@ -238,18 +238,6 @@ object LuaStateFactory {
// Provide some better Unicode support. // Provide some better Unicode support.
state.newTable() state.newTable()
state.pushScalaFunction(lua => {
lua.pushString(lua.checkString(1).toLowerCase)
1
})
state.setField(-2, "lower")
state.pushScalaFunction(lua => {
lua.pushString(lua.checkString(1).toUpperCase)
1
})
state.setField(-2, "upper")
state.pushScalaFunction(lua => { state.pushScalaFunction(lua => {
lua.pushString(String.valueOf((1 to lua.getTop).map(lua.checkInteger).map(_.toChar).toArray)) lua.pushString(String.valueOf((1 to lua.getTop).map(lua.checkInteger).map(_.toChar).toArray))
1 1
@ -262,6 +250,12 @@ object LuaStateFactory {
}) })
state.setField(-2, "len") state.setField(-2, "len")
state.pushScalaFunction(lua => {
lua.pushString(lua.checkString(1).toLowerCase)
1
})
state.setField(-2, "lower")
state.pushScalaFunction(lua => { state.pushScalaFunction(lua => {
lua.pushString(lua.checkString(1).reverse) lua.pushString(lua.checkString(1).reverse)
1 1
@ -286,6 +280,12 @@ object LuaStateFactory {
}) })
state.setField(-2, "sub") state.setField(-2, "sub")
state.pushScalaFunction(lua => {
lua.pushString(lua.checkString(1).toUpperCase)
1
})
state.setField(-2, "upper")
state.setGlobal("unicode") state.setGlobal("unicode")
return Some(state) return Some(state)