Skip to content

Commit

Permalink
DynValue: Strongly typed Equals(), remove boxing from ==
Browse files Browse the repository at this point in the history
  • Loading branch information
CallumDev committed Sep 24, 2023
1 parent d9b6814 commit b9a4024
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions src/WattleScript.Interpreter/DataTypes/DynValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,18 +587,16 @@ public override int GetHashCode()
}

/// <summary>
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
/// Determines whether the specified <see cref="WattleScript.Interpreter.DynValue" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <param name="obj">The <see cref="WattleScript.Interpreter.DynValue" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// <c>true</c> if the specified <see cref="WattleScript.Interpreter.DynValue" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
public bool Equals(DynValue other)
{
if (!(obj is DynValue other)) return false;

if ((other.Type == DataType.Nil && this.Type == DataType.Void)
|| (other.Type == DataType.Void && this.Type == DataType.Nil))
|| (other.Type == DataType.Void && this.Type == DataType.Nil))
return true;

if (other.Type != this.Type) return false;
Expand Down Expand Up @@ -632,29 +630,42 @@ public override bool Equals(object obj)
case DataType.Thread:
return Coroutine == other.Coroutine;
case DataType.UserData:
{
UserData ud1 = this.UserData;
UserData ud2 = other.UserData;
{
UserData ud1 = this.UserData;
UserData ud2 = other.UserData;

if (ud1 == null || ud2 == null)
return false;
if (ud1 == null || ud2 == null)
return false;

if (ud1.Descriptor != ud2.Descriptor)
return false;
if (ud1.Descriptor != ud2.Descriptor)
return false;

if (ud1.Object == null && ud2.Object == null)
return true;
if (ud1.Object == null && ud2.Object == null)
return true;

if (ud1.Object != null && ud2.Object != null)
return ud1.Object.Equals(ud2.Object);
if (ud1.Object != null && ud2.Object != null)
return ud1.Object.Equals(ud2.Object);

return false;
}
return false;
}
default:
return false;
}
}

/// <summary>
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
if (!(obj is DynValue other)) return false;
return Equals(other);
}


/// <summary>
/// Casts this DynValue to string, using coercion if the type is number.
Expand Down

0 comments on commit b9a4024

Please sign in to comment.