4

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?

hippietrail
  • 13,703
  • 15
  • 87
  • 133
Tristan
  • 6,376
  • 4
  • 34
  • 62
  • 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 Answers3

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,175
  • 9
  • 48
  • 81
  • 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,267
  • 2
  • 18
  • 30
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
  • 81
  • 1
  • 3
  • 1
    Please 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