Question

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?

Was it helpful?

Solution

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?

OTHER TIPS

Doesn't IE ActiveX use the same JavaScript engine as IE standalone?

You can simply embed Internet Explorer ActiveX frame and keep it hidden.

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);
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top