Skip to content

Commit

Permalink
Fix BigInt arithmetic assignments (#1887)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Jun 13, 2024
1 parent c0b0369 commit 7414296
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
29 changes: 29 additions & 0 deletions Jint.Tests/Runtime/BigIntTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Jint.Native;

namespace Jint.Tests.Runtime;

public class BigIntTests
{
[Theory]
[InlineData("a = a + b;", "146")]
[InlineData("a = a - b;", "100")]
[InlineData("a = a * b;", "2829")]
[InlineData("a = a / b;", "5")]
[InlineData("a += b;", "146")]
[InlineData("a -= b;", "100")]
[InlineData("a *= b;", "2829")]
[InlineData("a /= b;", "5")]
public void BasicOperations(string statement, string expected)
{
var outputValues = new List<JsValue>();
var engine = new Engine()
.SetValue("log", outputValues.Add);
engine.Evaluate("let a = 123n; let b = 23n;");

engine.Evaluate(statement);

engine.Evaluate("log(a)");
Assert.True(outputValues[0].IsBigInt(), "The type of the value is expected to stay BigInt");
Assert.Equal(expected, outputValues[0].ToString());
}
}
6 changes: 2 additions & 4 deletions Jint/Native/JsBigInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ static JsBigInt()
{
bigIntegers[i] = new JsBigInt(i);
}

_bigIntegerToJsValue = bigIntegers;
}

Expand All @@ -40,12 +41,9 @@ internal static JsBigInt Create(BigInteger bigInt)

internal static JsBigInt Create(JsValue value)
{
return value is JsBigInt jsBigInt
? jsBigInt
: Create(TypeConverter.ToBigInt(value));
return value as JsBigInt ?? Create(TypeConverter.ToBigInt(value));
}


public override object ToObject() => _value;

internal override bool ToBoolean() => _value != 0;
Expand Down
15 changes: 9 additions & 6 deletions Jint/Runtime/Interpreter/Expressions/JintAssignmentExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ protected override object EvaluateInternal(EvaluationContext context)

newLeftValue = jsString.Append(rprim);
}
else if (!AreIntegerOperands(originalLeftValue, rval))
else if (JintBinaryExpression.AreNonBigIntOperands(originalLeftValue, rval))
{
newLeftValue = TypeConverter.ToNumber(lprim) + TypeConverter.ToNumber(rprim);
}
else
{
newLeftValue = TypeConverter.ToBigInt(lprim) + TypeConverter.ToBigInt(rprim);
JintBinaryExpression.AssertValidBigIntArithmeticOperands(lprim, rprim);
newLeftValue = JsBigInt.Create(TypeConverter.ToBigInt(lprim) + TypeConverter.ToBigInt(rprim));
}
}

Expand All @@ -120,13 +121,14 @@ protected override object EvaluateInternal(EvaluationContext context)
{
newLeftValue = JsNumber.Create(originalLeftValue.AsInteger() - rval.AsInteger());
}
else if (!AreIntegerOperands(originalLeftValue, rval))
else if (JintBinaryExpression.AreNonBigIntOperands(originalLeftValue, rval))
{
newLeftValue = JsNumber.Create(TypeConverter.ToNumber(originalLeftValue) - TypeConverter.ToNumber(rval));
}
else
{
newLeftValue = JsNumber.Create(TypeConverter.ToBigInt(originalLeftValue) - TypeConverter.ToBigInt(rval));
JintBinaryExpression.AssertValidBigIntArithmeticOperands(originalLeftValue, rval);
newLeftValue = JsBigInt.Create(TypeConverter.ToBigInt(originalLeftValue) - TypeConverter.ToBigInt(rval));
}

break;
Expand All @@ -143,13 +145,14 @@ protected override object EvaluateInternal(EvaluationContext context)
{
newLeftValue = JsValue.Undefined;
}
else if (!AreIntegerOperands(originalLeftValue, rval))
else if (JintBinaryExpression.AreNonBigIntOperands(originalLeftValue, rval))
{
newLeftValue = TypeConverter.ToNumber(originalLeftValue) * TypeConverter.ToNumber(rval);
}
else
{
newLeftValue = TypeConverter.ToBigInt(originalLeftValue) * TypeConverter.ToBigInt(rval);
JintBinaryExpression.AssertValidBigIntArithmeticOperands(originalLeftValue, rval);
newLeftValue = JsBigInt.Create(TypeConverter.ToBigInt(originalLeftValue) * TypeConverter.ToBigInt(rval));
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ protected override object EvaluateInternal(EvaluationContext context)
}
else
{
JintBinaryExpression.AssertValidBigIntArithmeticOperands(left, right);
number = JsBigInt.Create(TypeConverter.ToBigInt(left) - TypeConverter.ToBigInt(right));
}

Expand Down

0 comments on commit 7414296

Please sign in to comment.