Given the rise of Javascript in Windows 8, does Windows 8 / .Net 4.5 / VS 2012 provide a mechanism to embed the Chakra javascript engine in application to enable scripting? If so, is there documentation for this somewhere?
Asked
Active
Viewed 1,317 times
4
-
Depending on your needs, you might be able to use another JavaScript implementation like Jint or IronJS. (Jurassic probably wouldn't work -- it P/Invokes to V8 and I don't think P/Invoke is allowed in Metro apps.) – Joe White Oct 17 '11 at 16:27
-
See also, http://stackoverflow.com/questions/7167690/what-is-the-progid-or-clsid-for-ie9s-javascript-engine-code-named-chakra – Cheeso Jan 06 '12 at 01:41
3 Answers
1
There is no mechanism to do this that has been released or talked about. For now, it is available only in IE and to Metro style apps. There isn't even a Windows Scripting Host style exposure of it.
What is it about Chakra that you want in your scripting?

Steve Rowe
- 19,411
- 9
- 51
- 82
-
Hi Steve, in my case, I would need to invoke javascript from both a Windows Store App and from WPF. We are developing an App for Windows 8 and its little brother from pre-Windows 8 in WPF. I would need to use JavaScript as a scripting language. So I would have, on one hand, a string representing a script and an object model in .NET. I would need to pass that object model to the script in order for the script to run on it. Any way you see to that in both Win Store App & traditional .NET? It could be two different mechanism. – Vincent-Philippe Lauzon Apr 17 '13 at 21:20
0
Doesn't IE ActiveX use the same JavaScript engine as IE standalone?
You can simply embed Internet Explorer ActiveX frame and keep it hidden.

Oleg Mihailik
- 2,514
- 2
- 19
- 32
0
Yes, exists.
See: https://github.com/Microsoft/ChakraCore/wiki/Embedding-ChakraCore
using System;
using System.Runtime.InteropServices;
using ChakraHost.Hosting;
public class HelloWorld
{
static void Main() {
JavaScriptRuntime runtime;
JavaScriptContext context;
JavaScriptSourceContext currentSourceContext = JavaScriptSourceContext.FromIntPtr(IntPtr.Zero);
JavaScriptValue result;
// Your script, try replace the basic hello world with something else
string script = "(()=>{return \'Hello world!\';})()";
// Create a runtime.
Native.JsCreateRuntime(JavaScriptRuntimeAttributes.None, null, out runtime);
// Create an execution context.
Native.JsCreateContext(runtime, out context);
// Now set the execution context as being the current one on this thread.
Native.JsSetCurrentContext(context);
// Run the script.
Native.JsRunScript(script, currentSourceContext++, "", out result);
// Convert your script result to String in JavaScript; redundant if your script returns a String
JavaScriptValue resultJSString;
Native.JsConvertValueToString(result, out resultJSString);
// Project script result in JS back to C#.
IntPtr resultPtr;
UIntPtr stringLength;
Native.JsStringToPointer(resultJSString, out resultPtr, out stringLength);
string resultString = Marshal.PtrToStringUni(resultPtr);
Console.WriteLine(resultString);
Console.ReadLine();
// Dispose runtime
Native.JsSetCurrentContext(JavaScriptContext.Invalid);
Native.JsDisposeRuntime(runtime);
}
}

Alex G. Wolff
- 166
- 1
- 5
-
1Please consider adding a code example on stackoverflow as the link might break or the repo might be removed. – lloyd Aug 07 '18 at 00:30