added support for tables as arguments to lua callbacks (will have to double check I didn't assume anywhere that this wouldn't happen...)

This commit is contained in:
Florian Nücke 2014-01-03 15:16:53 +01:00
parent 3a63b638ab
commit 666df3e74b
3 changed files with 53 additions and 5 deletions

View File

@ -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}.
* <p/>
@ -99,10 +101,22 @@ public interface Arguments extends Iterable<Object> {
*/
byte[] checkByteArray(int index);
/**
* Try to get a table at the specified index.
* <p/>
* 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.
* <p/>
* This will return true if there is <em>no</em> argument at the specified
* This will return false if there is <em>no</em> 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<Object> {
/**
* Tests whether the argument at the specified index is an integer value.
* <p/>
* This will return true if there is <em>no</em> argument at the specified
* This will return false if there is <em>no</em> 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<Object> {
/**
* Tests whether the argument at the specified index is a double value.
* <p/>
* This will return true if there is <em>no</em> argument at the specified
* This will return false if there is <em>no</em> 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<Object> {
/**
* Tests whether the argument at the specified index is a string value.
* <p/>
* This will return true if there is <em>no</em> argument at the specified
* This will return false if there is <em>no</em> 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<Object> {
/**
* Tests whether the argument at the specified index is a byte array.
* <p/>
* This will return true if there is <em>no</em> argument at the specified
* This will return false if there is <em>no</em> 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.
* <p/>
* This will return false if there is <em>no</em> 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);
}

View File

@ -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
}

View File

@ -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
}
}