Skip to content

ProxyObject and equality #928

@marat-gainullin

Description

@marat-gainullin

I've got a problem with the equals/hashCode contract of ProxyObjectin 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions