changeset 95:9aa7d5f20333

in LuanJavaFunction, fix primitive type checking and add boolean conversion git-svn-id: https://luan-java.googlecode.com/svn/trunk@96 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 08 Mar 2013 06:48:17 +0000
parents c6633e5f0cc5
children 72a4a5550ec7
files src/luan/LuanJavaFunction.java
diffstat 1 files changed, 34 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
diff -r c6633e5f0cc5 -r 9aa7d5f20333 src/luan/LuanJavaFunction.java
--- a/src/luan/LuanJavaFunction.java	Fri Mar 08 06:13:00 2013 +0000
+++ b/src/luan/LuanJavaFunction.java	Fri Mar 08 06:48:17 2013 +0000
@@ -6,6 +6,7 @@
 import java.lang.reflect.InvocationTargetException;
 import java.util.List;
 import java.util.Map;
+import java.util.HashMap;
 import java.util.Set;
 import java.util.Arrays;
 
@@ -84,13 +85,29 @@
 		return rtnConverter.convert(rtn);
 	}
 
+	private static final Map<Class,Class> primitiveMap = new HashMap<Class,Class>();
+	static {
+		primitiveMap.put(Boolean.TYPE,Boolean.class);
+		primitiveMap.put(Character.TYPE,Character.class);
+		primitiveMap.put(Byte.TYPE,Byte.class);
+		primitiveMap.put(Short.TYPE,Short.class);
+		primitiveMap.put(Integer.TYPE,Integer.class);
+		primitiveMap.put(Long.TYPE,Long.class);
+		primitiveMap.put(Float.TYPE,Float.class);
+		primitiveMap.put(Double.TYPE,Double.class);
+		primitiveMap.put(Void.TYPE,Void.class);
+	}
+
 	private void checkArgs(LuanState luan,Object[] args) throws LuanException {
 		Class<?>[] a = getParameterTypes();
 		int start = takesLuaState ? 1 : 0;
 		for( int i=start; i<a.length; i++ ) {
 			Class<?> paramType = a[i];
+			Class<?> type = paramType;
+			if( type.isPrimitive() )
+				type = primitiveMap.get(type);
 			Object arg = args[i];
-			if( !paramType.isInstance(arg) ) {
+			if( !type.isInstance(arg) ) {
 				String expected = paramType.getSimpleName();
 				if( arg==null ) {
 					if( paramType.isPrimitive() )
@@ -211,6 +228,18 @@
 		}
 	};
 
+	private static final ArgConverter ARG_BOOLEAN = new ArgConverter() {
+		public Object convert(Object obj) {
+			return Luan.toBoolean(obj);
+		}
+	};
+
+	private static final ArgConverter ARG_BOOLEAN_OBJ = new ArgConverter() {
+		public Object convert(Object obj) {
+			return obj==null ? null : Luan.toBoolean(obj);
+		}
+	};
+
 	private static final ArgConverter ARG_DOUBLE = new ArgConverter() {
 		public Object convert(Object obj) {
 			if( obj instanceof Double )
@@ -433,6 +462,10 @@
 	}
 
 	private static ArgConverter getArgConverter(Class<?> cls) {
+		if( cls == Boolean.TYPE )
+			return ARG_BOOLEAN;
+		if( cls.equals(Boolean.class) )
+			return ARG_BOOLEAN_OBJ;
 		if( cls == Double.TYPE || cls.equals(Double.class) )
 			return ARG_DOUBLE;
 		if( cls == Float.TYPE || cls.equals(Float.class) )