changeset 509:e3b0846dc2ef

throw exception for invalid indexes of string, binary, or java
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 22 May 2015 02:02:49 -0600
parents 9218f9cf45d3
children 2da0bcb979b5
files core/src/luan/LuanBit.java core/src/luan/LuanState.java core/src/luan/LuanTable.java core/src/luan/impl/ConcatExpr.java core/src/luan/modules/BinaryLuan.java core/src/luan/modules/JavaLuan.java core/src/luan/modules/StringLuan.java http/src/luan/modules/http/Http.luan
diffstat 8 files changed, 74 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/LuanBit.java	Thu May 21 20:20:54 2015 -0600
+++ b/core/src/luan/LuanBit.java	Fri May 22 02:02:49 2015 -0600
@@ -84,20 +84,6 @@
 		}
 	}
 
-	public String toString(Object obj) throws LuanException {
-		if( obj instanceof LuanTable ) {
-			LuanTable tbl = (LuanTable)obj;
-			return tbl.toString(luan);
-		}
-		if( obj == null )
-			return "nil";
-		if( obj instanceof Number )
-			return Luan.toString((Number)obj);
-		if( obj instanceof byte[] )
-			return "binary: " + Integer.toHexString(obj.hashCode());
-		return obj.toString();
-	}
-
 	public LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
 		Object f = t.getHandler(op);
 		if( f == null )
--- a/core/src/luan/LuanState.java	Thu May 21 20:20:54 2015 -0600
+++ b/core/src/luan/LuanState.java	Fri May 22 02:02:49 2015 -0600
@@ -45,6 +45,20 @@
 		return call(fn);
 	}
 
+	public String toString(Object obj) throws LuanException {
+		if( obj instanceof LuanTable ) {
+			LuanTable tbl = (LuanTable)obj;
+			return tbl.toString(this);
+		}
+		if( obj == null )
+			return "nil";
+		if( obj instanceof Number )
+			return Luan.toString((Number)obj);
+		if( obj instanceof byte[] )
+			return "binary: " + Integer.toHexString(obj.hashCode());
+		return obj.toString();
+	}
+
 	public final LuanBit bit(LuanElement el) {
 		return new LuanBit(this,el);
 	}
@@ -85,10 +99,6 @@
 		return JAVA.checkFunction(obj);
 	}
 
-	public String toString(Object obj) throws LuanException {
-		return JAVA.toString(obj);
-	}
-
 	public boolean isLessThan(Object o1,Object o2) throws LuanException {
 		return JAVA.isLessThan(o1,o2);
 	}
--- a/core/src/luan/LuanTable.java	Thu May 21 20:20:54 2015 -0600
+++ b/core/src/luan/LuanTable.java	Fri May 22 02:02:49 2015 -0600
@@ -131,11 +131,11 @@
 			return tbl.get(luan,key);
 		}
 		if( obj instanceof String )
-			return StringLuan.__index(luan,(String)obj,key);
+			return StringLuan.__index(bit,(String)obj,key);
 		if( obj instanceof byte[] )
-			return BinaryLuan.__index(luan,(byte[])obj,key);
+			return BinaryLuan.__index(bit,(byte[])obj,key);
 		if( obj != null && luan.hasJava() )
-			return JavaLuan.__index(luan,obj,key);
+			return JavaLuan.__index(bit,obj,key);
 		else if( bit.el==null )
 			throw bit.exception( "attempt to index a " + Luan.type(obj) + " value" );
 		else
--- a/core/src/luan/impl/ConcatExpr.java	Thu May 21 20:20:54 2015 -0600
+++ b/core/src/luan/impl/ConcatExpr.java	Fri May 22 02:02:49 2015 -0600
@@ -20,8 +20,8 @@
 		LuanFunction fn = bit.getBinHandler("__concat",o1,o2);
 		if( fn != null )
 			return Luan.first(bit.call(fn,"__concat",new Object[]{o1,o2}));
-		String s1 = luan.bit(op1.el()).toString(o1);
-		String s2 = luan.bit(op2.el()).toString(o2);
+		String s1 = luan.toString(o1);
+		String s2 = luan.toString(o2);
 		return s1 + s2;
 	}
 }
--- a/core/src/luan/modules/BinaryLuan.java	Thu May 21 20:20:54 2015 -0600
+++ b/core/src/luan/modules/BinaryLuan.java	Fri May 22 02:02:49 2015 -0600
@@ -6,11 +6,13 @@
 import luan.LuanFunction;
 import luan.LuanException;
 import luan.LuanMethod;
+import luan.LuanBit;
 
 
 public final class BinaryLuan {
 
-	public static Object __index(LuanState luan,final byte[] binary,Object key) throws LuanException {
+	public static Object __index(LuanBit bit,final byte[] binary,Object key) throws LuanException {
+		LuanState luan = bit.luan;
 		LuanTable mod = (LuanTable)PackageLuan.require(luan,"luan:Binary");
 		Object obj = mod.get(luan,key);
 		if( obj instanceof LuanFunction ) {
@@ -24,7 +26,10 @@
 				}
 			};
 		}
-		return null;
+		if( bit.el != null )
+			throw bit.exception( "invalid index ["+luan.toString(key)+"] for binary ("+bit.el.text()+")" ) ;
+		else
+			throw bit.exception( "invalid index ["+luan.toString(key)+"] for binary") ;
 	}
 
 	static int start(byte[] binary,int i) {
--- a/core/src/luan/modules/JavaLuan.java	Thu May 21 20:20:54 2015 -0600
+++ b/core/src/luan/modules/JavaLuan.java	Fri May 22 02:02:49 2015 -0600
@@ -23,6 +23,7 @@
 import luan.LuanFunction;
 import luan.LuanJavaFunction;
 import luan.LuanElement;
+import luan.LuanBit;
 
 
 public final class JavaLuan {
@@ -46,6 +47,19 @@
 			throw luan.exception("Java isn't allowed");
 	}
 
+	static final Object FAIL = new Object();
+
+	public static Object __index(LuanBit bit,Object obj,Object key) 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 ["+luan.toString(key)+"] for ("+bit.el.text()+") java: "+obj.getClass() );
+		else
+			throw bit.exception( "invalid index ["+luan.toString(key)+"] for java: "+obj.getClass() );
+	}
+
 	public static Object __index(LuanState luan,Object obj,Object key) throws LuanException {
 		checkJava(luan);
 		if( obj instanceof Static ) {
@@ -81,33 +95,32 @@
 					}
 				}
 			}
-			throw luan.exception("invalid member '"+key+"' for: "+obj);
-		}
-		Class cls = obj.getClass();
-		if( cls.isArray() ) {
-			if( "length".equals(key) ) {
-				return Array.getLength(obj);
-			}
-			Integer i = Luan.asInteger(key);
-			if( i != null ) {
-				return Array.get(obj,i);
+		} else {
+			Class cls = obj.getClass();
+			if( cls.isArray() ) {
+				if( "length".equals(key) ) {
+					return Array.getLength(obj);
+				}
+				Integer i = Luan.asInteger(key);
+				if( i != null ) {
+					return Array.get(obj,i);
+				}
+				throw luan.exception("invalid member '"+key+"' for java array: "+obj);
 			}
-			throw luan.exception("invalid member '"+key+"' for java array: "+obj);
-		}
-		if( key instanceof String ) {
-			String name = (String)key;
-			if( "instanceof".equals(name) ) {
-				return new LuanJavaFunction(instanceOf,new InstanceOf(obj));
-			} else {
-				List<Member> members = getMembers(cls,name);
-				if( !members.isEmpty() ) {
-					return member(obj,members);
+			if( key instanceof String ) {
+				String name = (String)key;
+				if( "instanceof".equals(name) ) {
+					return new LuanJavaFunction(instanceOf,new InstanceOf(obj));
+				} else {
+					List<Member> members = getMembers(cls,name);
+					if( !members.isEmpty() ) {
+						return member(obj,members);
+					}
 				}
 			}
 		}
-//		throw luan.exception("invalid member '"+key+"' for java object: "+obj);
 //System.out.println("invalid member '"+key+"' for java object: "+obj);
-		return null;
+		return FAIL;
 	}
 
 	private static Object member(Object obj,List<Member> members) throws LuanException {
--- a/core/src/luan/modules/StringLuan.java	Thu May 21 20:20:54 2015 -0600
+++ b/core/src/luan/modules/StringLuan.java	Fri May 22 02:02:49 2015 -0600
@@ -10,11 +10,13 @@
 import luan.LuanElement;
 import luan.LuanException;
 import luan.LuanMethod;
+import luan.LuanBit;
 
 
 public final class StringLuan {
 
-	public static Object __index(LuanState luan,final String s,Object key) throws LuanException {
+	public static Object __index(LuanBit bit,final String s,Object key) throws LuanException {
+		LuanState luan = bit.luan;
 		LuanTable mod = (LuanTable)PackageLuan.require(luan,"luan:String");
 		Object obj = mod.get(luan,key);
 		if( obj instanceof LuanFunction ) {
@@ -28,9 +30,15 @@
 				}
 			};
 		}
-		if( luan.hasJava() )
-			return JavaLuan.__index(luan,s,key);
-		return null;
+		if( luan.hasJava() ) {
+			Object rtn = JavaLuan.__index(luan,s,key);
+			if( rtn != JavaLuan.FAIL )
+				return rtn;
+		}
+		if( bit.el != null )
+			throw bit.exception( "invalid index ["+luan.toString(key)+"] for string ("+bit.el.text()+")" ) ;
+		else
+			throw bit.exception( "invalid index ["+luan.toString(key)+"] for string") ;
 	}
 
 	static int start(String s,int i) {
--- a/http/src/luan/modules/http/Http.luan	Thu May 21 20:20:54 2015 -0600
+++ b/http/src/luan/modules/http/Http.luan	Fri May 22 02:02:49 2015 -0600
@@ -16,7 +16,7 @@
 
 function singular_metatable.__index(table,key)
 	local list = table.__plural[key]
-	return list and (list[1] or error("invalid value "..list.." for "..key))
+	return list and list[1]
 end
 
 function singular_metatable.__new_index(table,key,value)