- It provides support for ECMAScript 5.1 - http://www.ecma-international.org/ecma-262/5.1/
- .NET Portable Class Library - http://msdn.microsoft.com/en-us/library/gg597391(v=vs.110).aspx
- .NET Interoperability
Besides Jint is a maintained project, that means that an upcoming update for ECMAScript 6 will be someday possible.
In this article you will learn how to install and use the awesome Jint JS Interpreter in your WinForms application.
Installing Jint with NuGet
The first thing we need to do, is to install the Jint package using the NuGet manager. The installation of a package from Visual Studio is really simple, just look for the Solution Explorer area at the right top of VS, then right click on your project and from the dropdown menu selecte Manage NuGet Packages:
Now the NuGet package manager should start and you will be able to install packages on your project. Go to the Browse Tab of the emergent window and search for jint, in the list it should be the first item on the list (verify that the author is Sebastien Ros), select it and then click on Install:
Once the installation of Jint finishes, you will be able to work with this awesome package in your project. For more information about the Jint project, please visit the official Github repository here.
Before work with the code, don't forget to include Jint in your C# class (wherever you want to use the JS Interpreter):
To understand how Jint works, there's no better way that showing it with examples. In some examples we will need a Form with 2 elements, a button identified as
button1 and a multiline textbox identified as
textBox1, that will contain the script that you want to run:
alert function generates a Dialog that shows a message to the user. This function expects as unique argument a string that represents the message that needs to be shown to the user (
Note that we are not binding directly the MessageBox class of WinForms but a custom Alert function. The script to execute that allow you to test if the alert function was correctly exposed, would be:
And its execution in the example form would be:
However, you are not limited to pass a single argument. You can send many arguments as you want as long as they're declared in your native binding. You need to specify them as arguments in the Action class with its respective type (string, int etc.), for example:
You could now set a custom Title to the alert function:
The execution of the Script should generate something like:
setValue function can be used to expose constants too, from variables in your C# code.
If some argument in the native function isn't provided, you will get a simple string as replacement, namely
Allow access to .NET classes and assemblies
You can allow an engine to access any .NET class by configuring the engine instance like shown in the following example:
// 0 stands for System.Environment.SpecialFolder.Desktop var theFilePath = System.Environment.GetFolderPath(0) + "\\custom_file.txt"; var AFile = new System.IO.StreamWriter(theFilePath); AFile.WriteLine("First Line of file in the desktop"); AFile.WriteLine("Second Line of file in the desktop"); AFile.Close();
You should have now a
Jint.Native.JsValue object that should be stored in a variable. To return the value execute the
Force engine i18n and l10n
You can change the timezone of the Engine in C# by providing it as argument in the Engine:
TimeZoneInfo PST = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); var engine = new Engine(cfg => cfg.LocalTimeZone(PST)); engine.Execute("new Date().toString()"); // Mon Apr 03 2017 06:10:31 GMT-07:00 // Or other LocalTimeZone TimeZoneInfo PST = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); var engine = new Engine(cfg => cfg.LocalTimeZone(PST)); engine.Execute("new Date().toString()"); // Mon Apr 03 2017 09:10:31 GMT-04:00
Or if you work with the localization:
// Import the Globalization type using System.Globalization; CultureInfo FR = CultureInfo.GetCultureInfo("fr-FR"); var engine = new Engine(); engine.Execute("new Number(1.23).toString()"); // 1.23 engine.Execute("new Number(1.23).toLocaleString()"); // 1,23
Happy coding !
Become a more social person