comparison core/src/luan/impl/EqExpr.java @ 171:3dcb0f9bee82

add core component git-svn-id: https://luan-java.googlecode.com/svn/trunk@172 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 05:41:22 +0000
parents src/luan/impl/EqExpr.java@4eaee12f6c65
children 2456ef7ada02
comparison
equal deleted inserted replaced
170:7c792a328a83 171:3dcb0f9bee82
1 package luan.impl;
2
3 import luan.Luan;
4 import luan.LuanFunction;
5 import luan.LuanTable;
6 import luan.LuanException;
7 import luan.LuanSource;
8 import luan.LuanBit;
9
10
11 final class EqExpr extends BinaryOpExpr {
12
13 EqExpr(LuanSource.Element se,Expr op1,Expr op2) {
14 super(se,op1,op2);
15 }
16
17 @Override public Object eval(LuanStateImpl luan) throws LuanException {
18 Object o1 = op1.eval(luan);
19 Object o2 = op2.eval(luan);
20 if( o1 == o2 || o1 != null && o1.equals(o2) )
21 return true;
22 if( o1 instanceof Number && o2 instanceof Number ) {
23 Number n1 = (Number)o1;
24 Number n2 = (Number)o2;
25 return n1.doubleValue() == n2.doubleValue();
26 }
27 if( o1==null || o2==null || !o1.getClass().equals(o2.getClass()) )
28 return false;
29 LuanTable mt1 = luan.getMetatable(o1);
30 LuanTable mt2 = luan.getMetatable(o2);
31 if( mt1==null || mt2==null )
32 return false;
33 Object f = mt1.get("__eq");
34 if( f == null || !f.equals(mt2.get("__eq")) )
35 return null;
36 LuanBit bit = luan.bit(se);
37 LuanFunction fn = bit.checkFunction(f);
38 return Luan.toBoolean( Luan.first(bit.call(fn,"__eq",new Object[]{o1,o2})) );
39 }
40 }