comparison src/luan/lib/BasicLib.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 8ede219cd111
children f86e4f77ef32
comparison
equal deleted inserted replaced
63:ebe578282363 64:177cfdc2bdb3
22 public final class BasicLib { 22 public final class BasicLib {
23 23
24 public static void register(LuanState luan) { 24 public static void register(LuanState luan) {
25 LuanTable global = luan.global(); 25 LuanTable global = luan.global();
26 global.put( "_G", global ); 26 global.put( "_G", global );
27 add( global, "do_file", LuanState.class, String.class );
28 add( global, "error", LuanState.class, Object.class );
29 add( global, "get_metatable", LuanState.class, Object.class );
30 add( global, "ipairs", LuanTable.class );
31 add( global, "load", LuanState.class, String.class, String.class );
32 add( global, "load_file", LuanState.class, String.class );
33 add( global, "pairs", LuanTable.class );
34 add( global, "print", LuanState.class, new Object[0].getClass() );
35 add( global, "raw_equal", Object.class, Object.class );
36 add( global, "raw_get", LuanTable.class, Object.class );
37 add( global, "raw_len", LuanState.class, Object.class );
38 add( global, "raw_set", LuanTable.class, Object.class, Object.class );
39 add( global, "set_metatable", LuanTable.class, LuanTable.class );
40 add( global, "to_number", Object.class, Integer.class );
41 add( global, "to_string", LuanState.class, Object.class );
42 add( global, "type", Object.class );
43 global.put( "_VERSION", Luan.version );
44
45 add( global, "make_standard", LuanState.class );
46 }
47
48 private static void add(LuanTable t,String method,Class<?>... parameterTypes) {
49 try { 27 try {
50 t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); 28 global.put( "assert", new LuanJavaFunction(BasicLib.class.getMethod("assert_",LuanState.class,Object.class,String.class),null) );
29 add( global, "assert_boolean", LuanState.class, Boolean.TYPE );
30 add( global, "assert_nil", LuanState.class, Object.class );
31 add( global, "assert_number", LuanState.class, Number.class );
32 add( global, "assert_string", LuanState.class, String.class );
33 add( global, "assert_table", LuanState.class, LuanTable.class );
34 add( global, "do_file", LuanState.class, String.class );
35 add( global, "error", LuanState.class, Object.class );
36 add( global, "get_metatable", LuanState.class, Object.class );
37 add( global, "ipairs", LuanTable.class );
38 add( global, "load", LuanState.class, String.class, String.class );
39 add( global, "load_file", LuanState.class, String.class );
40 add( global, "pairs", LuanTable.class );
41 add( global, "print", LuanState.class, new Object[0].getClass() );
42 add( global, "raw_equal", Object.class, Object.class );
43 add( global, "raw_get", LuanTable.class, Object.class );
44 add( global, "raw_len", LuanState.class, Object.class );
45 add( global, "raw_set", LuanTable.class, Object.class, Object.class );
46 add( global, "set_metatable", LuanTable.class, LuanTable.class );
47 add( global, "to_number", Object.class, Integer.class );
48 add( global, "to_string", LuanState.class, Object.class );
49 add( global, "type", Object.class );
50 global.put( "_VERSION", Luan.version );
51
52 add( global, "make_standard", LuanState.class );
51 } catch(NoSuchMethodException e) { 53 } catch(NoSuchMethodException e) {
52 throw new RuntimeException(e); 54 throw new RuntimeException(e);
53 } 55 }
56 }
57
58 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
59 t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) );
54 } 60 }
55 61
56 public static void make_standard(LuanState luan) { 62 public static void make_standard(LuanState luan) {
57 LuanTable global = luan.global(); 63 LuanTable global = luan.global();
58 global.put( "dofile", global.get("do_file") ); 64 global.put( "dofile", global.get("do_file") );
183 189
184 public static void error(LuanState luan,Object msg) throws LuanException { 190 public static void error(LuanState luan,Object msg) throws LuanException {
185 throw new LuanException(luan,LuanElement.JAVA,msg); 191 throw new LuanException(luan,LuanElement.JAVA,msg);
186 } 192 }
187 193
194 public static Object assert_(LuanState luan,Object v,String msg) throws LuanException {
195 if( Luan.toBoolean(v) )
196 return v;
197 if( msg == null )
198 msg = "assertion failed!";
199 throw new LuanException( luan, LuanElement.JAVA, msg );
200 }
201
202 private static void checkNotNull(LuanState luan,Object v,String expected) throws LuanException {
203 if( v == null )
204 throw new LuanException(luan,LuanElement.JAVA,"bad argument #1 ("+expected+" expected, got nil)");
205 }
206
207 public static String assert_string(LuanState luan,String v) throws LuanException {
208 checkNotNull(luan,v,"string");
209 return v;
210 }
211
212 public static Number assert_number(LuanState luan,Number v) throws LuanException {
213 checkNotNull(luan,v,"number");
214 return v;
215 }
216
217 public static LuanTable assert_table(LuanState luan,LuanTable v) throws LuanException {
218 checkNotNull(luan,v,"table");
219 return v;
220 }
221
222 public static boolean assert_boolean(LuanState luan,boolean v) throws LuanException {
223 return v;
224 }
225
226 public static Object assert_nil(LuanState luan,Object v) throws LuanException {
227 if( v != null )
228 throw new LuanException(luan,LuanElement.JAVA,"bad argument #1 (nil expected, got "+Luan.type(v)+")");
229 return v;
230 }
231
188 } 232 }