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?

2
  • 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, 2011 at 16:27
  • See also, stackoverflow.com/questions/7167690/…
    – Cheeso
    Jan 6, 2012 at 1:41

3 Answers 3

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?

1
  • 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. Apr 17, 2013 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.

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

}

1
  • 1
    Please consider adding a code example on stackoverflow as the link might break or the repo might be removed.
    – lloyd
    Aug 7, 2018 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.