comparison 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
comparison
equal deleted inserted replaced
34:0cdc1da466ee 35:e51906de0f11
1 package luan.interp; 1 package luan.interp;
2 2
3 import luan.Lua; 3 import luan.Lua;
4 import luan.LuaNumber; 4 import luan.LuaFunction;
5 import luan.LuaException; 5 import luan.LuaException;
6 6
7 7
8 final class ConcatExpr extends BinaryOpExpr { 8 final class ConcatExpr extends BinaryOpExpr {
9 9
10 ConcatExpr(Expr op1,Expr op2) { 10 ConcatExpr(Expr op1,Expr op2) {
11 super(op1,op2); 11 super(op1,op2);
12 } 12 }
13 13
14 @Override public Object eval(LuaStateImpl lua) throws LuaException { 14 @Override public Object eval(LuaStateImpl lua) throws LuaException {
15 return toString(op1.eval(lua)) + toString(op2.eval(lua)); 15 Object o1 = op1.eval(lua);
16 } 16 Object o2 = op2.eval(lua);
17 17 String s1 = Lua.asString(o1);
18 private static String toString(Object v) throws LuaException { 18 String s2 = Lua.asString(o2);
19 String s = Lua.asString(v); 19 if( s1 != null && s2 != null )
20 if( s==null ) 20 return s1 + s2;
21 throw new LuaException( "attempt to concatenate a " + Lua.type(v) + " value" ); 21 LuaFunction fn = getBinHandler("__concat",o1,o2);
22 return s; 22 if( fn != null )
23 return Utils.first(fn.call(lua,o1,o2));
24 String type = s1==null ? Lua.type(o1) : Lua.type(o2);
25 throw new LuaException( "attempt to concatenate a " + type + " value" );
23 } 26 }
24 } 27 }