diff core/src/luan/LuanTable.java @ 430:f28320fd671d

fix LuanTable.toString() to use metatables
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 02 May 2015 20:35:26 -0600
parents df95199ca4c0
children 3ffe8ba5b297
line wrap: on
line diff
--- a/core/src/luan/LuanTable.java	Sat May 02 20:13:24 2015 -0600
+++ b/core/src/luan/LuanTable.java	Sat May 02 20:35:26 2015 -0600
@@ -90,6 +90,22 @@
 		return list!=null ? list : Collections.emptyList();
 	}
 
+	public String toString(LuanState luan) 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);
+		}
+		LuanFunction fn = luan.checkFunction(h);
+		return luan.checkString( Luan.first( luan.call(fn,"__to_string",new Object[]{this}) ) );
+	}
+
+	public String rawToString() {
+		return "table: " + Integer.toHexString(hashCode());
+	}
+
 	public Object get(LuanState luan,Object key) throws LuanException {
 		Object value = rawGet(key);
 		if( value != null )
@@ -352,8 +368,4 @@
 		return map;
 	}
 
-	@Override public final String toString() {
-		return "table: " + Integer.toHexString(hashCode());
-	}
-
 }