Add tests for metatag operations.

This commit is contained in:
James Roseborough
2010-08-25 00:43:13 +00:00
parent 7e2c2db59c
commit d5b58107c6
6 changed files with 404 additions and 30 deletions

View File

@@ -75,7 +75,7 @@ public class LuaDouble extends LuaNumber {
public boolean equals(Object o) { return o instanceof LuaDouble? ((LuaDouble)o).v == v: false; }
// arithmetic equality
public LuaValue eq( LuaValue rhs ) { return rhs.eq_b(v)? TRUE: FALSE; }
public LuaValue eq( LuaValue rhs ) { return rhs.eq_b(v) || rhs.type()==TNUMBER && eqmt_b(rhs)? TRUE: FALSE; }
public boolean eq_b( LuaValue rhs ) { return rhs.eq_b(v); }
public boolean eq_b( double rhs ) { return v == rhs; }
public boolean eq_b( int rhs ) { return v == rhs; }

View File

@@ -105,7 +105,7 @@ public class LuaInteger extends LuaNumber {
public boolean equals(Object o) { return o instanceof LuaInteger? ((LuaInteger)o).v == v: false; }
// arithmetic equality
public LuaValue eq( LuaValue rhs ) { return rhs.eq_b(v)? TRUE: FALSE; }
public LuaValue eq( LuaValue rhs ) { return rhs.eq_b(v) || rhs.type()==TNUMBER && eqmt_b(rhs)? TRUE: FALSE; }
public boolean eq_b( LuaValue rhs ) { return rhs.eq_b(v); }
public boolean eq_b( double rhs ) { return v == rhs; }
public boolean eq_b( int rhs ) { return v == rhs; }

View File

@@ -313,7 +313,7 @@ public class LuaString extends LuaValue {
}
public boolean eq_b( LuaValue val ) {
return val.eq_b( this );
return val == this || (val.type()==TSTRING && val.eq_b(this));
}
public static boolean equals( LuaString a, int i, LuaString b, int j, int n ) {

View File

@@ -179,7 +179,6 @@ public class LuaValue extends Varargs {
protected LuaValue aritherror(String fun) { throw new LuaError("attempt to perform arithmetic '"+fun+"' on "+typename()); }
protected LuaValue compareerror(String rhs) { throw new LuaError("attempt to compare "+typename()+" with "+rhs); }
protected LuaValue compareerror(LuaValue rhs) { throw new LuaError("attempt to compare "+typename()+" with "+rhs.typename()); }
protected LuaValue concaterror() { throw new LuaError("attempt to concatenate "+typename()); }
// table operations
public LuaValue get( LuaValue key ) { return gettable(this,key); }
@@ -257,8 +256,8 @@ public class LuaValue extends Varargs {
public boolean equals(Object obj) { return this == obj; }
// arithmetic equality
public LuaValue eq( LuaValue val ) { return eq_b(val)? TRUE: FALSE; }
public boolean eq_b( LuaValue val ) { return this == val; }
public LuaValue eq( LuaValue val ) { return this == val || eq_b(val)? TRUE: FALSE; }
public boolean eq_b( LuaValue val ) { return this == val || (type() == val.type() && eqmt_b(val)); }
public boolean eq_b( LuaTable val ) { return false; }
public boolean eq_b( LuaUserdata val ) { return false; }
public boolean eq_b( LuaString val ) { return false; }
@@ -300,8 +299,11 @@ public class LuaValue extends Varargs {
public LuaValue modFrom(double lhs) { return aritherror("modFrom"); }
protected LuaValue arithmt(LuaValue tag, LuaValue op2) {
LuaValue h = this.metatag(tag);
if ( h.isnil() )
h = op2.checkmetatag(tag, "attempt to perform arithmetic on ");
if ( h.isnil() ) {
h = op2.metatag(tag);
if ( h.isnil() )
error( "attempt to perform arithmetic "+tag+" on "+typename()+" and "+op2.typename() );
}
return h.call( this, op2 );
}
@@ -336,7 +338,7 @@ public class LuaValue extends Varargs {
if ( !h.isnil() && h == op1.metatag(tag) )
return h.call(this, op1);
}
return error("attempt to compare "+typename());
return error("attempt to compare "+tag+" on "+typename()+" and "+op1.typename());
}
@@ -354,7 +356,7 @@ public class LuaValue extends Varargs {
public LuaValue concatmt(LuaValue rhs) {
LuaValue h=metatag(CONCAT);
if ( h.isnil() && (h=rhs.metatag(CONCAT)).isnil())
return (isstring()? rhs: this).concaterror();
error("attempt to concatenate "+typename()+" and "+rhs.typename());
return h.call(this,rhs);
}

View File

@@ -271,8 +271,7 @@ public class WeakTable extends LuaTable {
}
public int type() {
illegal("type","weak entry");
return 0;
return TNONE;
}
public String typename() {
@@ -293,6 +292,10 @@ public class WeakTable extends LuaTable {
return weakkey.eq_b(rhs);
}
public boolean eq_b( LuaString rhs ) { return false; }
public boolean eq_b( double val ) { return false; }
public boolean eq_b( int val ) { return false; }
public boolean isweaknil() {
return weakkey.isweaknil() || weakvalue.isweaknil();
}