From b9a4024ae36ccf4fd7eafb9c3a0222d5d215f8ea Mon Sep 17 00:00:00 2001 From: CallumDev Date: Sun, 24 Sep 2023 19:07:17 +0930 Subject: [PATCH] DynValue: Strongly typed Equals(), remove boxing from == --- .../DataTypes/DynValue.cs | 51 +++++++++++-------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/src/WattleScript.Interpreter/DataTypes/DynValue.cs b/src/WattleScript.Interpreter/DataTypes/DynValue.cs index e95e7799..4c7dbac9 100644 --- a/src/WattleScript.Interpreter/DataTypes/DynValue.cs +++ b/src/WattleScript.Interpreter/DataTypes/DynValue.cs @@ -587,18 +587,16 @@ public override int GetHashCode() } /// - /// Determines whether the specified , is equal to this instance. + /// Determines whether the specified , is equal to this instance. /// - /// The to compare with this instance. + /// The to compare with this instance. /// - /// true if the specified is equal to this instance; otherwise, false. + /// true if the specified is equal to this instance; otherwise, false. /// - 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; @@ -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; } } + /// + /// Determines whether the specified , is equal to this instance. + /// + /// The to compare with this instance. + /// + /// true if the specified is equal to this instance; otherwise, false. + /// + public override bool Equals(object obj) + { + if (!(obj is DynValue other)) return false; + return Equals(other); + } + /// /// Casts this DynValue to string, using coercion if the type is number.