Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve infinity detection when parsing numbers on full framework #2003

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}