diff core/src/luan/LuanBit.java @ 419:8fbb961aabd5

improve repr() to check metamethod recursively
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 30 Apr 2015 23:15:40 -0600
parents 91af5337b9ae
children b31d614343e8
line wrap: on
line diff
--- a/core/src/luan/LuanBit.java	Thu Apr 30 21:52:20 2015 -0600
+++ b/core/src/luan/LuanBit.java	Thu Apr 30 23:15:40 2015 -0600
@@ -90,7 +90,7 @@
 	public String toString(Object obj) throws LuanException {
 		if( obj instanceof LuanTable ) {
 			LuanTable tbl = (LuanTable)obj;
-			Object h = luan.getHandler("__tostring",tbl);
+			Object h = tbl.getHandler("__tostring");
 			if( h instanceof LuanMeta ) {
 				LuanMeta meta = (LuanMeta)h;
 				return meta.__tostring(luan,tbl);
@@ -103,19 +103,21 @@
 	}
 
 	public String repr(Object obj) throws LuanException {
-		if( obj instanceof LuanTable ) {
-			LuanFunction fn = getHandlerFunction("__repr",(LuanTable)obj);
-			if( fn != null )
-				return checkString( Luan.first( call(fn,"__repr",new Object[]{obj}) ) );
-		}
-		String repr = Luan.repr(obj);
-		if( repr==null )
-			throw exception( "value '" + obj + "' doesn't support repr()" );
-		return repr;
+		if( obj == null )
+			return "nil";
+		if( obj instanceof Boolean )
+			return Luan.toString((Boolean)obj);
+		if( obj instanceof Number )
+			return Luan.toString((Number)obj);
+		if( obj instanceof String )
+			return "\"" + Luan.stringEncode((String)obj) + "\"";
+		if( obj instanceof LuanRepr )
+			return ((LuanRepr)obj).repr(luan);
+		throw exception( "value '" + obj + "' doesn't support repr()" );
 	}
 
 	public LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
-		Object f = luan.getHandler(op,t);
+		Object f = t.getHandler(op);
 		if( f == null )
 			return null;
 		return checkFunction(f);