Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,39 @@ private void AddDefaultMathTranslations()
args => new JsIdentifierExpression("Math").Member("tanh").Invoke(args[1])));
AddMethodTranslator(() => Math.Truncate(1.0d), new GenericMethodCompiler(
args => new JsIdentifierExpression("Math").Member("trunc").Invoke(args[1])));


// double/single finite/NaN/infinity checks
var isNaN = new GenericMethodCompiler(args => new JsIdentifierExpression("Number").Member("isNaN").Invoke(args[1]));
AddMethodTranslator(() => double.IsNaN(0d), isNaN);
AddMethodTranslator(() => float.IsNaN(0f), isNaN);

#if DotNetCore
var isFinite = new GenericMethodCompiler(args => new JsIdentifierExpression("Number").Member("isFinite").Invoke(args[1]));
AddMethodTranslator(() => double.IsFinite(0d), isFinite);
AddMethodTranslator(() => float.IsFinite(0f), isFinite);
#endif

var isInfinity = new GenericMethodCompiler(args => new JsIdentifierExpression("Math").Member("abs").Invoke(args[1]).Binary(BinaryOperatorType.Equal, new JsIdentifierExpression("Infinity")));
AddMethodTranslator(() => double.IsInfinity(0d), isInfinity);
AddMethodTranslator(() => float.IsInfinity(0f), isInfinity);

var isPosInfinity = new GenericMethodCompiler(
args => args[1].Binary(BinaryOperatorType.Equal, new JsIdentifierExpression("Infinity")));
AddMethodTranslator(() => double.IsPositiveInfinity(0d), isPosInfinity);
AddMethodTranslator(() => float.IsPositiveInfinity(0f), isPosInfinity);

var isNegInfinity = new GenericMethodCompiler(
args => args[1].Binary(BinaryOperatorType.Equal, new JsIdentifierExpression("Infinity").Unary(UnaryOperatorType.Minus)));
AddMethodTranslator(() => double.IsNegativeInfinity(0d), isNegInfinity);
AddMethodTranslator(() => float.IsNegativeInfinity(0f), isNegInfinity);

#if NET8_0_OR_GREATER
var isInteger = new GenericMethodCompiler(
args => new JsIdentifierExpression("Number").Member("isInteger").Invoke(args[1]));
AddMethodTranslator(() => double.IsInteger(0d), isInteger);
AddMethodTranslator(() => float.IsInteger(0f), isInteger);
#endif
}

private bool EnsureIsComparableInJavascript(MethodInfo method, Type type)
Expand Down
1 change: 1 addition & 0 deletions src/Tests/Binding/BindingCompilationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,7 @@ public class TestViewModel
public int IntProp { get; set; }
public int? NullableIntProp { get; set; }
public double DoubleProp { get; set; }
public float FloatProp { get; set; }
public TestViewModel2 TestViewModel2 { get; set; }
public TestViewModel2 TestViewModel2B { get; set; }
public TestEnum EnumProperty { get; set; }
Expand Down
17 changes: 17 additions & 0 deletions src/Tests/Binding/JavascriptCompilationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,23 @@ public void JsTranslator_ListReverse()
[DataRow("Math.Tan(DoubleProp)", "Math.tan(DoubleProp())")]
[DataRow("Math.Tanh(DoubleProp)", "Math.tanh(DoubleProp())")]
[DataRow("Math.Truncate(DoubleProp)", "Math.trunc(DoubleProp())")]
[DataRow("double.IsNaN(DoubleProp)", "Number.isNaN(DoubleProp())")]
[DataRow("float.IsNaN(FloatProp)", "Number.isNaN(FloatProp())")]
#if DotNetCore
[DataRow("double.IsFinite(DoubleProp)", "Number.isFinite(DoubleProp())")]
[DataRow("float.IsFinite(FloatProp)", "Number.isFinite(FloatProp())")]
#endif
[DataRow("double.IsInfinity(DoubleProp)", "Math.abs(DoubleProp())==Infinity")]
[DataRow("float.IsInfinity(FloatProp)", "Math.abs(FloatProp())==Infinity")]
[DataRow("double.IsPositiveInfinity(DoubleProp)", "DoubleProp()==Infinity")]
[DataRow("float.IsPositiveInfinity(FloatProp)", "FloatProp()==Infinity")]
[DataRow("double.IsNegativeInfinity(DoubleProp)", "DoubleProp()==-Infinity")]
[DataRow("float.IsNegativeInfinity(FloatProp)", "FloatProp()==-Infinity")]
public void JsTranslator_DoubleFloat_IsChecks(string binding, string expected)
{
var result = CompileBinding(binding, new[] { typeof(TestViewModel) });
Assert.AreEqual(expected, result);
}
public void JsTranslator_MathMethods(string binding, string expected)
{
var result = CompileBinding(binding, new[] { typeof(TestViewModel) });
Expand Down