diff core/src/luan/LuanTable.java @ 572:f1601a4ce1aa

fix stack when calling meta-methods
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 12 Jul 2015 21:34:23 -0600
parents 473e456444ff
children 7c3ad6db8ac3
line wrap: on
line diff
--- a/core/src/luan/LuanTable.java	Wed Jul 08 23:10:52 2015 -0600
+++ b/core/src/luan/LuanTable.java	Sun Jul 12 21:34:23 2015 -0600
@@ -92,23 +92,23 @@
 		return list!=null ? list : Collections.emptyList();
 	}
 
-	public String toString(LuanState luan) throws LuanException {
+	public String toString(LuanBit bit) throws LuanException {
 		Object h = getHandler("__to_string");
 		if( h == null )
 			return rawToString();
 		if( h instanceof LuanMeta ) {
 			LuanMeta meta = (LuanMeta)h;
-			return meta.__to_string(luan,this);
+			return meta.__to_string(bit.luan,this);
 		}
-		LuanFunction fn = luan.checkFunction(h);
-		return luan.checkString( Luan.first( luan.call(fn,"__to_string",new Object[]{this}) ) );
+		LuanFunction fn = bit.checkFunction(h);
+		return bit.checkString( Luan.first( bit.call(fn,"__to_string",new Object[]{this}) ) );
 	}
 
 	public String rawToString() {
 		return "table: " + Integer.toHexString(hashCode());
 	}
 
-	public Object get(LuanState luan,Object key) throws LuanException {
+	public Object get(LuanBit bit,Object key) throws LuanException {
 		Object value = rawGet(key);
 		if( value != null )
 			return value;
@@ -117,22 +117,21 @@
 			return null;
 		if( h instanceof LuanFunction ) {
 			LuanFunction fn = (LuanFunction)h;
-			return Luan.first(luan.call(fn,"__index",new Object[]{this,key}));
+			return Luan.first(bit.call(fn,"__index",new Object[]{this,key}));
 		}
 		if( h instanceof LuanMeta ) {
 			LuanMeta meta = (LuanMeta)h;
-			return meta.__index(luan,this,key);
+			return meta.__index(bit.luan,this,key);
 		}
-		return index(luan.JAVA,h,key);
+		return index(bit,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);
+			return tbl.get(bit,key);
 		}
-		if( obj != null && luan.hasJava() )
+		if( obj != null && bit.luan.hasJava() )
 			return JavaLuan.__index(bit,obj,key,false);
 		else if( bit.el==null )
 			throw bit.exception( "attempt to index a " + Luan.type(obj) + " value" );
@@ -158,7 +157,7 @@
 		return map.get(key);
 	}
 
-	public void put(LuanState luan,Object key,Object value) throws LuanException {
+	public void put(LuanBit bit,Object key,Object value) throws LuanException {
 		Object h = getHandler("__new_index");
 		if( h==null || rawGet(key)!=null ) {
 			rawPut(key,value);
@@ -166,20 +165,20 @@
 		}
 		if( h instanceof LuanFunction ) {
 			LuanFunction fn = (LuanFunction)h;
-			luan.call(fn,"__new_index",new Object[]{this,key,value});
+			bit.call(fn,"__new_index",new Object[]{this,key,value});
 			return;
 		}
 		if( h instanceof LuanMeta ) {
 			LuanMeta meta = (LuanMeta)h;
-			meta.__new_index(luan,this,key,value);
+			meta.__new_index(bit.luan,this,key,value);
 			return;
 		}
 		if( h instanceof LuanTable ) {
 			LuanTable tbl = (LuanTable)h;
-			tbl.put(luan,key,value);
+			tbl.put(bit,key,value);
 			return;
 		}
-		throw luan.exception("invalid type "+Luan.type(h)+" for metamethod __new_index");
+		throw bit.exception("invalid type "+Luan.type(h)+" for metamethod __new_index");
 	}
 
 	public void rawPut(Object key,Object val) {
@@ -263,11 +262,11 @@
 		Collections.sort(list(),cmp);
 	}
 
-	public int length(LuanState luan) throws LuanException {
+	public int length(LuanBit bit) throws LuanException {
 		Object h = getHandler("__len");
 		if( h != null ) {
-			LuanFunction fn = luan.checkFunction(h);
-			return (Integer)Luan.first(luan.call(fn,"__len",new Object[]{this}));
+			LuanFunction fn = bit.checkFunction(h);
+			return (Integer)Luan.first(bit.call(fn,"__len",new Object[]{this}));
 		}
 		return rawLength();
 	}
@@ -276,8 +275,8 @@
 		return list==null ? 0 : list.size();
 	}
 
-	public Iterable<Map.Entry<Object,Object>> iterable(LuanState luan) throws LuanException {
-		final Iterator<Map.Entry<Object,Object>> iter = iterator(luan);
+	public Iterable<Map.Entry<Object,Object>> iterable(LuanBit bit) throws LuanException {
+		final Iterator<Map.Entry<Object,Object>> iter = iterator(bit);
 		return new Iterable<Map.Entry<Object,Object>>() {
 			public Iterator<Map.Entry<Object,Object>> iterator() {
 				return iter;
@@ -294,16 +293,16 @@
 		};
 	}
 
-	public Iterator<Map.Entry<Object,Object>> iterator(final LuanState luan) throws LuanException {
+	public Iterator<Map.Entry<Object,Object>> iterator(final LuanBit bit) throws LuanException {
 		if( getHandler("__pairs") == null )
 			return rawIterator();
-		final LuanFunction fn = pairs(luan);
+		final LuanFunction fn = pairs(bit);
 		return new Iterator<Map.Entry<Object,Object>>() {
 			private Map.Entry<Object,Object> next = getNext();
 
 			private Map.Entry<Object,Object> getNext() {
 				try {
-					Object obj = luan.call(fn);
+					Object obj = bit.call(fn,null,LuanFunction.NOTHING);
 					if( obj==null )
 						return null;
 					Object[] a = (Object[])obj;
@@ -331,20 +330,20 @@
 		};
 	}
 
-	public LuanFunction pairs(final LuanState luan) throws LuanException {
+	public LuanFunction pairs(LuanBit bit) throws LuanException {
 		Object h = getHandler("__pairs");
 		if( h != null ) {
 			if( h instanceof LuanFunction ) {
-				Object obj = Luan.first(luan.call((LuanFunction)h,"__pairs",new Object[]{this}));
+				Object obj = Luan.first(bit.call((LuanFunction)h,"__pairs",new Object[]{this}));
 				if( !(obj instanceof LuanFunction) )
-					throw luan.exception( "metamethod __pairs should return function but returned " + Luan.type(obj) );
+					throw bit.exception( "metamethod __pairs should return function but returned " + Luan.type(obj) );
 				return (LuanFunction)obj;
 			}
 			if( h instanceof LuanMeta ) {
 				LuanMeta meta = (LuanMeta)h;
-				return meta.__pairs(luan,this);
+				return meta.__pairs(bit.luan,this);
 			}
-			throw luan.exception( "invalid type of metamethod __pairs: " + Luan.type(h) );
+			throw bit.exception( "invalid type of metamethod __pairs: " + Luan.type(h) );
 		}
 		return rawPairs();
 	}
@@ -437,25 +436,25 @@
 		return new LinkedHashMap<Object,Object>();
 	}
 
-	public boolean isSet(LuanState luan) throws LuanException {
-		for( Map.Entry<Object,Object> entry : iterable(luan) ) {
+	public boolean isSet(LuanBit bit) throws LuanException {
+		for( Map.Entry<Object,Object> entry : iterable(bit) ) {
 			if( !entry.getValue().equals(Boolean.TRUE) )
 				return false;
 		}
 		return true;
 	}
 
-	public Set<Object> asSet(LuanState luan) throws LuanException {
+	public Set<Object> asSet(LuanBit bit) throws LuanException {
 		Set<Object> set = new HashSet<Object>();
-		for( Map.Entry<Object,Object> entry : iterable(luan) ) {
+		for( Map.Entry<Object,Object> entry : iterable(bit) ) {
 			set.add(entry.getKey());
 		}
 		return set;
 	}
 
-	public Map<Object,Object> asMap(LuanState luan) throws LuanException {
+	public Map<Object,Object> asMap(LuanBit bit) throws LuanException {
 		Map<Object,Object> map = newMap();
-		for( Map.Entry<Object,Object> entry : iterable(luan) ) {
+		for( Map.Entry<Object,Object> entry : iterable(bit) ) {
 			map.put(entry.getKey(),entry.getValue());
 		}
 		return map;