11

Microsoft may (actually I think it will) in the future release the IE10 Chakra (JScript engine) as a stand alone module, like google V8 JavaScript Engine.

  • The question is: will the engine accessible from C# like IronPython is?
2
  • 1
    may as in not yet confirmed? If so, then your question is unanswerable. Commented May 9, 2011 at 15:27
  • Agreed. This is way too subjective and theoretical. Vote to close.
    – Mike Hofer
    Commented May 9, 2011 at 15:33

5 Answers 5

23

The Chakra engine for Javascript is available to C# programs, through the IActiveScript interface. This is not the same as the IronPython model - JS invoked this way through Chakra is not compiled to MSIL, is not .NET logic. It does not run on the CLR/DLR. It runs in its own engine.

// Initialize Chakra (requires IE9 to be installed)
var guid = new System.Guid("{16d51579-a30b-4c8b-a276-0ff4dc41e755}");
Type t = Type.GetTypeFromCLSID(guid, true);
// you must have a p/invoke defn for IActiveScript
var engine = Activator.CreateInstance(t) as IActiveScript;

var site = new ScriptSite(); // this is a custom class
engine.SetScriptSite(site);

var parse32 = engine as IActiveScriptParse32;
parse32.InitNew();

// parse a script
engine.SetScriptState(ScriptState.Connected);
parse32.ParseScriptText(scriptText, null, null, null, IntPtr.Zero, 0, flags, out result, out exceptionInfo);

IntPtr comObject;
engine.GetScriptDispatch(null, out comObject);

// iDispatch is a COM IDispatch  that you can use to invoke script functions. 
var iDispatch = Marshal.GetObjectForIUnknown(comObject);

iDispatch.GetType().InvokeMember(methodName, BindingFlags.InvokeMethod, null, iDispatch, arguments);

Here's a winforms test app written in C# that runs Chakra through this interface:

enter image description here

You can download it from here. (look for the ScriptHost.zip file)

more information:
What is the ProgId or CLSID for IE9's Javascript engine (code-named "Chakra")

6
  • 2
    Hi, how would you go about providing native objects to the javascript engine? I.E. if I write function print() { alert("Hello World"); } it says object expected, presumably because alert is not available. So how would you implement such things? Commented Oct 22, 2012 at 8:31
  • @series0ne: That sounds like a great new question to ask on the site! Commented Dec 26, 2012 at 22:34
  • @hippietrail, thanks! I've actually worked out a way to do it, Both with Chakra and V8 (JavaScript.NET). For alert, you just reference a MessageBox instance in JavaScript from .NET, and write the alert function to call the MessageBox in .NET. I have come across some issues with it though. The MessageBox instance in JavaScript needs some really obscure name (or path) so as to not be overwritten by the user, although, this can be done within a browser also, so I dont think that it is something that can be prevented. Commented Dec 27, 2012 at 8:17
  • @Cheeso, what's the license on that code? I presume it's yours? (nice work, by the way) Commented Jan 25, 2013 at 9:29
  • As I look at it now, I see that I've attached no license to it. I will update the zip to include a license; but in the meantime you can use it under the simplified ("2 clause") BSD license.
    – Cheeso
    Commented Jan 28, 2013 at 17:49
2

C# and IronPython are both .NET languages. They share the same run-time, so they can interact easily. There's nothing to suggest that Chakra is built on .NET; rather, given that it compiles the JavaScript to machine code for the sake of performance, I'd say that it won't integrate in the same way.

They might provide a .NET API that would make it possible to pass JS from a .NET language to the JavaScript engine, but that's just conjecture on my part.

3
  • You already can pass JS to the JavaScript engine and get data back through manipulating an WebBrowser control.
    – mystery
    Commented May 9, 2011 at 16:02
  • 6
    No - the WebBrowser control is not suitable for executing Javascript. If you want to display a webpage, then use WebBrowser. If you want to run Script from C#, then you should use IActiveScript. msdn.microsoft.com/en-us/library/ky29ffxd(v=vs.94).aspx That is the MS-defined way to load and run script from .NET.
    – Cheeso
    Commented Sep 9, 2011 at 21:47
  • And this leads to be the BIG QUESTION that no one is asking: If .NET/CLR is the basket that Microsoft is putting all their eggs in as I remember them saying at one point in time, then why is MS pushing out a Javascript that is NOT RUNNING ON TOP OF THE CLR? Are they now hedging their bets because Javascript is so popular that it might become its own platform that might give CLR tough competition? Will .NET programmers one day be abandoned, just like Silverlight, etc... ?
    – Andz
    Commented Jun 21, 2015 at 10:17
2

The Chakra runtime is now available to call from C# through pinvoke directly. You don't need to go through active script. The api is quite nice and very fast. Here is an example of how to do it on MSDN:

http://code.msdn.microsoft.com/windowsdesktop/JavaScript-Runtime-Hosting-d3a13880

0

JavaScript (as JScript) is one of the original languages supported by .Net. Support was created before dynamics was available, so in that sence it is different from IronPython... If you need a JavaScript engine in .Net it may be enough for your needs - http://msdn.microsoft.com/en-us/library/72bd815a(v=VS.100).aspx.

0

Check out what Microsoft just released regarding this topic:

http://blogs.windows.com/msedgedev/2015/05/18/using-chakra-for-scripting-applications-across-windows-10/#comment-841

Inside the blog answers you can find the latest comment from Microsoft. The answer is: "Re:Windows scripting – Windows Scripting host supports JavaScript but is not based on Chakra engine. For now, this project does not intend to change the same."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.