Skip to content

Commit

Permalink
Improve infinity detection when parsing numbers on full framework (#2003
Browse files Browse the repository at this point in the history
)
  • Loading branch information
lahma authored Nov 14, 2024
1 parent 408b636 commit a506929
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
26 changes: 24 additions & 2 deletions Jint.Tests/Runtime/NumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,33 @@ public void ToLocaleString(string parseNumber, string culture, string result)
}

[Theory]
// Does not add extra zeros of there is no cuture argument.
// Does not add extra zeros of there is no culture argument.
[InlineData("123456")]
public void ToLocaleStringNoArg(string parseNumber)
{
var value = _engine.Evaluate($"({parseNumber}).toLocaleString()").AsString();
Assert.DoesNotContain(".0", value);
}
}

[Fact]
public void CoercingOverflowFromString()
{
var engine = new Engine();

Assert.Equal(double.PositiveInfinity, engine.Evaluate("Number(1e1000)").ToObject());
Assert.Equal(double.PositiveInfinity, engine.Evaluate("+1e1000").ToObject());
Assert.Equal("Infinity", engine.Evaluate("(+1e1000).toString()").ToObject());

Assert.Equal(double.PositiveInfinity, engine.Evaluate("Number('1e1000')").ToObject());
Assert.Equal(double.PositiveInfinity, engine.Evaluate("+'1e1000'").ToObject());
Assert.Equal("Infinity", engine.Evaluate("(+'1e1000').toString()").ToObject());

Assert.Equal(double.NegativeInfinity, engine.Evaluate("Number(-1e1000)").ToObject());
Assert.Equal(double.NegativeInfinity, engine.Evaluate("-1e1000").ToObject());
Assert.Equal("-Infinity", engine.Evaluate("(-1e1000).toString()").ToObject());

Assert.Equal(double.NegativeInfinity, engine.Evaluate("Number('-1e1000')").ToObject());
Assert.Equal(double.NegativeInfinity, engine.Evaluate("-'1e1000'").ToObject());
Assert.Equal("-Infinity", engine.Evaluate("(-'1e1000').toString()").ToObject());
}
}
27 changes: 26 additions & 1 deletion Jint/Runtime/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,37 @@ private static double ToNumber(string input)
}
}

#if NETFRAMEWORK
// if we are on full framework, one extra check for whether it was actually over the bounds of double
// in modern NET parsing was fixed to be IEEE 754 compliant, full framework is not and cannot detect positive infinity
try
{
var targetString = firstChar == '-' ? input.Substring(1) : input;
var n = double.Parse(targetString, NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, CultureInfo.InvariantCulture);

if (n == 0 && firstChar == '-')
{
return -0.0;
}

return firstChar == '-' ? - 1 * n : n;
}
catch (Exception e) when (e is OverflowException)
{
return firstChar == '-' ? double.NegativeInfinity : double.PositiveInfinity;
}
catch
{
return double.NaN;
}
#else
if (double.TryParse(input, NumberStyles, CultureInfo.InvariantCulture, out var n))
{
return n == 0 && firstChar == '-' ? -0.0 : n;
}

return double.NaN;
#endif
}

/// <summary>
Expand Down Expand Up @@ -1014,4 +1039,4 @@ public static void CheckObjectCoercible(Engine engine, JsValue o)
ExceptionHelper.ThrowTypeError(engine.Realm, "Cannot call method on " + o);
}
}
}
}

0 comments on commit a506929

Please sign in to comment.