Improve __eq metatag processing.
This commit is contained in:
@@ -75,7 +75,8 @@ public class LuaDouble extends LuaNumber {
|
|||||||
public boolean equals(Object o) { return o instanceof LuaDouble? ((LuaDouble)o).v == v: false; }
|
public boolean equals(Object o) { return o instanceof LuaDouble? ((LuaDouble)o).v == v: false; }
|
||||||
|
|
||||||
// arithmetic equality
|
// arithmetic equality
|
||||||
public boolean eq_b( LuaValue rhs ) { return rhs.eq_b(v); }
|
public LuaValue eq( LuaValue rhs ) { return rhs.eq_b(v)? 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( double rhs ) { return v == rhs; }
|
||||||
public boolean eq_b( int rhs ) { return v == rhs; }
|
public boolean eq_b( int rhs ) { return v == rhs; }
|
||||||
|
|
||||||
|
|||||||
@@ -105,7 +105,8 @@ public class LuaInteger extends LuaNumber {
|
|||||||
public boolean equals(Object o) { return o instanceof LuaInteger? ((LuaInteger)o).v == v: false; }
|
public boolean equals(Object o) { return o instanceof LuaInteger? ((LuaInteger)o).v == v: false; }
|
||||||
|
|
||||||
// arithmetic equality
|
// arithmetic equality
|
||||||
public boolean eq_b( LuaValue rhs ) { return rhs.eq_b(v); }
|
public LuaValue eq( LuaValue rhs ) { return rhs.eq_b(v)? 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( double rhs ) { return v == rhs; }
|
||||||
public boolean eq_b( int rhs ) { return v == rhs; }
|
public boolean eq_b( int rhs ) { return v == rhs; }
|
||||||
|
|
||||||
|
|||||||
@@ -287,6 +287,18 @@ public class LuaTable extends LuaValue {
|
|||||||
return LuaInteger.valueOf(length());
|
return LuaInteger.valueOf(length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LuaValue eq( LuaValue rhs ) {
|
||||||
|
return rhs.eq_b(this)? TRUE: FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean eq_b( LuaValue rhs ) {
|
||||||
|
return rhs.eq_b(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean eq_b( LuaTable val ) {
|
||||||
|
return this == val || val.eqmt_b(this);
|
||||||
|
}
|
||||||
|
|
||||||
public int maxn() {
|
public int maxn() {
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for ( int i=0; i<array.length; i++ )
|
for ( int i=0; i<array.length; i++ )
|
||||||
|
|||||||
@@ -96,14 +96,23 @@ public class LuaUserdata extends LuaValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean equals( Object val ) {
|
public boolean equals( Object val ) {
|
||||||
|
if ( this == val )
|
||||||
|
return true;
|
||||||
if ( ! (val instanceof LuaUserdata) )
|
if ( ! (val instanceof LuaUserdata) )
|
||||||
return false;
|
return false;
|
||||||
LuaUserdata u = (LuaUserdata) val;
|
LuaUserdata u = (LuaUserdata) val;
|
||||||
return m_instance.equals(u.m_instance);
|
return m_instance.equals(u.m_instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean eq_b( LuaValue val ) {
|
public LuaValue eq( LuaValue rhs ) {
|
||||||
return equals(val);
|
return rhs.eq_b(this)? TRUE: FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean eq_b( LuaValue rhs ) {
|
||||||
|
return rhs.eq_b(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean eq_b( LuaUserdata val ) {
|
||||||
|
return this == val || m_instance.equals(val.m_instance) || val.eqmt_b(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -262,22 +262,20 @@ public class LuaValue extends Varargs {
|
|||||||
public boolean equals(Object obj) { return this == obj; }
|
public boolean equals(Object obj) { return this == obj; }
|
||||||
|
|
||||||
// arithmetic equality
|
// arithmetic equality
|
||||||
public LuaValue eq( LuaValue val ) { return eqmt(val); }
|
public LuaValue eq( LuaValue val ) { return eq_b(val)? TRUE: FALSE; }
|
||||||
public boolean eq_b( LuaValue val ) { return this == val; }
|
public boolean eq_b( LuaValue val ) { return this == 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; }
|
public boolean eq_b( LuaString val ) { return false; }
|
||||||
public boolean eq_b( double val ) { return false; }
|
public boolean eq_b( double val ) { return false; }
|
||||||
public boolean eq_b( int val ) { return false; }
|
public boolean eq_b( int val ) { return false; }
|
||||||
public LuaValue neq( LuaValue val ) { return eq(val).not(); }
|
public LuaValue neq( LuaValue val ) { return eq_b(val)? FALSE: TRUE; }
|
||||||
public boolean neq_b( LuaValue val ) { return this != val; }
|
public boolean neq_b( LuaValue val ) { return ! eq_b(val); }
|
||||||
public boolean neq_b( double val ) { return ! eq_b(val); }
|
public boolean neq_b( double val ) { return ! eq_b(val); }
|
||||||
public boolean neq_b( int val ) { return ! eq_b(val); }
|
public boolean neq_b( int val ) { return ! eq_b(val); }
|
||||||
public LuaValue eqmt( LuaValue op2 ) {
|
protected boolean eqmt_b( LuaValue op2 ) {
|
||||||
if ( type() != op2.type() )
|
|
||||||
return FALSE;
|
|
||||||
if ( eq_b(op2) )
|
|
||||||
return TRUE;
|
|
||||||
LuaValue h = metatag(EQ);
|
LuaValue h = metatag(EQ);
|
||||||
return !h.isnil() && h == op2.metatag(EQ)? h.call(this,op2): FALSE;
|
return !h.isnil() && h==op2.metatag(EQ)? h.call(this,op2).toboolean(): false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// arithmetic operators
|
// arithmetic operators
|
||||||
|
|||||||
@@ -176,6 +176,15 @@ public class UnaryBinaryOperatorsTest extends TestCase {
|
|||||||
// check arithmetic equality among different types
|
// check arithmetic equality among different types
|
||||||
assertEquals(ia.eq(sa),LuaValue.FALSE);
|
assertEquals(ia.eq(sa),LuaValue.FALSE);
|
||||||
assertEquals(sa.eq(ia),LuaValue.FALSE);
|
assertEquals(sa.eq(ia),LuaValue.FALSE);
|
||||||
|
|
||||||
|
// equals with mismatched types
|
||||||
|
LuaValue t = new LuaTable();
|
||||||
|
assertEquals(ia.eq(t),LuaValue.FALSE);
|
||||||
|
assertEquals(t.eq(ia),LuaValue.FALSE);
|
||||||
|
assertEquals(ia.eq(LuaValue.FALSE),LuaValue.FALSE);
|
||||||
|
assertEquals(LuaValue.FALSE.eq(ia),LuaValue.FALSE);
|
||||||
|
assertEquals(ia.eq(LuaValue.NIL),LuaValue.FALSE);
|
||||||
|
assertEquals(LuaValue.NIL.eq(ia),LuaValue.FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEqDouble() {
|
public void testEqDouble() {
|
||||||
@@ -191,6 +200,15 @@ public class UnaryBinaryOperatorsTest extends TestCase {
|
|||||||
// check arithmetic equality among different types
|
// check arithmetic equality among different types
|
||||||
assertEquals(da.eq(sa),LuaValue.FALSE);
|
assertEquals(da.eq(sa),LuaValue.FALSE);
|
||||||
assertEquals(sa.eq(da),LuaValue.FALSE);
|
assertEquals(sa.eq(da),LuaValue.FALSE);
|
||||||
|
|
||||||
|
// equals with mismatched types
|
||||||
|
LuaValue t = new LuaTable();
|
||||||
|
assertEquals(da.eq(t),LuaValue.FALSE);
|
||||||
|
assertEquals(t.eq(da),LuaValue.FALSE);
|
||||||
|
assertEquals(da.eq(LuaValue.FALSE),LuaValue.FALSE);
|
||||||
|
assertEquals(LuaValue.FALSE.eq(da),LuaValue.FALSE);
|
||||||
|
assertEquals(da.eq(LuaValue.NIL),LuaValue.FALSE);
|
||||||
|
assertEquals(LuaValue.NIL.eq(da),LuaValue.FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAdd() {
|
public void testAdd() {
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ local anumber = 111
|
|||||||
local aboolean = false
|
local aboolean = false
|
||||||
local afunction = function() end
|
local afunction = function() end
|
||||||
local athread = coroutine.create( afunction )
|
local athread = coroutine.create( afunction )
|
||||||
local values = { anumber, aboolean, afunction, athread }
|
local atable = {}
|
||||||
|
local values = { anumber, aboolean, afunction, athread, atable }
|
||||||
for i=1,#values do
|
for i=1,#values do
|
||||||
print( debug.getmetatable( values[i] ) )
|
print( debug.getmetatable( values[i] ) )
|
||||||
end
|
end
|
||||||
@@ -68,7 +69,7 @@ for i=1,#values do
|
|||||||
end
|
end
|
||||||
|
|
||||||
print( '---- __add, __sub, __mul, __div, __pow, __mod' )
|
print( '---- __add, __sub, __mul, __div, __pow, __mod' )
|
||||||
local groups = { {aboolean, aboolean}, {aboolean, athread}, {aboolean, afunction}, {aboolean, "abc"} }
|
local groups = { {aboolean, aboolean}, {aboolean, athread}, {aboolean, afunction}, {aboolean, "abc"}, {aboolean, atable} }
|
||||||
for i=1,#groups do
|
for i=1,#groups do
|
||||||
local a,b = groups[i][1], groups[i][2]
|
local a,b = groups[i][1], groups[i][2]
|
||||||
print( type(a), type(b), 'before', ecall( 'attempt to perform arithmetic', function() return a+b end ) )
|
print( type(a), type(b), 'before', ecall( 'attempt to perform arithmetic', function() return a+b end ) )
|
||||||
@@ -106,7 +107,7 @@ for i=1,#values do
|
|||||||
end
|
end
|
||||||
|
|
||||||
print( '---- __neg' )
|
print( '---- __neg' )
|
||||||
values = { aboolean, afunction, athread, "abcd" }
|
values = { aboolean, afunction, athread, "abcd", atable }
|
||||||
for i=1,#values do
|
for i=1,#values do
|
||||||
print( type(values[i]), 'before', ecall( 'attempt to get length of ', function() return #values[i] end ) )
|
print( type(values[i]), 'before', ecall( 'attempt to get length of ', function() return #values[i] end ) )
|
||||||
print( debug.setmetatable( values[i], mt ) )
|
print( debug.setmetatable( values[i], mt ) )
|
||||||
@@ -118,7 +119,7 @@ print( '---- __eq, __lt, __le, same types' )
|
|||||||
local bfunction = function() end
|
local bfunction = function() end
|
||||||
local bthread = coroutine.create( bfunction )
|
local bthread = coroutine.create( bfunction )
|
||||||
local groups
|
local groups
|
||||||
groups = { {afunction, bfunction}, {true, true}, {true, false}, {afunction, bfunction}, {athread, bthread}, }
|
groups = { {afunction, bfunction}, {true, true}, {true, false}, {afunction, bfunction}, {athread, bthread}, {atable, atable}, {atable, {}} }
|
||||||
for i=1,#groups do
|
for i=1,#groups do
|
||||||
local a,b = groups[i][1], groups[i][2]
|
local a,b = groups[i][1], groups[i][2]
|
||||||
print( type(a), type(b), 'before', pcall( function() return a==b end ) )
|
print( type(a), type(b), 'before', pcall( function() return a==b end ) )
|
||||||
@@ -162,7 +163,7 @@ for i=1,#groups do
|
|||||||
end
|
end
|
||||||
|
|
||||||
print( '---- __tostring' )
|
print( '---- __tostring' )
|
||||||
values = { aboolean, afunction, athread }
|
values = { aboolean, afunction, athread, atable, "abc" }
|
||||||
for i=1,#values do
|
for i=1,#values do
|
||||||
local a = values[i]
|
local a = values[i]
|
||||||
print( debug.setmetatable( a, mt ) )
|
print( debug.setmetatable( a, mt ) )
|
||||||
@@ -171,7 +172,7 @@ for i=1,#values do
|
|||||||
end
|
end
|
||||||
|
|
||||||
print( '---- __metatable' )
|
print( '---- __metatable' )
|
||||||
values = { aboolean, afunction, athread }
|
values = { aboolean, afunction, athread, atable, "abc" }
|
||||||
for i=1,#values do
|
for i=1,#values do
|
||||||
local a = values[i]
|
local a = values[i]
|
||||||
print( type(a), 'before', pcall( function() return debug.getmetatable(a), getmetatable(a) end ) )
|
print( type(a), 'before', pcall( function() return debug.getmetatable(a), getmetatable(a) end ) )
|
||||||
|
|||||||
Reference in New Issue
Block a user