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

Royi Namir
  • 131,490
  • 121
  • 408
  • 714
  • 1
    Or maybe it's just a bug in Jint? http://jint.codeplex.com/SourceControl/changeset/view/22024d8a6e7a#Jint%2fNative%2fJsGlobal.cs According to javascript specification, it should return 123 instead of NaN http://www.ecma-international.org/ecma-262/5.1/Ecma-262.pdf – Marek Kembrowski Dec 03 '12 at 13:33
  • @MarekKembrowski I can provide a 1000 more examples which will yields inconstant results. – Royi Namir Dec 03 '12 at 14:14
  • So maybe Chakra (IE9 engine and IActiveScript) will work for you? http://stackoverflow.com/questions/5939002/will-the-ie10-chakra-jscript-engine-available-as-stand-alone-accessible-from-c – Marek Kembrowski Dec 03 '12 at 14:27
  • @MarekKembrowski thanks i'll have a look at it. – Royi Namir Dec 03 '12 at 14:36
  • No there is not a bug. I stay with my question. – Royi Namir Dec 29 '12 at 20:09
  • This will not answer your question exactly, but if you're not tied to jint, you can reuse Windows javascript engine directly as demonstrated here: http://stackoverflow.com/questions/4744105/parse-and-execute-js-by-c-sharp this will ensure you're talking to the 'real' javascript engine. – Simon Mourier Dec 30 '12 at 07:44
  • @SimonMourier so youre saying it will also yield '123' (from my question) ? p.s. When you said the word "real" I knew you know what I was talking about. I need the same peach of code to return the exact same result (when running in browser vs interpreted in C#) – Royi Namir Dec 30 '12 at 08:28
  • @SimonMourier please consider writing an answer. – Royi Namir Dec 30 '12 at 08:30
  • @RoyiNamir - I did, a bit late unfortunately. – Simon Mourier Dec 30 '12 at 10:10
  • @SimonMourier no. not late at all. – Royi Namir Dec 30 '12 at 11:20

2 Answers2

4

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.

Euphoric
  • 12,015
  • 1
  • 26
  • 41
  • I'm sure that if I used another Javascript Interpreter I would find other mismatches.I repeat my questions : _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!_ – Royi Namir Dec 30 '12 at 07:36
  • my question is more abstract then this lousy JINT. How can I trust a different code (which is supported by DLR ) behavior to be exactly as in my C#? ( for example : somone can write a complext algorithm in JS. and ( as you must agree) it will have to yield the 100% same results - as if I wrote this myself in c#). – Royi Namir Dec 30 '12 at 08:06
  • @RoyiNamir And why would you expect that? C# and JS are completely different languages with different ways to program. There is no way you can expect exact results unless you have it tested both ways. – Euphoric Dec 30 '12 at 08:14
  • (ofcourse they are different . but my POV is DLR.)So what you're saying is : although they are both build on the DLR , I cant expect the same code to yield same results. So : what benefit the "different languages build on DLR " provides ? – Royi Namir Dec 30 '12 at 08:24
  • @RoyiNamir Oh. Right. You are confused what DLR means. First. C# runs on CLR, not DLR. Second, DLR is only "convenience" feature. It has nothing to do with running actual code. It only allows simpler implementation of dynamic languages in .NET. Also, it seems Jint has nothing to do with DLR. It is simply javascript interpreter build in .NET – Euphoric Dec 30 '12 at 09:25
3

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.

Community
  • 1
  • 1
Simon Mourier
  • 117,251
  • 17
  • 221
  • 269
  • Simon , has JS changed in such way that now , via c# , I can do `parsed.show()` ? I mean , What did they do to make this call work ? they have had to change something.... – Royi Namir Dec 30 '12 at 11:17
  • yes, the `dynamic` keyword means you can call any method/property on it and it will be resolved at runtime, not at compile time (so you can basically type anything and compile). One of the reason for creating `dynamic` was specifically COM interop (which is how works my sample). – Simon Mourier Dec 30 '12 at 18:04
  • 1
    I know what dynamic means at C#. but they had to expose JS im such way that I will be able to call the func via dynamic.... – Royi Namir Dec 30 '12 at 18:10