mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-07-07 12:32:54 +00:00

When creating a peripheral or custom Lua object, one must implement two methods: - getMethodNames(): String[] - Returns the name of the methods - callMethod(int, ...): Object[] - Invokes the method using an index in the above array. This has a couple of problems: - It's somewhat unwieldy to use - you need to keep track of array indices, which leads to ugly code. - Functions which yield (for instance, those which run on the main thread) are blocking. This means we need to spawn new threads for each CC-side yield. We replace this system with a few changes: - @LuaFunction annotation: One may annotate a public instance method with this annotation. This then exposes a peripheral/lua object method. Furthermore, this method can accept and return a variety of types, which often makes functions cleaner (e.g. can return an int rather than an Object[], and specify and int argument rather than Object[]). - MethodResult: Instead of returning an Object[] and having blocking yields, functions return a MethodResult. This either contains an immediate return, or an instruction to yield with some continuation to resume with. MethodResult is then interpreted by the Lua runtime (i.e. Cobalt), rather than our weird bodgey hacks before. This means we no longer spawn new threads when yielding within CC. - Methods accept IArguments instead of a raw Object array. This has a few benefits: - Consistent argument handling - people no longer need to use ArgumentHelper (as it doesn't exist!), or even be aware of its existence - you're rather forced into using it. - More efficient code in some cases. We provide a Cobalt-specific implementation of IArguments, which avoids the boxing/unboxing when handling numbers and binary strings.
93 lines
3.6 KiB
Lua
93 lines
3.6 KiB
Lua
local function it_side(func, ...)
|
|
local arg = table.pack(...)
|
|
it("requires a specific side", function()
|
|
expect.error(func, 0):eq("bad argument #1 (string expected, got number)")
|
|
expect.error(func, "blah", table.unpack(arg)):eq("bad argument #1 (unknown option blah)")
|
|
|
|
func("top", table.unpack(arg))
|
|
func("Top", table.unpack(arg))
|
|
func("toP", table.unpack(arg))
|
|
end)
|
|
end
|
|
|
|
describe("The redstone library", function()
|
|
describe("redstone.setOutput", function()
|
|
it_side(redstone.setOutput, false)
|
|
|
|
it("sets the output strength correctly", function()
|
|
redstone.setOutput("top", false)
|
|
expect(redstone.getAnalogueOutput("top")):eq(0)
|
|
|
|
redstone.setOutput("top", true)
|
|
expect(redstone.getAnalogueOutput("top")):eq(15)
|
|
end)
|
|
end)
|
|
|
|
describe("redstone.getOutput", function()
|
|
it_side(redstone.getOutput)
|
|
|
|
it("gets the output strength correctly", function()
|
|
redstone.setAnalogueOutput("top", 0)
|
|
expect(redstone.getOutput("top")):eq(false)
|
|
|
|
redstone.setAnalogueOutput("top", 1)
|
|
expect(redstone.getOutput("top")):eq(true)
|
|
|
|
redstone.setAnalogueOutput("top", 15)
|
|
expect(redstone.getOutput("top")):eq(true)
|
|
end)
|
|
end)
|
|
|
|
describe("redstone.getInput", function()
|
|
it_side(redstone.getInput)
|
|
end)
|
|
|
|
describe("redstone.setAnalogueOutput", function()
|
|
it_side(redstone.setAnalogueOutput, 0)
|
|
|
|
it("checks the strength parameter", function()
|
|
expect.error(redstone.setAnalogueOutput, "top", true):eq("bad argument #2 (number expected, got boolean)")
|
|
expect.error(redstone.setAnalogueOutput, "top", 0 / 0):eq("bad argument #2 (number expected, got nan)")
|
|
expect.error(redstone.setAnalogueOutput, "top", math.huge):eq("bad argument #2 (number expected, got inf)")
|
|
expect.error(redstone.setAnalogueOutput, "top", -1):eq("Expected number in range 0-15")
|
|
expect.error(redstone.setAnalogueOutput, "top", 16):eq("Expected number in range 0-15")
|
|
end)
|
|
end)
|
|
|
|
describe("redstone.getAnalogueOutput", function()
|
|
it_side(redstone.getAnalogueOutput)
|
|
end)
|
|
|
|
describe("redstone.getAnalogueInput", function()
|
|
it_side(redstone.getAnalogueInput)
|
|
end)
|
|
|
|
describe("redstone.setBundledOutput", function()
|
|
it_side(redstone.setBundledOutput, 0)
|
|
|
|
it("checks the mask parameter", function()
|
|
expect.error(redstone.setBundledOutput, "top", true):eq("bad argument #2 (number expected, got boolean)")
|
|
expect.error(redstone.setBundledOutput, "top", 0 / 0):eq("bad argument #2 (number expected, got nan)")
|
|
expect.error(redstone.setBundledOutput, "top", math.huge):eq("bad argument #2 (number expected, got inf)")
|
|
end)
|
|
end)
|
|
|
|
describe("redstone.getBundledOutput", function()
|
|
it_side(redstone.getBundledOutput)
|
|
end)
|
|
|
|
describe("redstone.getBundledInput", function()
|
|
it_side(redstone.getBundledInput)
|
|
end)
|
|
|
|
describe("redstone.testBundledInput", function()
|
|
it_side(redstone.testBundledInput, 0)
|
|
|
|
it("checks the mask parameter", function()
|
|
expect.error(redstone.testBundledInput, "top", true):eq("bad argument #2 (number expected, got boolean)")
|
|
expect.error(redstone.testBundledInput, "top", 0 / 0):eq("bad argument #2 (number expected, got nan)")
|
|
expect.error(redstone.testBundledInput, "top", math.huge):eq("bad argument #2 (number expected, got inf)")
|
|
end)
|
|
end)
|
|
end)
|