changeset 430:f28320fd671d

fix LuanTable.toString() to use metatables
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 02 May 2015 20:35:26 -0600
parents e3a6d9dbd694
children 3ffe8ba5b297
files core/src/luan/Luan.java core/src/luan/LuanBit.java core/src/luan/LuanMeta.java core/src/luan/LuanState.java core/src/luan/LuanTable.java core/src/luan/modules/PickleCon.java
diffstat 6 files changed, 34 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
diff -r e3a6d9dbd694 -r f28320fd671d core/src/luan/Luan.java
--- a/core/src/luan/Luan.java	Sat May 02 20:13:24 2015 -0600
+++ b/core/src/luan/Luan.java	Sat May 02 20:35:26 2015 -0600
@@ -108,20 +108,6 @@
 		return i==n.doubleValue() ? Integer.valueOf(i) : null;
 	}
 
-	public static String toString(Object obj) {
-		if( obj == null )
-			return "nil";
-		if( obj instanceof Number )
-			return Luan.toString((Number)obj);
-		if( obj instanceof LuanException ) {
-			LuanException le = (LuanException)obj;
-			return le.getFullMessage();
-		}
-		if( obj instanceof byte[] )
-			return "binary: " + Integer.toHexString(obj.hashCode());
-		return obj.toString();
-	}
-
 	public static String stringEncode(String s) {
 		s = s.replace("\\","\\\\");
 		s = s.replace("\u0007","\\a");
diff -r e3a6d9dbd694 -r f28320fd671d core/src/luan/LuanBit.java
--- a/core/src/luan/LuanBit.java	Sat May 02 20:13:24 2015 -0600
+++ b/core/src/luan/LuanBit.java	Sat May 02 20:35:26 2015 -0600
@@ -90,18 +90,19 @@
 	public String toString(Object obj) throws LuanException {
 		if( obj instanceof LuanTable ) {
 			LuanTable tbl = (LuanTable)obj;
-			Object h = tbl.getHandler("__to_string");
-			if( h instanceof LuanMeta ) {
-				LuanMeta meta = (LuanMeta)h;
-				return meta.__to_string(luan,tbl);
-			}
-			if( h != null ) {
-				LuanFunction fn = checkFunction(h);
-				if( fn != null )
-					return checkString( Luan.first( call(fn,"__to_string",new Object[]{obj}) ) );
-			}
+			return tbl.toString(luan);
 		}
-		return Luan.toString(obj);
+		if( obj == null )
+			return "nil";
+		if( obj instanceof Number )
+			return Luan.toString((Number)obj);
+		if( obj instanceof LuanException ) {
+			LuanException le = (LuanException)obj;
+			return le.getFullMessage();
+		}
+		if( obj instanceof byte[] )
+			return "binary: " + Integer.toHexString(obj.hashCode());
+		return obj.toString();
 	}
 
 	public LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
diff -r e3a6d9dbd694 -r f28320fd671d core/src/luan/LuanMeta.java
--- a/core/src/luan/LuanMeta.java	Sat May 02 20:13:24 2015 -0600
+++ b/core/src/luan/LuanMeta.java	Sat May 02 20:35:26 2015 -0600
@@ -48,7 +48,7 @@
 	protected abstract String type(LuanTable tbl);
 
 	public String __to_string(LuanState luan,LuanTable tbl) throws LuanException {
-		return type(tbl) + "-" + tbl;
+		return type(tbl) + "-" + tbl.rawToString();
 	}
 
 	public LuanTable newMetatable() {
diff -r e3a6d9dbd694 -r f28320fd671d core/src/luan/LuanState.java
--- a/core/src/luan/LuanState.java	Sat May 02 20:13:24 2015 -0600
+++ b/core/src/luan/LuanState.java	Sat May 02 20:35:26 2015 -0600
@@ -70,6 +70,10 @@
 		return JAVA.call(fn,fnName,args);
 	}
 
+	public String checkString(Object obj) throws LuanException {
+		return JAVA.checkString(obj);
+	}
+
 	public LuanFunction checkFunction(Object obj) throws LuanException {
 		return JAVA.checkFunction(obj);
 	}
diff -r e3a6d9dbd694 -r f28320fd671d core/src/luan/LuanTable.java
--- 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());
-	}
-
 }
diff -r e3a6d9dbd694 -r f28320fd671d core/src/luan/modules/PickleCon.java
--- a/core/src/luan/modules/PickleCon.java	Sat May 02 20:13:24 2015 -0600
+++ b/core/src/luan/modules/PickleCon.java	Sat May 02 20:35:26 2015 -0600
@@ -71,7 +71,7 @@
 		if( obj == null )
 			return "nil";
 		if( obj instanceof Boolean )
-			return Luan.toString((Boolean)obj);
+			return obj.toString();
 		if( obj instanceof Number )
 			return Luan.toString((Number)obj);
 		if( obj instanceof String )