Skip to content

Commit

Permalink
Detect double.PositiveInfinity when parsing dates on full framework
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Nov 14, 2024
1 parent 408b636 commit 0469e0a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
18 changes: 16 additions & 2 deletions Jint.Tests/Runtime/NumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,25 @@ 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 CoercingFromString()
{
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());
}
}
20 changes: 19 additions & 1 deletion Jint/Runtime/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,30 @@ 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 n = double.Parse(input, NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, CultureInfo.InvariantCulture);
return n == 0 && firstChar == '-' ? -0.0 : n;
}
catch (Exception e) when (e is OverflowException)
{
return 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 +1032,4 @@ public static void CheckObjectCoercible(Engine engine, JsValue o)
ExceptionHelper.ThrowTypeError(engine.Realm, "Cannot call method on " + o);
}
}
}
}

0 comments on commit 0469e0a

Please sign in to comment.