Search code examples
c#.netdynamic.net-4.0dynamic-language-runtime

DLR and Javascript interpretation in c# 4?


I want to execute a javascript code from c# using DLR.

So I wrote a simple code using c# and Jint :

  var script = @"
                               function show( )
                               {
                                      return  parseInt('123asd'); //in js it's 123
                               };
                               return show();";

                var result = new JintEngine().Run(script);
                Console.WriteLine(result);

parseInt('123asd') in javascript is : 123

But the result I get is :

enter image description here

  • Maybe I don't see the whole picture, but if a programmer on the other side of the world sends me his script file, I (and him) expect the result to be consistent!

  • If I was mistaken, in what scenario would I use running other code on .Net? (I will have to be very very suspicious for every line of code...)


Solution

  • This seems to be current behavior of Jint's partseInt function.

    Related code:

    // most common case
    double result;
    if(double.TryParse(number,NumberStyles.Any, CultureInfo.InvariantCulture, out result)) {
        // parseInt(12.42) == 42
        return NumberClass.New(sign * Math.Floor(result));
    }
    else {
        return this["NaN"];
    }
    

    .NET's double.TryParse won't parse '123asd' as 123, but returns that it cannot parse the whole string.

    You should fill in bug report in Jint.

    E: This is problem with "standards". You can never get standards 100% compatible across all implementations. There will always be differences. Just look at IE vs Chrome vs Firefox vs Opera. All of those use different javascript engines, so you can expect differences. If you really want to be cross-compatible, you should implement according to standard and treat all non-standard behavior as bug in implementation. It is up to you if you try to work around this bug or report it and expect it to be fixed.