Browse Source

Object.is fix when providing BigInt values (#2183)

tomatosalat0 2 months ago
parent
commit
2d65613208
2 changed files with 41 additions and 0 deletions
  1. 39 0
      Jint.Tests/Runtime/BigIntTests.cs
  2. 2 0
      Jint/Native/JsValue.cs

+ 39 - 0
Jint.Tests/Runtime/BigIntTests.cs

@@ -1,3 +1,4 @@
+using System.Numerics;
 using Jint.Native;
 
 namespace Jint.Tests.Runtime;
@@ -28,4 +29,42 @@ public class BigIntTests
         Assert.True(outputValues[0].IsBigInt(), "The type of the value is expected to stay BigInt");
         Assert.Equal(expected, outputValues[0].ToString());
     }
+
+    [Theory]
+    [InlineData("let right = 123n;")]
+    [InlineData("let right = BigInt(123);")]
+    [InlineData("let right = BigInt('123');")]
+    public void ObjectIsReturnsTrueForSameBigInts(string statement)
+    {
+        // Arrange
+        var engine = new Engine();
+        engine.SetValue("left", new JsBigInt(new BigInteger(123)));
+        engine.Evaluate(statement);
+
+        // Act
+        var result = engine.Evaluate("Object.is(left, right);");
+
+        // Assert
+        Assert.True(result.AsBoolean());
+    }
+
+    [Theory]
+    [InlineData("let right = false;")]
+    [InlineData("let right = 'test';")]
+    [InlineData("let right = 123;")]
+    [InlineData("let right = 321n;")]
+    [InlineData("let right = BigInt('321');")]
+    public void ObjectIsReturnsFalseDifferentComparisonsWithBigInt(string statement)
+    {
+        // Arrange
+        var engine = new Engine();
+        engine.SetValue("left", new JsBigInt(new BigInteger(123)));
+        engine.Evaluate(statement);
+
+        // Act
+        var result = engine.Evaluate("Object.is(left, right);");
+
+        // Assert
+        Assert.False(result.AsBoolean());
+    }
 }

+ 2 - 0
Jint/Native/JsValue.cs

@@ -537,6 +537,8 @@ public abstract partial class JsValue : IEquatable<JsValue>
                 return x == y;
             case Types.Object:
                 return x is ObjectWrapper xo && y is ObjectWrapper yo && ReferenceEquals(xo.Target, yo.Target);
+            case Types.BigInt:
+                return (x is JsBigInt xBigInt && y is JsBigInt yBigInt && xBigInt.Equals(yBigInt));
             default:
                 return false;
         }