Fix equality test for userdata. Includes improved test7.lua.

This commit is contained in:
Ian Farmer
2008-01-11 07:56:42 +00:00
parent 9f9f31b969
commit 2461b46908
5 changed files with 37 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
<project default="all">
<property name="version" value="0.15"/>
<property name="version" value="0.16"/>
<target name="clean">
<delete dir="build"/>

View File

@@ -43,7 +43,7 @@ public class LUserData extends LValue {
public int hashCode() {
return System.identityHashCode( m_instance );
}
public int luaGetType() {
return Lua.LUA_TUSERDATA;
}
@@ -51,8 +51,14 @@ public class LUserData extends LValue {
public LTable luaGetMetatable() {
return m_metatable;
}
public Object toJavaInstance() {
return m_instance;
}
public boolean luaBinCmpUnknown( int opcode, LValue lhs ) {
if ( opcode == Lua.OP_EQ )
return lhs.equals( this );
return super.luaBinCmpUnknown( opcode, lhs );
}
}

View File

@@ -23,6 +23,7 @@ package org.luaj.sample;
public class SampleClass {
public Object o;
public String s;
public String t;
@@ -41,4 +42,12 @@ public class SampleClass {
public void setS(String s) {
this.s = s;
}
public void setObj(Object o) {
this.o = o;
}
public Object getObj() {
return o;
}
}

View File

@@ -1,6 +1,7 @@
java.lang.Object@b1b4c3
SampleClass@72ffb
java.lang.Object@xxxxxx
org.luaj.sample.SampleClass@xxxxxx
Hello
Hello
true
World
Square root of 9 is 3.0
Square root of 9 is 3

View File

@@ -1,14 +1,21 @@
local function fixhash(msg)
return string.gsub(msg, "@(%x+)", function(s) return "@"..(string.rep("x", 6)) end)
end
obj = luajava.newInstance("java.lang.Object")
print( obj )
print( fixhash( tostring(obj) ) )
obj = luajava.newInstance("org.luaj.sample.SampleClass")
print( obj )
obj.s = "Hello"
print( obj.s )
print( obj:getS() )
sample = luajava.newInstance("org.luaj.sample.SampleClass")
print( fixhash( tostring(sample) ) )
sample.s = "Hello"
print( sample.s )
print( sample:getS() )
obj:setS( "World" )
print( obj.s )
sample:setObj(obj)
print( obj == sample:getObj() )
sample:setS( "World" )
print( sample.s )
math = luajava.bindClass("java.lang.Math")
print("Square root of 9 is", math:sqrt(9.0))