Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

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

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?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...