diff core/src/luan/modules/JavaLuan.java @ 511:e3fb9768dbb3

better error messages
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 22 May 2015 10:47:56 -0600
parents 2da0bcb979b5
children f1601a4ce1aa
line wrap: on
line diff
--- a/core/src/luan/modules/JavaLuan.java	Fri May 22 02:28:15 2015 -0600
+++ b/core/src/luan/modules/JavaLuan.java	Fri May 22 10:47:56 2015 -0600
@@ -49,24 +49,15 @@
 
 	static final Object FAIL = new Object();
 
-	public static Object __index(LuanBit bit,Object obj,Object key) throws LuanException {
+	public static Object __index(LuanBit bit,Object obj,Object key,boolean canReturnFail) throws LuanException {
 		LuanState luan = bit.luan;
-		Object rtn = __index(luan,obj,key);
-		if( rtn != FAIL )
-			return rtn;
-		if( bit.el != null )
-			throw bit.exception( "invalid index for java "+obj.getClass()+" in '"+bit.el.text()+"'" );
-		else
-			throw bit.exception( "invalid index for java "+obj.getClass() );
-	}
-
-	public static Object __index(LuanState luan,Object obj,Object key) throws LuanException {
 		checkJava(luan);
+		Class cls;
 		if( obj instanceof Static ) {
+			Static st = (Static)obj;
+			cls = st.cls;
 			if( key instanceof String ) {
 				String name = (String)key;
-				Static st = (Static)obj;
-				Class cls = st.cls;
 				if( "class".equals(name) ) {
 					return cls;
 				} else if( "new".equals(name) ) {
@@ -96,7 +87,7 @@
 				}
 			}
 		} else {
-			Class cls = obj.getClass();
+			cls = obj.getClass();
 			if( cls.isArray() ) {
 				if( "length".equals(key) ) {
 					return Array.getLength(obj);
@@ -119,7 +110,12 @@
 			}
 		}
 //System.out.println("invalid member '"+key+"' for java object: "+obj);
-		return FAIL;
+		if( canReturnFail )
+			return FAIL;
+		if( bit.el != null )
+			throw bit.exception( "invalid index for java "+cls+" in '"+bit.el.text()+"'" );
+		else
+			throw bit.exception( "invalid index for java "+cls );
 	}
 
 	private static Object member(Object obj,List<Member> members) throws LuanException {
@@ -149,13 +145,15 @@
 		}
 	}
 
-	public static void __new_index(LuanState luan,Object obj,Object key,Object value) throws LuanException {
+	public static void __new_index(LuanBit bit,Object obj,Object key,Object value) throws LuanException {
+		LuanState luan = bit.luan;
 		checkJava(luan);
+		Class cls;
 		if( obj instanceof Static ) {
+			Static st = (Static)obj;
+			cls = st.cls;
 			if( key instanceof String ) {
 				String name = (String)key;
-				Static st = (Static)obj;
-				Class cls = st.cls;
 				List<Member> members = getStaticMembers(cls,name);
 				if( !members.isEmpty() ) {
 					if( members.size() != 1 )
@@ -164,28 +162,31 @@
 					return;
 				}
 			}
-			throw luan.exception("invalid member '"+key+"' for: "+obj);
-		}
-		Class cls = obj.getClass();
-		if( cls.isArray() ) {
-			Integer i = Luan.asInteger(key);
-			if( i != null ) {
-				Array.set(obj,i,value);
-				return;
-			}
-			throw luan.exception("invalid member '"+key+"' for java array: "+obj);
-		}
-		if( key instanceof String ) {
-			String name = (String)key;
-			List<Member> members = getMembers(cls,name);
-			if( !members.isEmpty() ) {
-				if( members.size() != 1 )
-					throw new RuntimeException("not field '"+name+"' of "+obj);
-				setMember(obj,members,value);
-				return;
+//			throw luan.exception("invalid member '"+key+"' for: "+obj);
+		} else {
+			cls = obj.getClass();
+			if( cls.isArray() ) {
+				Integer i = Luan.asInteger(key);
+				if( i != null ) {
+					Array.set(obj,i,value);
+					return;
+				}
+//				throw luan.exception("invalid member '"+key+"' for java array: "+obj);
+			} else if( key instanceof String ) {
+				String name = (String)key;
+				List<Member> members = getMembers(cls,name);
+				if( !members.isEmpty() ) {
+					if( members.size() != 1 )
+						throw new RuntimeException("not field '"+name+"' of "+obj);
+					setMember(obj,members,value);
+					return;
+				}
 			}
 		}
-		throw luan.exception("invalid member '"+key+"' for java object: "+obj);
+		if( bit.el != null )
+			throw bit.exception( "invalid index for java "+cls+" in '"+bit.el.text()+"'" );
+		else
+			throw bit.exception( "invalid index for java "+cls );
 	}
 
 	private static void setMember(Object obj,List<Member> members,Object value) {