changeset 209:239c8d650028

convert returned arrays to tables for java methods git-svn-id: https://luan-java.googlecode.com/svn/trunk@210 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 08 Jul 2014 10:25:35 +0000
parents 5ba136769034
children e84655b4c45e
files core/src/luan/LuanJavaFunction.java core/src/luan/impl/LuanParser.java core/src/luan/modules/JavaLuan.java dist/luan-core-trunk.jar dist/luan-logging-trunk.jar dist/luan-web-trunk.jar
diffstat 6 files changed, 27 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/LuanJavaFunction.java	Tue Jul 08 07:04:47 2014 +0000
+++ b/core/src/luan/LuanJavaFunction.java	Tue Jul 08 10:25:35 2014 +0000
@@ -5,6 +5,7 @@
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.List;
+import java.util.ArrayList;
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Set;
@@ -20,17 +21,21 @@
 	private final Class<?> varArgCls;
 
 	public LuanJavaFunction(Method method,Object obj) {
-		this( JavaMethod.of(method), obj );
+		this(method,obj,false);
+	}
+
+	public LuanJavaFunction(Method method,Object obj,boolean convertArray) {
+		this( JavaMethod.of(method), obj, convertArray );
 	}
 
 	public LuanJavaFunction(Constructor constr,Object obj) {
-		this( JavaMethod.of(constr), obj );
+		this( JavaMethod.of(constr), obj, false );
 	}
 
-	LuanJavaFunction(JavaMethod method,Object obj) {
+	private LuanJavaFunction(JavaMethod method,Object obj,boolean convertArray) {
 		this.method = method;
 		this.obj = obj;
-		this.rtnConverter = getRtnConverter(method);
+		this.rtnConverter = getRtnConverter(method,convertArray);
 		this.takesLuaState = takesLuaState(method);
 		this.argConverters = getArgConverters(takesLuaState,method);
 		if( method.isVarArgs() ) {
@@ -187,10 +192,24 @@
 		}
 	};
 
-	private static RtnConverter getRtnConverter(JavaMethod m) {
+	private static final RtnConverter RTN_ARRAY = new RtnConverter() {
+		@Override public Object convert(Object obj) {
+			if( obj == null )
+				return null;
+			Object[] a = new Object[Array.getLength(obj)];
+			for( int i=0; i<a.length; i++ ) {
+				a[i] = Array.get(obj,i);
+			}
+			return new LuanTable(new ArrayList<Object>(Arrays.asList(a)));
+		}
+	};
+
+	private static RtnConverter getRtnConverter(JavaMethod m,boolean convertArray) {
 		Class<?> rtnType = m.getReturnType();
 		if( rtnType == Void.TYPE )
 			return RTN_NOTHING;
+		if( convertArray && rtnType.isArray() )
+			return RTN_ARRAY;
 		return RTN_SAME;
 	}
 
--- a/core/src/luan/impl/LuanParser.java	Tue Jul 08 07:04:47 2014 +0000
+++ b/core/src/luan/impl/LuanParser.java	Tue Jul 08 10:25:35 2014 +0000
@@ -1060,7 +1060,7 @@
 
 	private Expr SubExpr(In in) throws ParseException {
 		parser.begin();
-		if( !parser.match('[') )
+		if( !parser.match('[') || parser.test("[") || parser.test("=") )
 			return parser.failure(null);
 		In inParens = in.parens();
 		Spaces(inParens);
--- a/core/src/luan/modules/JavaLuan.java	Tue Jul 08 07:04:47 2014 +0000
+++ b/core/src/luan/modules/JavaLuan.java	Tue Jul 08 10:25:35 2014 +0000
@@ -141,13 +141,13 @@
 					return rtn instanceof Object[] ? Arrays.asList((Object[])rtn) : rtn;
 				} else {
 					Method method = (Method)member;
-					return new LuanJavaFunction(method,obj);
+					return new LuanJavaFunction(method,obj,true);
 				}
 			} else {
 				List<LuanJavaFunction> fns = new ArrayList<LuanJavaFunction>();
 				for( Member member : members ) {
 					Method method = (Method)member;
-					fns.add(new LuanJavaFunction(method,obj));
+					fns.add(new LuanJavaFunction(method,obj,true));
 				}
 				return new AmbiguousJavaFunction(fns);
 			}
Binary file dist/luan-core-trunk.jar has changed
Binary file dist/luan-logging-trunk.jar has changed
Binary file dist/luan-web-trunk.jar has changed