JavaScript Engine for Windows Phone

Edit: This is now available on GitHub: https://github.com/joelmartinez/Jint.Phone

Did a fun bit of hacking, ported the Jint JavaScript interpereter to Windows Phone 7.5 … the result is seen below:

var engine = new JintEngine();
engine.SetFunction("alert", new Action<string>(t => MessageBox.Show(t)));

engine.Run("alert('Hello World, from dynamically interpereted JavaScript on WP7!')");
view raw gistfile1.cs This Gist brought to you by GitHub.

And when we run that code in a silverlight application:

mapped alert method

This would also work in XNA games for wp7, so one can imagine scripting scenarios if that’s your cup of tea. Though it would take more work for this to be supported on the xbox since DynamicMethod is not supported there.

I was really happy to see that Windows Phone 7.1 (Mango) brings in support for the DynamicMethod and some of the other Reflection.Emit features. One of my previous projects (New LateBinder) showed significant perf benefits to using this over regular old reflection.

I had to overcome some quirks due to the slight differences in the compact framework, but after some time I got it all working … so far all my tests have been successful (though it’s not to say it’s all fully working). Many props to the team of developers that did this over at codeplex.

6 Comments »

  1. krishnakant Said,

    November 9, 2011 @ 1:31 am

    i am also trying for the same…
    can you please share how did you manage to do this…

  2. Saar Said,

    November 14, 2011 @ 5:47 pm

    Sounds very interesting do you mind sharing what you got so far on GitHub?

    Thank You,
    -saar

  3. Joel Martinez Said,

    November 15, 2011 @ 6:49 pm

    yes, I’m absolutely going to share this. Just need to find the time as its actually two things, antlr and jint that I had to port.

  4. Domenic Datti Said,

    November 22, 2011 @ 9:35 am

    Holy crap this has huge potential…

  5. Joel Martinez Said,

    February 2, 2012 @ 11:11 pm

    This is now available on github: https://github.com/joelmartinez/Jint.Phone

  6. Janak Said,

    March 14, 2012 @ 7:26 am

    That’s really great. It is working fine in most of the cases.

    But when I try to execute following script it is giving InvalidProgramException. Please do needful to me:

    string scriptData = @”function ShowMessage(){ var a = new SayHelloClass();
    a.SayHello(); // This is giving invalidprogramexception.
    }

    C# code is:

    public class SayHelloClass()
    {
    public SayHelloClass()
    {
    }
    public SayHello()
    {
    MessageBox.Show(“Hello!”);
    }
    }

    I’ve registered global settings as follow:
    JintEngine engine = new JintEngine();
    JsConstructor ctor = engine.Global.Marshaller.MarshalType(typeof(HelperClass));
    ((JsObject)engine.Global)["HelperClass"] = ctor;

    and calling the ShowMessage API as follow:
    engine.Run(“ShowMessage()”);

    I’ve tried to debug and it is giving exception from NativeMethod.cs class:
    visitor.Return( m_impl(visitor.Global, that, parameters) );

    What Am I missing here? Does it due to reflection limitations? How can I overcome to this problem?

RSS feed for comments on this post

Leave a Comment