changeset 57:19ca9c4c6c7f

auto-convert table to java array git-svn-id: https://luan-java.googlecode.com/svn/trunk@58 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 06 Jan 2013 19:17:56 +0000
parents c84274b18f0c
children d6c454b7f58c
files src/luan/LuanJavaFunction.java
diffstat 1 files changed, 22 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
diff -r c84274b18f0c -r 19ca9c4c6c7f src/luan/LuanJavaFunction.java
--- a/src/luan/LuanJavaFunction.java	Fri Jan 04 21:35:55 2013 +0000
+++ b/src/luan/LuanJavaFunction.java	Sun Jan 06 19:17:56 2013 +0000
@@ -372,6 +372,26 @@
 		}
 	};
 
+	private static class ArgArray implements ArgConverter {
+		private final Object[] a;
+
+		ArgArray(Class cls) {
+			a = (Object[])Array.newInstance(cls.getComponentType(),0);
+		}
+
+		public Object convert(Object obj) {
+			if( obj instanceof LuanTable ) {
+				LuanTable t = (LuanTable)obj;
+				if( t.isList() ) {
+					try {
+						return t.asList().toArray(a);
+					} catch(ArrayStoreException e) {}
+				}
+			}
+			return obj;
+		}
+	}
+
 	private static boolean takesLuaState(JavaMethod m) {
 		Class<?>[] paramTypes = m.getParameterTypes();
 		return paramTypes.length > 0 && paramTypes[0].equals(LuanState.class);
@@ -416,6 +436,8 @@
 			return ARG_LIST;
 		if( cls.equals(Set.class) )
 			return ARG_SET;
+		if( cls.isArray() && !cls.getComponentType().isPrimitive() )
+			return new ArgArray(cls);
 		return ARG_SAME;
 	}