changeset 10:8217d8485715

fix string conversion git-svn-id: https://luan-java.googlecode.com/svn/trunk@11 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 22 Nov 2012 04:37:10 +0000
parents 600676034a1a
children b7d7069fee58
files src/luan/Lua.java src/luan/interp/ConcatExpr.java src/luan/lib/BasicLib.java
diffstat 3 files changed, 17 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
diff -r 600676034a1a -r 8217d8485715 src/luan/Lua.java
--- a/src/luan/Lua.java	Wed Nov 21 09:40:33 2012 +0000
+++ b/src/luan/Lua.java	Thu Nov 22 04:37:10 2012 +0000
@@ -25,8 +25,17 @@
 		return obj.toString();
 	}
 
-	public static String checkString(Object obj) {
-		return toString(obj);
+	public static String asString(Object obj) {
+		if( obj instanceof String || obj instanceof LuaNumber )
+			return obj.toString();
+		return null;
+	}
+
+	public static String checkString(Object obj) throws LuaException {
+		String s = asString(obj);
+		if( s == null )
+			throw new LuaException( "attempt to use a " + Lua.type(obj) + " as a string" );
+		return s;
 	}
 
 	public static LuaNumber toNumber(Object obj) {
diff -r 600676034a1a -r 8217d8485715 src/luan/interp/ConcatExpr.java
--- a/src/luan/interp/ConcatExpr.java	Wed Nov 21 09:40:33 2012 +0000
+++ b/src/luan/interp/ConcatExpr.java	Thu Nov 22 04:37:10 2012 +0000
@@ -13,15 +13,13 @@
 	}
 
 	@Override Object eval(LuaState lua) throws LuaException {
-		Object v1 = op1.eval(lua);
-		Object v2 = op2.eval(lua);
-		check(v1);
-		check(v2);
-		return Lua.toString(v1) + Lua.toString(v2);
+		return toString(op1.eval(lua)) + toString(op2.eval(lua));
 	}
 
-	private static void check(Object v) throws LuaException {
-		if( !(v instanceof String || v instanceof LuaNumber) )
+	private static String toString(Object v) throws LuaException {
+		String s = Lua.asString(v);
+		if( s==null )
 			throw new LuaException( "attempt to concatenate a " + Lua.type(v) + " value" );
+		return s;
 	}
 }
diff -r 600676034a1a -r 8217d8485715 src/luan/lib/BasicLib.java
--- a/src/luan/lib/BasicLib.java	Wed Nov 21 09:40:33 2012 +0000
+++ b/src/luan/lib/BasicLib.java	Thu Nov 22 04:37:10 2012 +0000
@@ -24,7 +24,7 @@
 		for( int i=0; i<args.length; i++ ) {
 			if( i > 0 )
 				System.out.print('\t');
-			System.out.print( Lua.checkString(args[i]) );
+			System.out.print( Lua.toString(args[i]) );
 		}
 		System.out.println();
 	}