Mercurial Hosting > luan
changeset 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 | 1ff53a88579a |
files | src/luan/LuanJavaFunction.java src/luan/lib/BasicLib.java src/luan/lib/JavaLib.java |
diffstat | 3 files changed, 104 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
diff -r ebe578282363 -r 177cfdc2bdb3 src/luan/LuanJavaFunction.java --- a/src/luan/LuanJavaFunction.java Wed Jan 09 22:54:50 2013 +0000 +++ b/src/luan/LuanJavaFunction.java Thu Jan 17 19:29:58 2013 +0000 @@ -81,13 +81,16 @@ private void checkArgs(LuanState luan,Object[] args) throws LuanException { Class<?>[] a = getParameterTypes(); + if( takesLuaState ) { + Class<?>[] t = new Class<?>[a.length-1]; + System.arraycopy(a,1,t,0,t.length); + a = t; + } for( int i=0; i<a.length; i++ ) { if( !a[i].isInstance(args[i]) ) { - String got = args[i].getClass().getName(); - String expected = a[i].getName(); - if( !takesLuaState ) - i++; - throw new LuanException(luan,LuanElement.JAVA,"bad argument #"+i+" ("+expected+" expected, got "+got+")"); + String got = args[i].getClass().getSimpleName(); + String expected = a[i].getSimpleName(); + throw new LuanException(luan,LuanElement.JAVA,"bad argument #"+(i+1)+" ("+expected+" expected, got "+got+")"); } } }
diff -r ebe578282363 -r 177cfdc2bdb3 src/luan/lib/BasicLib.java --- a/src/luan/lib/BasicLib.java Wed Jan 09 22:54:50 2013 +0000 +++ b/src/luan/lib/BasicLib.java Thu Jan 17 19:29:58 2013 +0000 @@ -24,35 +24,41 @@ public static void register(LuanState luan) { LuanTable global = luan.global(); global.put( "_G", global ); - add( global, "do_file", LuanState.class, String.class ); - add( global, "error", LuanState.class, Object.class ); - add( global, "get_metatable", LuanState.class, Object.class ); - add( global, "ipairs", LuanTable.class ); - add( global, "load", LuanState.class, String.class, String.class ); - add( global, "load_file", LuanState.class, String.class ); - add( global, "pairs", LuanTable.class ); - add( global, "print", LuanState.class, new Object[0].getClass() ); - add( global, "raw_equal", Object.class, Object.class ); - add( global, "raw_get", LuanTable.class, Object.class ); - add( global, "raw_len", LuanState.class, Object.class ); - add( global, "raw_set", LuanTable.class, Object.class, Object.class ); - add( global, "set_metatable", LuanTable.class, LuanTable.class ); - add( global, "to_number", Object.class, Integer.class ); - add( global, "to_string", LuanState.class, Object.class ); - add( global, "type", Object.class ); - global.put( "_VERSION", Luan.version ); - - add( global, "make_standard", LuanState.class ); - } - - private static void add(LuanTable t,String method,Class<?>... parameterTypes) { try { - t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); + global.put( "assert", new LuanJavaFunction(BasicLib.class.getMethod("assert_",LuanState.class,Object.class,String.class),null) ); + add( global, "assert_boolean", LuanState.class, Boolean.TYPE ); + add( global, "assert_nil", LuanState.class, Object.class ); + add( global, "assert_number", LuanState.class, Number.class ); + add( global, "assert_string", LuanState.class, String.class ); + add( global, "assert_table", LuanState.class, LuanTable.class ); + add( global, "do_file", LuanState.class, String.class ); + add( global, "error", LuanState.class, Object.class ); + add( global, "get_metatable", LuanState.class, Object.class ); + add( global, "ipairs", LuanTable.class ); + add( global, "load", LuanState.class, String.class, String.class ); + add( global, "load_file", LuanState.class, String.class ); + add( global, "pairs", LuanTable.class ); + add( global, "print", LuanState.class, new Object[0].getClass() ); + add( global, "raw_equal", Object.class, Object.class ); + add( global, "raw_get", LuanTable.class, Object.class ); + add( global, "raw_len", LuanState.class, Object.class ); + add( global, "raw_set", LuanTable.class, Object.class, Object.class ); + add( global, "set_metatable", LuanTable.class, LuanTable.class ); + add( global, "to_number", Object.class, Integer.class ); + add( global, "to_string", LuanState.class, Object.class ); + add( global, "type", Object.class ); + global.put( "_VERSION", Luan.version ); + + add( global, "make_standard", LuanState.class ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } } + private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { + t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); + } + public static void make_standard(LuanState luan) { LuanTable global = luan.global(); global.put( "dofile", global.get("do_file") ); @@ -185,4 +191,42 @@ throw new LuanException(luan,LuanElement.JAVA,msg); } + public static Object assert_(LuanState luan,Object v,String msg) throws LuanException { + if( Luan.toBoolean(v) ) + return v; + if( msg == null ) + msg = "assertion failed!"; + throw new LuanException( luan, LuanElement.JAVA, msg ); + } + + private static void checkNotNull(LuanState luan,Object v,String expected) throws LuanException { + if( v == null ) + throw new LuanException(luan,LuanElement.JAVA,"bad argument #1 ("+expected+" expected, got nil)"); + } + + public static String assert_string(LuanState luan,String v) throws LuanException { + checkNotNull(luan,v,"string"); + return v; + } + + public static Number assert_number(LuanState luan,Number v) throws LuanException { + checkNotNull(luan,v,"number"); + return v; + } + + public static LuanTable assert_table(LuanState luan,LuanTable v) throws LuanException { + checkNotNull(luan,v,"table"); + return v; + } + + public static boolean assert_boolean(LuanState luan,boolean v) throws LuanException { + return v; + } + + public static Object assert_nil(LuanState luan,Object v) throws LuanException { + if( v != null ) + throw new LuanException(luan,LuanElement.JAVA,"bad argument #1 (nil expected, got "+Luan.type(v)+")"); + return v; + } + }
diff -r ebe578282363 -r 177cfdc2bdb3 src/luan/lib/JavaLib.java --- 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(),