Will the IE10 Chakra JScript engine available as stand alone accessible from C#?

[亡魂溺海] 提交于 2019-11-28 06:34:35

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.

Cheeso

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:

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

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!