Skip to content

Commit

Permalink
Remove exposed CLR type equality requirement from ObjectWrapper.Equals (
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Oct 21, 2023
1 parent 8794aa0 commit f4667ed
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
3 changes: 0 additions & 3 deletions Jint.Tests/Runtime/InteropExplicitTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ public InteropExplicitTypeTests()
public void EqualTest()
{
Assert.Equal(_engine.Evaluate("holder.I1"), _engine.Evaluate("holder.i1"));
Assert.NotEqual(_engine.Evaluate("holder.I1"), _engine.Evaluate("holder.ci1"));

Assert.Equal(_engine.Evaluate("holder.Super"), _engine.Evaluate("holder.super"));
Assert.NotEqual(_engine.Evaluate("holder.Super"), _engine.Evaluate("holder.ci1"));
}

[Fact]
Expand Down
20 changes: 20 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3263,5 +3263,25 @@ public void CanPassDateTimeMinAndMaxViaInterop()
engine.Execute("capture(maxDate);");
Assert.Equal(DateTime.MaxValue, dt);
}

private class Container
{
private readonly Child _child = new();
public Child Child => _child;
public BaseClass Get() => _child;
}

private class BaseClass { }

private class Child : BaseClass { }

[Fact]
public void AccessingBaseTypeShouldBeEqualToAccessingDerivedType()
{
var engine = new Engine().SetValue("container", new Container());
var res = engine.Evaluate("container.Child === container.Get()"); // These two should be the same object. But this PR makes `container.Get()` return a different object

Assert.True(res.AsBoolean());
}
}
}
7 changes: 1 addition & 6 deletions Jint/Native/JsValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,12 +517,7 @@ internal static bool SameValue(JsValue x, JsValue y)
case Types.Symbol:
return x == y;
case Types.Object:
if (x is ObjectWrapper xo && y is ObjectWrapper yo)
{
return ReferenceEquals(xo.Target, yo.Target)
&& xo.ClrType == yo.ClrType;
}
return false;
return x is ObjectWrapper xo && y is ObjectWrapper yo && ReferenceEquals(xo.Target, yo.Target);
default:
return false;
}
Expand Down
8 changes: 2 additions & 6 deletions Jint/Runtime/Interop/ObjectWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,7 @@ internal override ulong GetSmallestIndex(ulong length)
return Target is ICollection ? 0 : base.GetSmallestIndex(length);
}

public override bool Equals(JsValue? obj)
{
return Equals(obj as ObjectWrapper);
}
public override bool Equals(JsValue? obj) => Equals(obj as ObjectWrapper);

public bool Equals(ObjectWrapper? other)
{
Expand All @@ -318,14 +315,13 @@ public bool Equals(ObjectWrapper? other)
return true;
}

return Equals(Target, other.Target) && Equals(ClrType, other.ClrType);
return Equals(Target, other.Target);
}

public override int GetHashCode()
{
var hashCode = -1468639730;
hashCode = hashCode * -1521134295 + Target.GetHashCode();
hashCode = hashCode * -1521134295 + ClrType.GetHashCode();
return hashCode;
}

Expand Down

0 comments on commit f4667ed

Please sign in to comment.