Skip to content

Commit 2d65613

Browse files
authored
Object.is fix when providing BigInt values (#2183)
1 parent fb64c38 commit 2d65613

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Jint.Tests/Runtime/BigIntTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Numerics;
12
using Jint.Native;
23

34
namespace Jint.Tests.Runtime;
@@ -28,4 +29,42 @@ public void BasicOperations(string statement, string expected)
2829
Assert.True(outputValues[0].IsBigInt(), "The type of the value is expected to stay BigInt");
2930
Assert.Equal(expected, outputValues[0].ToString());
3031
}
32+
33+
[Theory]
34+
[InlineData("let right = 123n;")]
35+
[InlineData("let right = BigInt(123);")]
36+
[InlineData("let right = BigInt('123');")]
37+
public void ObjectIsReturnsTrueForSameBigInts(string statement)
38+
{
39+
// Arrange
40+
var engine = new Engine();
41+
engine.SetValue("left", new JsBigInt(new BigInteger(123)));
42+
engine.Evaluate(statement);
43+
44+
// Act
45+
var result = engine.Evaluate("Object.is(left, right);");
46+
47+
// Assert
48+
Assert.True(result.AsBoolean());
49+
}
50+
51+
[Theory]
52+
[InlineData("let right = false;")]
53+
[InlineData("let right = 'test';")]
54+
[InlineData("let right = 123;")]
55+
[InlineData("let right = 321n;")]
56+
[InlineData("let right = BigInt('321');")]
57+
public void ObjectIsReturnsFalseDifferentComparisonsWithBigInt(string statement)
58+
{
59+
// Arrange
60+
var engine = new Engine();
61+
engine.SetValue("left", new JsBigInt(new BigInteger(123)));
62+
engine.Evaluate(statement);
63+
64+
// Act
65+
var result = engine.Evaluate("Object.is(left, right);");
66+
67+
// Assert
68+
Assert.False(result.AsBoolean());
69+
}
3170
}

Jint/Native/JsValue.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,8 @@ internal static bool SameValue(JsValue x, JsValue y)
537537
return x == y;
538538
case Types.Object:
539539
return x is ObjectWrapper xo && y is ObjectWrapper yo && ReferenceEquals(xo.Target, yo.Target);
540+
case Types.BigInt:
541+
return (x is JsBigInt xBigInt && y is JsBigInt yBigInt && xBigInt.Equals(yBigInt));
540542
default:
541543
return false;
542544
}

0 commit comments

Comments
 (0)