瀏覽代碼

Remove floating point precision loss checks under interop (#1491)

Marko Lahma 2 年之前
父節點
當前提交
c116ab6f3a
共有 2 個文件被更改,包括 7 次插入17 次删除
  1. 7 4
      Jint.Tests/Runtime/InteropTests.cs
  2. 0 13
      Jint/Runtime/Interop/DefaultTypeConverter.cs

+ 7 - 4
Jint.Tests/Runtime/InteropTests.cs

@@ -2508,11 +2508,14 @@ namespace Jint.Tests.Runtime
             Assert.Equal("No public methods with the specified arguments were found.", ex.Message);
 
             Assert.Equal(123, engine.Evaluate("new IntValueInput().testFunc(123);").AsNumber());
+        }
 
-            ex = Assert.Throws<JavaScriptException>(() => engine.Evaluate("new IntValueInput().testFunc(12.3);").AsNumber());
-            Assert.Equal("No public methods with the specified arguments were found.", ex.Message);
-
-            Assert.Equal(123, engine.Evaluate("new IntValueInput().testFunc(123);").AsNumber());
+        [Fact]
+        public void CanConvertFloatingPointToIntegerWithoutError()
+        {
+            var engine = new Engine(options => options.AllowClr());
+            engine.SetValue("IntValueInput", TypeReference.CreateTypeReference(engine, typeof(IntValueInput)));
+            Assert.Equal(12, engine.Evaluate("new IntValueInput().testFunc(12.3);").AsNumber());
         }
 
         public class IntValueInput

+ 0 - 13
Jint/Runtime/Interop/DefaultTypeConverter.cs

@@ -102,19 +102,6 @@ namespace Jint.Runtime.Interop
 
             var valueType = value.GetType();
 
-            if (valueType == typeof(double) || valueType == typeof(float) || valueType == typeof(decimal))
-            {
-                // conversion can be dangerous
-                var doubleValue = (double) value;
-                if (!TypeConverter.IsIntegralNumber(doubleValue)
-                    && (type == typeof(long) || type == typeof(int) || type == typeof(short) || type == typeof(byte) || type == typeof(ulong) || type == typeof(uint) || type == typeof(ushort) || type == typeof(sbyte)))
-                {
-                    // this is not safe
-                    problemMessage = $"Cannot convert floating point number {doubleValue} with decimals to integral type {type}";
-                    return false;
-                }
-            }
-
             // is the javascript value an ICallable instance ?
             if (valueType == iCallableType)
             {