mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-17 03:05:30 -04:00
made checkarg a kernel provided function again after all; cleaned up some
This commit is contained in:
parent
e85afe8518
commit
7ef9798cdd
@ -1,7 +1,3 @@
|
|||||||
driver.fs = {}
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
local mtab = {children={}}
|
local mtab = {children={}}
|
||||||
|
|
||||||
local function segments(path)
|
local function segments(path)
|
||||||
@ -31,6 +27,7 @@ local function segments(path)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function findNode(path, create)
|
local function findNode(path, create)
|
||||||
|
checkArg(1, path, "string")
|
||||||
local parts = segments(path)
|
local parts = segments(path)
|
||||||
local node = mtab
|
local node = mtab
|
||||||
for i = 1, #parts do
|
for i = 1, #parts do
|
||||||
@ -46,13 +43,24 @@ local function findNode(path, create)
|
|||||||
return node
|
return node
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function removeEmptyNodes(node)
|
||||||
|
while node and node.parent and not node.fs and not next(node.children) do
|
||||||
|
for k, c in pairs(node.parent.children) do
|
||||||
|
if c == node then
|
||||||
|
node.parent.children[k] = nil
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
node = node.parent
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
driver.fs = {}
|
||||||
|
|
||||||
function driver.fs.mount(fs, path)
|
function driver.fs.mount(fs, path)
|
||||||
assert(type(fs) == "string",
|
checkArg(1, fs, "string")
|
||||||
"bad argument #1 (string expected, got " .. type(fs) .. ")")
|
|
||||||
assert(type(path) == "string",
|
|
||||||
"bad argument #2 (string expected, got " .. type(path) .. ")")
|
|
||||||
local node = findNode(path, true)
|
local node = findNode(path, true)
|
||||||
if node.fs then
|
if node.fs then
|
||||||
return nil, "another filesystem is already mounted here"
|
return nil, "another filesystem is already mounted here"
|
||||||
@ -61,19 +69,6 @@ function driver.fs.mount(fs, path)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function driver.fs.umount(fsOrPath)
|
function driver.fs.umount(fsOrPath)
|
||||||
assert(type(fsOrPath) == "string",
|
|
||||||
"bad argument #1 (string expected, got " .. type(fsOrPath) .. ")")
|
|
||||||
local function removeEmptyNodes(node)
|
|
||||||
while node and node.parent and not node.fs and not next(node.children) do
|
|
||||||
for k, c in pairs(node.parent.children) do
|
|
||||||
if c == node then
|
|
||||||
node.parent.children[k] = nil
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
node = node.parent
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local node, rest = findNode(fsOrPath)
|
local node, rest = findNode(fsOrPath)
|
||||||
if not rest and node.fs then
|
if not rest and node.fs then
|
||||||
node.fs = nil
|
node.fs = nil
|
||||||
@ -331,9 +326,7 @@ function file.write(f, ...)
|
|||||||
if type(arg) == "number" then
|
if type(arg) == "number" then
|
||||||
args[n] = tostring(arg)
|
args[n] = tostring(arg)
|
||||||
end
|
end
|
||||||
if type(arg) ~= "string" then
|
checkArg(n, arg, "string")
|
||||||
error("bad argument #" .. n .. " (string or number expected, got " .. type(arg) .. ")")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
for _, arg in ipairs(args) do
|
for _, arg in ipairs(args) do
|
||||||
--[[ TODO buffer
|
--[[ TODO buffer
|
||||||
@ -350,9 +343,8 @@ end
|
|||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
function driver.fs.open(path, mode)
|
function driver.fs.open(path, mode)
|
||||||
assert(type(path) == "string",
|
|
||||||
"bad argument #1 (string expected, got " .. type(path) .. ")")
|
|
||||||
mode = mode or "r"
|
mode = mode or "r"
|
||||||
|
checkArg(2, mode, "string")
|
||||||
assert(({r=true, rb=true, w=true, wb=true, a=true, ab=true})[mode],
|
assert(({r=true, rb=true, w=true, wb=true, a=true, ab=true})[mode],
|
||||||
"bad argument #2 (r[b], w[b] or a[b] expected, got " .. tostring(mode) .. ")")
|
"bad argument #2 (r[b], w[b] or a[b] expected, got " .. tostring(mode) .. ")")
|
||||||
local node, rest = findNode(path)
|
local node, rest = findNode(path)
|
||||||
@ -400,7 +392,6 @@ function loadfile(file, env)
|
|||||||
end
|
end
|
||||||
local source, reason = f:read("*a")
|
local source, reason = f:read("*a")
|
||||||
f:close()
|
f:close()
|
||||||
f = nil
|
|
||||||
if not source then
|
if not source then
|
||||||
return nil, reason
|
return nil, reason
|
||||||
end
|
end
|
||||||
|
@ -136,6 +136,17 @@ function sandbox.load(code, source, env)
|
|||||||
return load(code, source, "t", env or sandbox)
|
return load(code, source, "t", env or sandbox)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function sandbox.checkArg(n, have, ...)
|
||||||
|
have = type(have)
|
||||||
|
for _, want in pairs({...}) do
|
||||||
|
if have == want then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local msg = "bad argument #" .. n .. " (" .. table.concat({...}, " or ") .. " expected, got " .. have .. ")"
|
||||||
|
error(debug.traceback(msg, 3), 2)
|
||||||
|
end
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
--[[ Install wrappers for coroutine management that reserves the first value
|
--[[ Install wrappers for coroutine management that reserves the first value
|
||||||
|
@ -1,14 +1,3 @@
|
|||||||
local function checkArg(n, have, ...)
|
|
||||||
have = type(have)
|
|
||||||
for _, want in pairs({...}) do
|
|
||||||
if have == want then return end
|
|
||||||
end
|
|
||||||
error("bad argument #" .. n .. " (" .. table.concat({...}, " or ") ..
|
|
||||||
" expected, got " .. have .. ")", 3)
|
|
||||||
end
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
local listeners = {}
|
local listeners = {}
|
||||||
local weakListeners = {}
|
local weakListeners = {}
|
||||||
local timers = {}
|
local timers = {}
|
||||||
|
@ -568,16 +568,6 @@ class Computer(val owner: Computer.Environment) extends Persistable with Runnabl
|
|||||||
})
|
})
|
||||||
lua.setGlobal("drivers")
|
lua.setGlobal("drivers")
|
||||||
|
|
||||||
// Loads the init script. This is loaded and then run by the kernel as a
|
|
||||||
// separate coroutine to sandbox it and enforce timeouts and sandbox user
|
|
||||||
// scripts.
|
|
||||||
lua.pushScalaFunction(lua => {
|
|
||||||
lua.pushString(Source.fromInputStream(classOf[Computer].
|
|
||||||
getResourceAsStream(Config.scriptPath + "init.lua")).mkString)
|
|
||||||
1
|
|
||||||
})
|
|
||||||
lua.setGlobal("init")
|
|
||||||
|
|
||||||
// Run the boot script. This sets up the permanent value tables as
|
// Run the boot script. This sets up the permanent value tables as
|
||||||
// well as making the functions used for persisting/unpersisting
|
// well as making the functions used for persisting/unpersisting
|
||||||
// available as globals. It also wraps the message sending functions
|
// available as globals. It also wraps the message sending functions
|
||||||
|
Loading…
x
Reference in New Issue
Block a user