Passing values into user defined methods: "-1" is NaN ?

Nov 28, 2013 at 6:08 AM

The following code creates a result, that is unexpected for me. Passing the value "-1" as a string into the script via the method get1(), will cause that the script engine recognize this value as NaN. The result that I expected was 0.

What is the reason and the fix for this behaviour ?
namespace JintTest
    using System;
    using Jint;

    class Program
        static void Main(string[] args)
            var x = new JintEngine();

            x.SetFunction("get1", new Func<object>(() => "-1"));
            x.SetFunction("get2", new Func<object>(() => 1));
            x.SetFunction("set", new Action<object>(Console.WriteLine));

                        var a = get1();
                        var b = get2();

                        set(a + b);                   

Dec 31, 2013 at 8:09 PM
Edited Dec 31, 2013 at 8:14 PM
First the expected value should be:
var s = "-1" plus 1  // -11

But I tracked it down in the source code

in the file D:\DVT.NET\Jint.0.9.2\Jint - Sources\Jint\Native\JsString.cs,
method public static double StringToNumber(string value) ;

there is a bug. for example this line of code will throw an exception.
var r = double.Parse("-1", NumberStyles.AllowDecimalPoint );
I think the right way to fix it would be
var r = double.Parse("-1", NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign);
Basically Jint cannot convert the string '-1' into a number, this is not related to the call to the .NET method.

I do not think I can find a work around. You must fix the source code.