changeset 511:e3fb9768dbb3

better error messages
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 22 May 2015 10:47:56 -0600
parents 2da0bcb979b5
children d96944467ffc
files core/src/luan/LuanTable.java core/src/luan/impl/SetTableEntry.java core/src/luan/modules/JavaLuan.java core/src/luan/modules/StringLuan.java
diffstat 4 files changed, 45 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/LuanTable.java	Fri May 22 02:28:15 2015 -0600
+++ b/core/src/luan/LuanTable.java	Fri May 22 10:47:56 2015 -0600
@@ -135,7 +135,7 @@
 		if( obj instanceof byte[] )
 			return BinaryLuan.__index(bit,(byte[])obj,key);
 		if( obj != null && luan.hasJava() )
-			return JavaLuan.__index(bit,obj,key);
+			return JavaLuan.__index(bit,obj,key,false);
 		else if( bit.el==null )
 			throw bit.exception( "attempt to index a " + Luan.type(obj) + " value" );
 		else
--- a/core/src/luan/impl/SetTableEntry.java	Fri May 22 02:28:15 2015 -0600
+++ b/core/src/luan/impl/SetTableEntry.java	Fri May 22 10:47:56 2015 -0600
@@ -6,6 +6,7 @@
 import luan.LuanFunction;
 import luan.LuanElement;
 import luan.LuanMeta;
+import luan.LuanBit;
 import luan.modules.JavaLuan;
 
 
@@ -29,10 +30,11 @@
 			tbl.put(luan,key,value);
 			return;
 		}
+		LuanBit bit = luan.bit(el());
 		if( t != null && luan.hasJava() )
-			JavaLuan.__new_index(luan,t,key,value);
+			JavaLuan.__new_index(bit,t,key,value);
 		else
-			throw luan.bit(el).exception( "attempt to index '"+tableExpr.el().text()+"' (a " + Luan.type(t) + " value)" );
+			throw bit.exception( "attempt to index a " + Luan.type(t) + " value in '"+bit.el.text()+"'" );
 	}
 
 }
--- 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) {
--- a/core/src/luan/modules/StringLuan.java	Fri May 22 02:28:15 2015 -0600
+++ b/core/src/luan/modules/StringLuan.java	Fri May 22 10:47:56 2015 -0600
@@ -31,7 +31,7 @@
 			};
 		}
 		if( luan.hasJava() ) {
-			Object rtn = JavaLuan.__index(luan,s,key);
+			Object rtn = JavaLuan.__index(bit,s,key,true);
 			if( rtn != JavaLuan.FAIL )
 				return rtn;
 		}