DLR and Javascript interpretation in c# 4?

2019-05-05 19:11发布

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...)

2条回答
beautiful°
2楼-- · 2019-05-05 19:29

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-05-05 19:36

If you're looking for a 1:1 match between how your javascript is executed in .NET and how it would be executed in a windows program such as Internet Explorer, there is a way to reuse the Windows script engines (not only Jscript but also VBScript or any other ActiveX Scripting language) that's described here on SO: parse and execute JS by C#

This is how your code could be implemented using this technique:

        var script = @"
                           function show( )
                           {
                                  return  parseInt('123asd'); //in js it's 123
                           }"; // Note I have removed the return as it's not needed here

        using (ScriptEngine engine = new ScriptEngine("jscript"))
        {
            ParsedScript parsed = engine.Parse(script);
            Console.WriteLine(parsed.CallMethod("show"));
        }
    }

This will output 123 as expected. Note I think the original code could be improved with the new dynamic C# keyword, so we could probably write Console.WriteLine(parsed.show()) directly.

查看更多
登录 后发表回答