diff core/src/luan/LuanTable.java @ 502:d3183a330ff5

improve the __index metamethod to work with any type; simplify luan_proxy to eliminate base;
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 19 May 2015 17:57:20 -0600
parents ee55be414a34
children 92c3d22745b8
line wrap: on
line diff
--- a/core/src/luan/LuanTable.java	Mon May 18 23:23:01 2015 -0600
+++ b/core/src/luan/LuanTable.java	Tue May 19 17:57:20 2015 -0600
@@ -13,6 +13,9 @@
 import java.util.HashSet;
 import java.util.IdentityHashMap;
 import java.util.regex.Pattern;
+import luan.modules.StringLuan;
+import luan.modules.BinaryLuan;
+import luan.modules.JavaLuan;
 
 
 public final class LuanTable implements DeepCloneable {
@@ -120,11 +123,25 @@
 			LuanMeta meta = (LuanMeta)h;
 			return meta.__index(luan,this,key);
 		}
-		if( h instanceof LuanTable ) {
-			LuanTable tbl = (LuanTable)h;
+		return index(luan.JAVA,h,key);
+	}
+
+	public static Object index(LuanBit bit,Object obj,Object key) throws LuanException {
+		LuanState luan = bit.luan;
+		if( obj instanceof LuanTable ) {
+			LuanTable tbl = (LuanTable)obj;
 			return tbl.get(luan,key);
 		}
-		throw luan.exception("invalid type "+Luan.type(h)+" for metamethod __index");
+		if( obj instanceof String )
+			return StringLuan.__index(luan,(String)obj,key);
+		if( obj instanceof byte[] )
+			return BinaryLuan.__index(luan,(byte[])obj,key);
+		if( obj != null && luan.currentEnvironment().hasJava() )
+			return JavaLuan.__index(luan,obj,key);
+		else if( bit.el==null )
+			throw bit.exception( "attempt to index a " + Luan.type(obj) + " value" );
+		else
+			throw bit.exception( "attempt to index '"+bit.el.text()+"' (a " + Luan.type(obj) + " value)" );
 	}
 
 	public Object rawGet(Object key) {