JsRT-WinRT is a Windows Runtime Component that you can add to your C++ and .NET Windows Store apps to enable them to be scripted with JavaScript. It uses the same Chakra JavaScript engine that powers Microsoft Edge by interacting with the JavaScript Runtime Hosting APIs, also known as JsRT. You don't need to use JsRT-WinRT to host the Chakra engine, but I at least think it'll be an easier entry point, particularly for .NET developers who would have to write a lot of P/Invoke interop code.
JsRT-WinRT tries to provide an API surface which will be familiar both to Windows Runtime developers and JavaScript developers. The API surface is modeled after the way that JavaScript developers see the world from within the JavaScript environment, well-known globals generally live directly off of the JavaScript engine, and the goal is that it's easy to run code and use it to safely extend your app.
To start, add a reference to the Microsoft.Scripting.winmd file (or the Microsoft.Scripting project) to your app. Add a reference to the namespaces in your code file:
using Microsoft.Scripting;
using Microsoft.Scripting.JavaScript;
Then create the runtime and engine:
var settings = new JavaScriptRuntimeSettings();
var runtime = new JavaScriptRuntime(settings);
var engine = runtime.CreateEngine();
To add a host-provided function, you pass a delegate into the Engine's CreateFunction
or SetGlobalFunction
methods:
engine.SetGlobalFunction("alert", (eng, construct, thisObj, args) => {
var message = string.Format(args.First().ToString(), (object[])args.Skip(1).ToArray());
new Windows.Popups.UI.MessageDialog(message).ShowAsync();
return eng.UndefinedValue;
});
Host callback functions get four arguments:
new
operator).this
object context, if any (such as if the developer had called apply
or bind
)Host callback functions must also return a value. Because it's JavaScript, that could be undefined
, which you access via the UndefinedValue
property on the engine.
If you wanted to cause an exception to be thrown from your callback, you have two options:
Error
object with the message in your Exceptionengine.SetException(value)
.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4