diff src/luan/lib/JavaLib.java @ 64:177cfdc2bdb3

add type assertions git-svn-id: https://luan-java.googlecode.com/svn/trunk@65 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 17 Jan 2013 19:29:58 +0000
parents ebe578282363
children 5a93129995e1
line wrap: on
line diff
--- a/src/luan/lib/JavaLib.java	Wed Jan 09 22:54:50 2013 +0000
+++ b/src/luan/lib/JavaLib.java	Thu Jan 17 19:29:58 2013 +0000
@@ -84,6 +84,8 @@
 							return new AmbiguousJavaFunction(fns);
 						}
 					}
+				} else if( "assert".equals(name) ) {
+					return new LuanJavaFunction(assertClass,new AssertClass(cls));
 				} else {
 					List<Member> members = getStaticMembers(cls,name);
 					if( !members.isEmpty() ) {
@@ -344,6 +346,33 @@
 	}
 
 
+	private static class AssertClass {
+		private final Class cls;
+
+		AssertClass(Class cls) {
+			this.cls = cls;
+		}
+
+		public Object assertClass(LuanState luan,Object v) throws LuanException {
+			if( !cls.isInstance(v) ) {
+				String got = v.getClass().getSimpleName();
+				String expected = cls.getSimpleName();
+				throw new LuanException(luan,LuanElement.JAVA,"bad argument #1 ("+expected+" expected, got "+got+")");
+			}
+			return v;
+		}
+	}
+	private static final Method assertClass;
+	static {
+		try {
+			assertClass = AssertClass.class.getMethod("assertClass",LuanState.class,Object.class);
+			assertClass.setAccessible(true);
+		} catch(NoSuchMethodException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+
 	public static Object proxy(final LuanState luan,Static st,final LuanTable t,final Object base) throws LuanException {
 		return Proxy.newProxyInstance(
 			st.cls.getClassLoader(),