Awesome
MSIE JavaScript Engine for .NET
This project is a .NET wrapper for working with the JavaScript engines of Internet Explorer and Edge Legacy (JsRT versions of Chakra, ActiveScript version of Chakra and Classic JavaScript Engine). Project was based on the code of SassAndCoffee.JavaScript, Chakra Sample Hosts and jsrt-dotnet.
MSIE JavaScript Engine requires a installation of Internet Explorer or Edge Legacy on the machine and can work in 5 modes, that are defined in the <code title="MsieJavaScriptEngine.JsEngineMode">JsEngineMode</code> enumeration:
Auto
. Automatically selects the most modern JavaScript engine from available on the machine.Classic
. Classic MSIE JavaScript engine (supports ECMAScript 3 with possibility of using the ECMAScript 5 Polyfill and the JSON2 library). Requires Internet Explorer 6 or higher on the machine. Not supported in version for .NET Core.ChakraActiveScript
. ActiveScript version of Chakra JavaScript engine (supports ECMAScript 5). Requires Internet Explorer 9 or higher on the machine. Not supported in version for .NET Core.ChakraIeJsRt
. “IE” JsRT version of Chakra JavaScript engine (supports ECMAScript 5). Requires Internet Explorer 11 or Microsoft Edge Legacy on the machine.ChakraEdgeJsRt
. “Edge” JsRT version of Chakra JavaScript engine (supports ECMAScript 5). Requires Microsoft Edge Legacy on the machine.
The supported .NET types are as follows:
MsieJavaScriptEngine.Undefined
System.Boolean
System.Int32
System.Double
System.String
Installation
This library can be installed through NuGet - https://www.nuget.org/packages/MsieJavaScriptEngine.
Usage
Consider a simple example of usage of the MSIE JavaScript Engine:
using System;
using MsieJavaScriptEngine;
using MsieJavaScriptEngine.Helpers;
namespace MsieJavaScriptEngine.Example.Console
{
class Program
{
static void Main(string[] args)
{
try
{
using (var jsEngine = new MsieJsEngine())
{
const string expression = "7 * 8 - 20";
var result = jsEngine.Evaluate<int>(expression);
Console.WriteLine("{0} = {1}", expression, result);
}
}
catch (JsEngineLoadException e)
{
Console.WriteLine("During loading of JavaScript engine an error occurred.");
Console.WriteLine();
Console.WriteLine(JsErrorHelpers.GenerateErrorDetails(e));
}
catch (JsScriptException e)
{
Console.WriteLine("During processing of JavaScript code an error occurred.");
Console.WriteLine();
Console.WriteLine(JsErrorHelpers.GenerateErrorDetails(e));
}
catch (JsException e)
{
Console.WriteLine("During working of JavaScript engine an unknown error occurred.");
Console.WriteLine();
Console.WriteLine(JsErrorHelpers.GenerateErrorDetails(e));
}
Console.ReadLine();
}
}
}
First we create an instance of the <code title="MsieJavaScriptEngine.MsieJsEngine">MsieJsEngine</code> class.
Then we evaluate a JavaScript expression by using of the Evaluate
method and output its result to the console.
In addition, we provide handling of the following exception types: <code title="MsieJavaScriptEngine.JsEngineLoadException">JsEngineLoadException</code>, <code title="MsieJavaScriptEngine.JsScriptException">JsScriptException</code> and <code title="MsieJavaScriptEngine.JsException">JsException</code>.
In the MSIE JavaScript Engine, exceptions have the following hierarchy:
- <code title="MsieJavaScriptEngine.JsException">JsException</code>
- <code title="MsieJavaScriptEngine.JsEngineException">JsEngineException</code>
- <code title="MsieJavaScriptEngine.JsEngineLoadException">JsEngineLoadException</code>
- <code title="MsieJavaScriptEngine.JsFatalException">JsFatalException</code>
- <code title="MsieJavaScriptEngine.JsScriptException">JsScriptException</code>
- <code title="MsieJavaScriptEngine.JsCompilationException">JsCompilationException</code>
- <code title="MsieJavaScriptEngine.JsRuntimeException">JsRuntimeException</code>
- <code title="MsieJavaScriptEngine.JsInterruptedException">JsInterruptedException</code>
- <code title="MsieJavaScriptEngine.JsUsageException">JsUsageException</code>
- <code title="MsieJavaScriptEngine.JsEngineException">JsEngineException</code>
Also, when you create an instance of the <code title="MsieJavaScriptEngine.MsieJsEngine">MsieJsEngine</code> class, then you can pass the JavaScript engine settings via the constructor. Consider in detail properties of the <code title="MsieJavaScriptEngine.JsEngineSettings">JsEngineSettings</code> class:
<table border="1" style="font-size: 0.7em"> <thead> <tr valign="top"> <th>Property name</th> <th>Data type</th> <th>Default value</th> <th>Description</th> </tr> </thead> <tbody> <tr valign="top"> <td><code>AllowReflection</code></td> <td><code title="System.Boolean">Boolean</code></td> <td><code>false</code></td> <td> <p>Flag for whether to allow the usage of reflection API in the script code.</p> <p>This affects <code>Object.GetType</code>, <code>Exception.GetType</code>, <code>Exception.TargetSite</code> and <code>Delegate.Method</code>.</p> </td> </tr> <tr valign="top"> <td><code>EnableDebugging</code></td> <td><code title="System.Boolean">Boolean</code></td> <td><code>false</code></td> <td>Flag for whether to allow debugging in Visual Studio by adding the <code>debugger</code> statement to script code.</td> </tr> <tr valign="top"> <td><code>EngineMode</code></td> <td><code title="MsieJavaScriptEngine.JsEngineMode">JsEngineMode</code> enumeration</td> <td><code>Auto</code></td> <td>JavaScript engine mode.</td> </tr> <tr valign="top"> <td><code>MaxStackSize</code></td> <td><code title="System.Int32">Int32</code></td> <td><code>503 808</code> or <code>1 007 616</code></td> <td> <p>Maximum stack size in bytes.</p> <p>Set a <code>0</code> to use the default maximum stack size specified in the header for the executable.</p> </td> </tr> <tr valign="top"> <td><code>UseEcmaScript5Polyfill</code></td> <td><code title="System.Boolean">Boolean</code></td> <td><code>false</code></td> <td>Flag for whether to use the ECMAScript 5 Polyfill.</td> </tr> <tr valign="top"> <td><code>UseJson2Library</code></td> <td><code title="System.Boolean">Boolean</code></td> <td><code>false</code></td> <td>Flag for whether to use the <a href="https://github.com/douglascrockford/JSON-js">JSON2</a> library</td> </tr> </tbody> </table>Release History
See the changelog.
License
Credits
- SassAndCoffee.JavaScript - License: Microsoft Public License (Ms-PL) Part of the code of this library served as the basis for the ActiveScript version of Chakra and Classic JavaScript Engine.
- Chakra Sample Hosts - License: Apache License 2.0 (Apache) C# example from this project served as the basis for the JsRT versions of Chakra.
- jsrt-dotnet - License: The MIT License (MIT) Part of the code of this library is used in the JsRT versions of Chakra.
- ECMAScript 5 Polyfill and MDN JavaScript Polyfills - Adds support for many of the new functions in ECMAScript 5 to downlevel browsers.
- Cross-Browser Split - Adds ECMAScript compliant and uniform cross-browser split method.
- JSON2 library - Adds support of the JSON object from ECMAScript 5 to downlevel browsers.
- UglifyJS - License: BSD License (BSD) JS-files, that used MSIE JS Engine, minificated by using this tool.
Who's Using MSIE JavaScript Engine
If you use the MSIE JavaScript Engine in some project, please send me a message so I can include it in this list:
- BitAdminCore.ALL
- Chevron by Simon Cropp
- JavaScript Engine Switcher by Andrey Taritsyn
- PowerShell.JS by Karl Prosser
- SquishIt by Justin Etheredge and Alex Ullrich
- Strike by Simon Cropp