Well, that's a thorny issue... POJOs are wrapped into a NativeJavaObject, which from JS perspective is an object, and Rhino will (correctly) follow the JS equality semantics for objects, which considers only reference-identical objects to be equal. This ain't really Java specific, consider how even native object wrappers for JS primitives will have reference identity equality semantics:

$ java -jar js.jar
Rhino 1.7 release 3 PRERELEASE 2009 07 06
js> new String("a") == new String("a")
false
js> new Number(2) == new Number(2)
false

However consider:

js> 2 == new Number(2)
true
js> new Number(2) == 2
true
js> "a" == new String("a")
true
js> new String("a") == "a"
true

Which is due to the fact that if any of the operands is a JS primitive type (i.e. string, number) then the other will be coerced into a primitive value and then the equality among primitive values will be used instead. Yeah, it's bloody un-intuitive, but that's the way JS works. Nothing you can do about it.

Attila.

On 2009.07.07., at 21:46, Greg Brown wrote:

Hi all,

I have been working with Rhino a bit lately, and I have a couple of questions:

1) When a Java object is used in an equality expression, I expected the JavaScript == operator to invoke the object's equals() method, and === to use Java's reference equality operator (==). However, it appears that both == and === translate to reference equality; equals() is only called if I invoke it explicitly. Is this the expected behavior, or am I missing something?

2) If == does not currently delegate to equals(), should it? Taking this one step further, would it be possible to support additional operator overloads as well, a la Groovy?

http://groovy.codehaus.org/Operator+Overloading

For example, "a + b" translates to "a.plus(b)", "a - b" translates to "a.minus(b)", etc.

Operator overloading is a feature that I have missed in Java (coming from C++, way back) and many of the newer JVM scripting languages support it (Groovy, Scala, JRuby (I think), and possibly others). It would be really cool if Rhino supported it as well.

Greg
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to