diff src/luan/interp/ConcatExpr.java @ 35:e51906de0f11

implement metatables git-svn-id: https://luan-java.googlecode.com/svn/trunk@36 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 18 Dec 2012 07:05:58 +0000
parents 5cf15507d77e
children 8a57ebfdfd78
line wrap: on
line diff
--- a/src/luan/interp/ConcatExpr.java	Sun Dec 16 09:23:56 2012 +0000
+++ b/src/luan/interp/ConcatExpr.java	Tue Dec 18 07:05:58 2012 +0000
@@ -1,7 +1,7 @@
 package luan.interp;
 
 import luan.Lua;
-import luan.LuaNumber;
+import luan.LuaFunction;
 import luan.LuaException;
 
 
@@ -12,13 +12,16 @@
 	}
 
 	@Override public Object eval(LuaStateImpl lua) throws LuaException {
-		return toString(op1.eval(lua)) + toString(op2.eval(lua));
-	}
-
-	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;
+		Object o1 = op1.eval(lua);
+		Object o2 = op2.eval(lua);
+		String s1 = Lua.asString(o1);
+		String s2 = Lua.asString(o2);
+		if( s1 != null && s2 != null )
+			return s1 + s2;
+		LuaFunction fn = getBinHandler("__concat",o1,o2);
+		if( fn != null )
+			return Utils.first(fn.call(lua,o1,o2));
+		String type = s1==null ? Lua.type(o1) : Lua.type(o2);
+		throw new LuaException( "attempt to concatenate a " + type + " value" );
 	}
 }