view src/luan/interp/EqExpr.java @ 86:6db8f286fa6c

_ENV is per module, not global git-svn-id: https://luan-java.googlecode.com/svn/trunk@87 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 27 Feb 2013 08:03:51 +0000
parents 9381b23ea9e1
children 6ca02b188dba
line wrap: on
line source

package luan.interp;

import luan.Luan;
import luan.LuanFunction;
import luan.LuanTable;
import luan.LuanException;
import luan.LuanSource;


final class EqExpr extends BinaryOpExpr {

	EqExpr(LuanSource.Element se,Expr op1,Expr op2) {
		super(se,op1,op2);
	}

	@Override public Object eval(LuanStateImpl luan) throws LuanException {
		Object o1 = op1.eval(luan);
		Object o2 = op2.eval(luan);
		if( o1 == o2 || o1 != null && o1.equals(o2) )
			return true;
		if( o1 instanceof Number && o2 instanceof Number ) {
			Number n1 = (Number)o1;
			Number n2 = (Number)o2;
			return n1.doubleValue() == n2.doubleValue();
		}
		if( o1==null || o2==null || !o1.getClass().equals(o2.getClass()) )
			return false;
		LuanTable mt1 = luan.getMetatable(o1);
		LuanTable mt2 = luan.getMetatable(o2);
		if( mt1==null || mt2==null )
			return false;
		Object f = mt1.get("__eq");
		if( f == null || !f.equals(mt2.get("__eq")) )
			return null;
		LuanFunction fn = luan.checkFunction(se,f);
		return Luan.toBoolean( Luan.first(luan.call(fn,se,"__eq",o1,o2)) );
	}
}