mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-28 04:17:38 +00:00
Support arguments being coerced from strings
In this case, we use Lua's tostring(x) semantics (well, modulo metamethods), instead of Java's Object.toString(x) call. This ensures that values are formatted (mostly) consistently between Lua and Java methods. - Add IArguments.getStringCoerced, which uses Lua's tostring semantics. - Add a Coerced<T> wrapper type, which says to use the .getXCoerced methods. I'm not thrilled about this interface - there's definitely an argument for using annotations - but this is probably more consistent for now. - Convert existing methods to use this call. Closes #1445
This commit is contained in:
@@ -39,7 +39,7 @@ public class ObjectWrapper implements ILuaContext {
|
||||
return method.apply(object, this, new ObjectArguments(args)).getResult();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings({ "unchecked", "TypeParameterUnusedInFormals" })
|
||||
public <T> T callOf(String name, Object... args) throws LuaException {
|
||||
return (T) call(name, args)[0];
|
||||
}
|
||||
|
||||
@@ -137,6 +137,7 @@ public class GeneratorTest {
|
||||
|
||||
public static class IllegalThrows {
|
||||
@LuaFunction
|
||||
@SuppressWarnings("DoNotCallSuggester")
|
||||
public final void go() throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
@@ -173,11 +173,13 @@ public class MethodTest {
|
||||
|
||||
public static class PeripheralThrow implements IPeripheral {
|
||||
@LuaFunction
|
||||
@SuppressWarnings("DoNotCallSuggester")
|
||||
public final void thisThread() throws LuaException {
|
||||
throw new LuaException("!");
|
||||
}
|
||||
|
||||
@LuaFunction(mainThread = true)
|
||||
@SuppressWarnings("DoNotCallSuggester")
|
||||
public final void mainThread() throws LuaException {
|
||||
throw new LuaException("!");
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ package dan200.computercraft.core.filesystem;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
import dan200.computercraft.api.filesystem.WritableMount;
|
||||
import dan200.computercraft.api.lua.Coerced;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
import dan200.computercraft.api.lua.ObjectArguments;
|
||||
import dan200.computercraft.core.TestFiles;
|
||||
import dan200.computercraft.core.apis.handles.EncodedWritableHandle;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -45,7 +45,7 @@ public class FileSystemTest {
|
||||
{
|
||||
var writer = fs.openForWrite("out.txt", false, EncodedWritableHandle::openUtf8);
|
||||
var handle = new EncodedWritableHandle(writer.get(), writer);
|
||||
handle.write(new ObjectArguments("This is a long line"));
|
||||
handle.write(new Coerced<>("This is a long line"));
|
||||
handle.doClose();
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ public class FileSystemTest {
|
||||
{
|
||||
var writer = fs.openForWrite("out.txt", false, EncodedWritableHandle::openUtf8);
|
||||
var handle = new EncodedWritableHandle(writer.get(), writer);
|
||||
handle.write(new ObjectArguments("Tiny line"));
|
||||
handle.write(new Coerced<>("Tiny line"));
|
||||
handle.doClose();
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ public class FileSystemTest {
|
||||
|
||||
fs.unmount("disk");
|
||||
|
||||
var err = assertThrows(LuaException.class, () -> handle.write(new ObjectArguments("Tiny line")));
|
||||
var err = assertThrows(LuaException.class, () -> handle.write(new Coerced<>("Tiny line")));
|
||||
assertEquals("attempt to use a closed file", err.getMessage());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user