changeset 447:0bd42e774c50

add assert_binary; improve wrong type messages;
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 04 May 2015 17:19:43 -0600
parents bbad2d06f728
children 8b2db645b9b2
files core/src/luan/LuanJavaFunction.java core/src/luan/modules/BasicLuan.java core/src/luan/modules/Luan.luan
diffstat 3 files changed, 29 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
diff -r bbad2d06f728 -r 0bd42e774c50 core/src/luan/LuanJavaFunction.java
--- a/core/src/luan/LuanJavaFunction.java	Mon May 04 16:21:17 2015 -0600
+++ b/core/src/luan/LuanJavaFunction.java	Mon May 04 17:19:43 2015 -0600
@@ -126,20 +126,40 @@
 				type = (Class)primitiveMap.get(type);
 			Object arg = args[i];
 			if( !type.isInstance(arg) ) {
-				String expected = paramType.getSimpleName();
+				String expected;
 				if( i==a.length-1 && method.isVarArgs() )
-					expected = paramType.getComponentType().getSimpleName()+"...";
+					expected = fixType(paramType.getComponentType().getSimpleName())+"...";
+				else
+					expected = fixType(paramType.getSimpleName());
 				if( arg==null ) {
 					if( paramType.isPrimitive() )
 						throw luan.exception("bad argument #"+(i+1-start)+" ("+expected+" expected, got nil)");
 				} else {
-					String got = arg.getClass().getSimpleName();
+					String got = fixType(arg.getClass().getSimpleName());
 					throw luan.exception("bad argument #"+(i+1-start)+" ("+expected+" expected, got "+got+")");
 				}
 			}
 		}
 	}
 
+	private static String fixType(String type) {
+		if( type.equals("byte[]") )
+			return "binary";
+		if( type.equals("Double") )
+			return "number";
+		if( type.equals("LuanTable") )
+			return "table";
+		if( type.equals("Boolean") )
+			return "boolean";
+		if( type.equals("String") )
+			return "string";
+		if( type.equals("Closure") )
+			return "function";
+		if( type.equals("LuanJavaFunction") )
+			return "function";
+		return type;
+	}
+
 	private Object[] fixArgs(LuanState luan,Object[] args) throws LuanException {
 		int n = argConverters.length;
 		Object[] rtn;
diff -r bbad2d06f728 -r 0bd42e774c50 core/src/luan/modules/BasicLuan.java
--- a/core/src/luan/modules/BasicLuan.java	Mon May 04 16:21:17 2015 -0600
+++ b/core/src/luan/modules/BasicLuan.java	Mon May 04 17:19:43 2015 -0600
@@ -153,6 +153,11 @@
 		return v;
 	}
 
+	@LuanMethod public static byte[] assert_binary(LuanState luan,byte[] v) throws LuanException {
+		Utils.checkNotNull(luan,v);
+		return v;
+	}
+
 	public static LuanFunction range(LuanState luan,final double from,final double to,Double stepV) throws LuanException {
 		final double step = stepV==null ? 1.0 : stepV;
 		if( step == 0.0 )
diff -r bbad2d06f728 -r 0bd42e774c50 core/src/luan/modules/Luan.luan
--- a/core/src/luan/modules/Luan.luan	Mon May 04 16:21:17 2015 -0600
+++ b/core/src/luan/modules/Luan.luan	Mon May 04 17:19:43 2015 -0600
@@ -1,6 +1,7 @@
 java()
 local BasicLuan = require "java:luan.modules.BasicLuan"
 
+assert_binary = BasicLuan.assert_binary
 assert_boolean = BasicLuan.assert_boolean
 assert_integer = BasicLuan.assert_integer
 assert_long = BasicLuan.assert_long