Mercurial Hosting > luan
comparison src/luan/interp/EqExpr.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.LuaNumber; |
5 import luan.LuaFunction; | |
6 import luan.LuaTable; | |
5 import luan.LuaException; | 7 import luan.LuaException; |
6 | 8 |
7 | 9 |
8 final class EqExpr extends BinaryOpExpr { | 10 final class EqExpr extends BinaryOpExpr { |
9 | 11 |
10 EqExpr(Expr op1,Expr op2) { | 12 EqExpr(Expr op1,Expr op2) { |
11 super(op1,op2); | 13 super(op1,op2); |
12 } | 14 } |
13 | 15 |
14 @Override public Object eval(LuaStateImpl lua) throws LuaException { | 16 @Override public Object eval(LuaStateImpl lua) throws LuaException { |
15 Object v1 = op1.eval(lua); | 17 Object o1 = op1.eval(lua); |
16 Object v2 = op2.eval(lua); | 18 Object o2 = op2.eval(lua); |
17 return v1 == v2 || v1 != null && v1.equals(v2); | 19 if( o1 == o2 || o1 != null && o1.equals(o2) ) |
20 return true; | |
21 if( !o1.getClass().equals(o2.getClass()) ) | |
22 return false; | |
23 LuaTable mt1 = Lua.getMetatable(o1); | |
24 LuaTable mt2 = Lua.getMetatable(o2); | |
25 if( mt1==null || mt2==null ) | |
26 return false; | |
27 Object f = mt1.get("__eq"); | |
28 if( f == null || !f.equals(mt2.get("__eq")) ) | |
29 return null; | |
30 LuaFunction fn = Lua.checkFunction(f); | |
31 return Lua.toBoolean( Utils.first(fn.call(lua,o1,o2)) ); | |
18 } | 32 } |
19 } | 33 } |