-
Notifications
You must be signed in to change notification settings - Fork 202
Open
Description
I've got a problem with the equals/hashCode
contract of ProxyObject
in graalJS. I've implemented ProxyObject
for my Java objects like this:
public class MyProxyObject implements ProxyObject
{
private final Object receiver;
private MyProxyObject(Object aReceiver){
receiver = aReceiver;
}
@Override
public boolean equals(Object value)
{
Object unwrapped;
if (value instanceof Value v)
{
unwrapped = v.isNull() ? null : v.isProxyObject() ? v.asProxyObject() : v.isHostObject() ? v.asHostObject() : value;
}
else
{
unwrapped = value;
}
return receiver.equals(unwrapped instanceof MyProxyObject ? ((MyProxyObject) unwrapped).unwrap() : unwrapped);
}
@Override
public int hashCode()
{
return receiver.hashCode();
}
}
When I make the bindings like this:
Object sample = new Object();
var bindings = context.getBindings("js");
bindings.putMember("p1", MyProxyObject(sample));
bindings.putMember("p2", MyProxyObject(sample));
I believe that such comparision p1 == p2
in script should evaluate to true because equals
and hashCode
have been overridden. But the equals method gets never called. This leads to a strange behavior. Let's say that we have a proxy, which returns another proxy for its property. Then we can't get the same property in script multiple times and then compare them as equal. But we expect them to be equal. Can you help with this?
Metadata
Metadata
Assignees
Labels
No labels