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,
pow = math.pow,
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)
checkArg(1, seed, "number")
math.randomseed(seed)
@ -321,12 +331,38 @@ local libcomputer = {
}
local libunicode = {
char = unicode.char,
len = unicode.len,
lower = unicode.lower,
reverse = unicode.reverse,
sub = unicode.sub,
upper = unicode.upper
char = function(...)
local args = table.pack(...)
for i = 1, args.n do
checkArg(i, args[i], "number")
end
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 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)
if componentType == "filesystem" then
local proxy = component.proxy(address)
@ -16,9 +33,16 @@ local function onComponentAdded(_, address, componentType)
name = fs.concat("/mnt", name)
fs.mount(proxy, name)
if fs.isAutorunEnabled() then
local result, reason = shell.execute(fs.concat(name, "autorun"), _ENV, proxy)
if not result and reason ~= "file not found" then
error(reason)
local function run()
local result, reason = shell.execute(fs.concat(name, "autorun"), _ENV, proxy)
if not result and reason ~= "file not found" then
error(reason)
end
end
if isInitialized then
run()
else
table.insert(pendingAutoruns, run)
end
end
end
@ -34,5 +58,6 @@ local function onComponentRemoved(_, address, componentType)
end
end
event.listen("init", onInit)
event.listen("component_added", onComponentAdded)
event.listen("component_removed", onComponentRemoved)

View File

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

View File

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

View File

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

View File

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