diff core/src/luan/impl/IndexExpr.java @ 404:d55e873e1f0d

metatables now only apply to tables
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 29 Apr 2015 07:04:40 -0600
parents 3dcb0f9bee82
children 3e68917a0dc6
line wrap: on
line diff
--- a/core/src/luan/impl/IndexExpr.java	Tue Apr 28 22:49:33 2015 -0600
+++ b/core/src/luan/impl/IndexExpr.java	Wed Apr 29 07:04:40 2015 -0600
@@ -5,6 +5,9 @@
 import luan.LuanTable;
 import luan.LuanFunction;
 import luan.LuanSource;
+import luan.modules.StringLuan;
+import luan.modules.BinaryLuan;
+import luan.modules.JavaLuan;
 
 
 final class IndexExpr extends BinaryOpExpr {
@@ -17,25 +20,28 @@
 		return index(luan,op1.eval(luan),op2.eval(luan));
 	}
 
-	private Object index(LuanStateImpl luan,Object t,Object key) throws LuanException {
-		Object h;
-		if( t instanceof LuanTable ) {
-			LuanTable tbl = (LuanTable)t;
+	private Object index(LuanStateImpl luan,Object obj,Object key) throws LuanException {
+		if( obj instanceof LuanTable ) {
+			LuanTable tbl = (LuanTable)obj;
 			Object value = tbl.get(key);
 			if( value != null )
 				return value;
-			h = luan.getHandler("__index",t);
+			Object h = luan.getHandler("__index",tbl);
 			if( h==null )
 				return null;
-		} else {
-			h = luan.getHandler("__index",t);
-			if( h==null )
-				throw luan.bit(op1.se()).exception( "attempt to index '"+op1.se().text()+"' (a " + Luan.type(t) + " value)" );
+			if( h instanceof LuanFunction ) {
+				LuanFunction fn = (LuanFunction)h;
+				return Luan.first(luan.bit(se).call(fn,"__index",new Object[]{tbl,key}));
+			}
+			return index(luan,h,key);
 		}
-		if( h instanceof LuanFunction ) {
-			LuanFunction fn = (LuanFunction)h;
-			return Luan.first(luan.bit(se).call(fn,"__index",new Object[]{t,key}));
-		}
-		return index(luan,h,key);
+		if( obj instanceof String )
+			return StringLuan.__index(luan,(String)obj,key);
+		if( obj instanceof byte[] )
+			return BinaryLuan.__index(luan,(byte[])obj,key);
+		Object value = JavaLuan.__index(luan,obj,key);
+		if( value != null )
+			return value;
+		throw luan.bit(op1.se()).exception( "attempt to index '"+op1.se().text()+"' (a " + Luan.type(obj) + " value)" );
 	}
 }