From 666df3e74b64cc95aa0a45d393e91623e1b67cdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Fri, 3 Jan 2014 15:16:53 +0100 Subject: [PATCH] added support for tables as arguments to lua callbacks (will have to double check I didn't assume anywhere that this wouldn't happen...) --- li/cil/oc/api/network/Arguments.java | 35 +++++++++++++++++++---- li/cil/oc/server/component/Computer.scala | 1 + li/cil/oc/server/network/Component.scala | 22 ++++++++++++++ 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/li/cil/oc/api/network/Arguments.java b/li/cil/oc/api/network/Arguments.java index 1db862dbb..547d5cb66 100644 --- a/li/cil/oc/api/network/Arguments.java +++ b/li/cil/oc/api/network/Arguments.java @@ -1,5 +1,7 @@ package li.cil.oc.api.network; +import java.util.Map; + /** * This interface provides access to arguments passed to a {@link LuaCallback}. *

@@ -99,10 +101,22 @@ public interface Arguments extends Iterable { */ byte[] checkByteArray(int index); + /** + * Try to get a table at the specified index. + *

+ * Throws an error if there are too few arguments. + * + * @param index the index from which to get the argument. + * @return the table at the specified index. + * @throws IllegalArgumentException if there is no argument at that index, + * or if the argument is not a table. + */ + Map checkTable(int index); + /** * Tests whether the argument at the specified index is a boolean value. *

- * This will return true if there is no argument at the specified + * This will return false if there is no argument at the specified * index, i.e. if there are too few arguments. * * @param index the index to check. @@ -113,7 +127,7 @@ public interface Arguments extends Iterable { /** * Tests whether the argument at the specified index is an integer value. *

- * This will return true if there is no argument at the specified + * This will return false if there is no argument at the specified * index, i.e. if there are too few arguments. * * @param index the index to check. @@ -124,7 +138,7 @@ public interface Arguments extends Iterable { /** * Tests whether the argument at the specified index is a double value. *

- * This will return true if there is no argument at the specified + * This will return false if there is no argument at the specified * index, i.e. if there are too few arguments. * * @param index the index to check. @@ -135,7 +149,7 @@ public interface Arguments extends Iterable { /** * Tests whether the argument at the specified index is a string value. *

- * This will return true if there is no argument at the specified + * This will return false if there is no argument at the specified * index, i.e. if there are too few arguments. * * @param index the index to check. @@ -146,11 +160,22 @@ public interface Arguments extends Iterable { /** * Tests whether the argument at the specified index is a byte array. *

- * This will return true if there is no argument at the specified + * This will return false if there is no argument at the specified * index, i.e. if there are too few arguments. * * @param index the index to check. * @return true if the argument is a byte array; false otherwise. */ boolean isByteArray(int index); + + /** + * Tests whether the argument at the specified index is a table. + *

+ * This will return false if there is no argument at the specified + * index, i.e. if there are too few arguments. + * + * @param index the index to check. + * @return true if the argument is a string; false otherwise. + */ + boolean isTable(int index); } diff --git a/li/cil/oc/server/component/Computer.scala b/li/cil/oc/server/component/Computer.scala index 160373f68..d4231544c 100644 --- a/li/cil/oc/server/component/Computer.scala +++ b/li/cil/oc/server/component/Computer.scala @@ -647,6 +647,7 @@ class Computer(val owner: tileentity.Computer) extends ManagedComponent with Con case LuaType.BOOLEAN => Boolean.box(lua.toBoolean(index)) case LuaType.NUMBER => Double.box(lua.toNumber(index)) case LuaType.STRING => lua.toByteArray(index) + case LuaType.TABLE => lua.toJavaObject(index, classOf[java.util.Map[_, _]]) case _ => Unit } diff --git a/li/cil/oc/server/network/Component.scala b/li/cil/oc/server/network/Component.scala index a94768eac..5e3a3833b 100644 --- a/li/cil/oc/server/network/Component.scala +++ b/li/cil/oc/server/network/Component.scala @@ -3,6 +3,7 @@ package li.cil.oc.server.network import cpw.mods.fml.common.FMLCommonHandler import cpw.mods.fml.relauncher.Side import java.lang.reflect.{Method, InvocationTargetException} +import java.util import li.cil.oc.api import li.cil.oc.api.network._ import li.cil.oc.server.component @@ -204,6 +205,16 @@ object Component { } } + def checkTable(index: Int) = { + checkIndex(index, "table") + args(index) match { + case value: java.util.Map[_, _] => value + case value: Map[_, _] => value + case value: mutable.Map[_, _] => value + case value => throw typeError(index, value, "table") + } + } + def isBoolean(index: Int) = index >= 0 && index < count && (args(index) match { case value: java.lang.Boolean => true @@ -236,6 +247,14 @@ object Component { case _ => false }) + def isTable(index: Int) = + index >= 0 && index < count && (args(index) match { + case value: util.Map[_, _] => true + case value: Map[_, _] => true + case value: mutable.Map[_, _] => true + case _ => false + }) + private def checkIndex(index: Int, name: String) = if (index < 0) throw new IndexOutOfBoundsException() else if (args.length <= index) throw new IllegalArgumentException( @@ -253,6 +272,9 @@ object Component { case _: java.lang.Double => "double" case _: java.lang.String => "string" case _: Array[Byte] => "string" + case value: util.Map[_, _] => "table" + case value: Map[_, _] => "table" + case value: mutable.Map[_, _] => "table" case _ => value.getClass.getSimpleName } }