Skip to content

Commit 52aefc2

Browse files
committed
README.md: Updated example of usage
1 parent de40351 commit 52aefc2

File tree

1 file changed

+70
-46
lines changed

1 file changed

+70
-46
lines changed

README.md

Lines changed: 70 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ MSIE JavaScript Engine for .NET
66
This project is a .NET wrapper for working with the JavaScript engines of Internet Explorer and Edge (JsRT versions of Chakra, ActiveScript version of Chakra and Classic JavaScript Engine).
77
Project was based on the code of [SassAndCoffee.JavaScript](http://github.com/paulcbetts/SassAndCoffee) and [Chakra Sample Hosts](http://github.com/panopticoncentral/chakra-host).
88

9-
MSIE JavaScript Engine requires a installation of Internet Explorer or Edge on the machine and can work in 5 modes, that are defined in the `MsieJavaScriptEngine.JsEngineMode` enumeration:
9+
MSIE JavaScript Engine requires a installation of Internet Explorer or Edge on the machine and can work in 5 modes, that are defined in the <code title="MsieJavaScriptEngine.JsEngineMode">JsEngineMode</code> enumeration:
1010

1111
* `Auto`. Automatically selects the most modern JavaScript engine from available on the machine.
1212
* `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.
1313
* `ChakraActiveScript`. ActiveScript version of Chakra JavaScript engine (supports ECMAScript 5). Requires Internet Explorer 9 or higher on the machine.
14-
* `ChakraIeJsRt`. “Legacy” JsRT version of Chakra JavaScript engine (supports ECMAScript 5). Requires Internet Explorer 11 or higher on the machine.
14+
* `ChakraIeJsRt`. “IE” JsRT version of Chakra JavaScript engine (supports ECMAScript 5). Requires Internet Explorer 11 or Microsoft Edge on the machine.
1515
* `ChakraEdgeJsRt`. “Edge” JsRT version of Chakra JavaScript engine (supports ECMAScript 5). Requires Microsoft Edge on the machine.
1616

1717
The supported .NET types are as follows:
@@ -28,61 +28,85 @@ This library can be installed through NuGet - [http://nuget.org/packages/MsieJav
2828
## Usage
2929
Consider a simple example of usage of the MSIE JavaScript Engine:
3030

31-
namespace MsieJavaScriptEngine.Example.Console
32-
{
33-
using System;
31+
```csharp
32+
namespace MsieJavaScriptEngine.Example.Console
33+
{
34+
using System;
3435

35-
using MsieJavaScriptEngine;
36-
using MsieJavaScriptEngine.Helpers;
36+
using MsieJavaScriptEngine;
37+
using MsieJavaScriptEngine.Helpers;
3738

38-
class Program
39+
class Program
40+
{
41+
static void Main(string[] args)
3942
{
40-
static void Main(string[] args)
43+
try
4144
{
42-
try
43-
{
44-
using (var jsEngine = new MsieJsEngine(new JsEngineSettings
45-
{
46-
EngineMode = JsEngineMode.Auto,
47-
UseEcmaScript5Polyfill = false,
48-
UseJson2Library = false
49-
}))
50-
{
51-
const string expression = "7 * 8 - 20";
52-
var result = jsEngine.Evaluate<int>(expression);
53-
54-
Console.WriteLine("{0} = {1}", expression, result);
55-
}
56-
}
57-
catch (JsEngineLoadException e)
45+
using (var jsEngine = new MsieJsEngine())
5846
{
59-
Console.WriteLine("During loading of JavaScript engine an error occurred.");
60-
Console.WriteLine();
61-
Console.WriteLine(JsErrorHelpers.Format(e));
62-
}
63-
catch (JsRuntimeException e)
64-
{
65-
Console.WriteLine("During execution of JavaScript code an error occurred.");
66-
Console.WriteLine();
67-
Console.WriteLine(JsErrorHelpers.Format(e));
68-
}
47+
const string expression = "7 * 8 - 20";
48+
var result = jsEngine.Evaluate<int>(expression);
6949

70-
Console.ReadLine();
50+
Console.WriteLine("{0} = {1}", expression, result);
51+
}
7152
}
53+
catch (JsEngineLoadException e)
54+
{
55+
Console.WriteLine("During loading of JavaScript engine an error occurred.");
56+
Console.WriteLine();
57+
Console.WriteLine(JsErrorHelpers.Format(e));
58+
}
59+
catch (JsRuntimeException e)
60+
{
61+
Console.WriteLine("During execution of JavaScript code an error occurred.");
62+
Console.WriteLine();
63+
Console.WriteLine(JsErrorHelpers.Format(e));
64+
}
65+
66+
Console.ReadLine();
7267
}
7368
}
69+
}
70+
```
7471

75-
First we create an instance of the `MsieJsEngine` class and pass the following parameters to the constructor:
76-
77-
1. `engineMode` - JavaScript engine mode;
78-
2. `useEcmaScript5Polyfill` - flag for whether to use the ECMAScript 5 Polyfill;
79-
3. `useJson2Library` - flag for whether to use the [JSON2](http://github.com/douglascrockford/JSON-js) library.
80-
81-
The values of constructor parameters in the above code correspond to the default values.
82-
72+
First we create an instance of the <code title="MsieJavaScriptEngine.MsieJsEngine">MsieJsEngine</code> class.
8373
Then we evaluate a JavaScript expression by using of the `Evaluate` method and output its result to the console.
74+
In addition, we provide handling of the following exception types: <code title="MsieJavaScriptEngine.JsEngineLoadException">JsEngineLoadException</code> and <code title="MsieJavaScriptEngine.JsRuntimeException">JsRuntimeException</code>.
75+
76+
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.
77+
Consider in detail properties of the <code title="MsieJavaScriptEngine.JsEngineSettings">JsEngineSettings</code> class:
78+
79+
<table border="1" style="font-size: 0.7em">
80+
<thead>
81+
<tr valign="top">
82+
<th>Property name</th>
83+
<th>Data&nbsp;type</th>
84+
<th>Default value</th>
85+
<th>Description</th>
86+
</tr>
87+
</thead>
88+
<tbody>
89+
<tr valign="top">
90+
<td><code>EngineMode</code></td>
91+
<td><code title="MsieJavaScriptEngine.JsEngineMode">JsEngineMode</code> enumeration</td>
92+
<td><code>Auto</code></td>
93+
<td>JavaScript engine mode.</td>
94+
</tr>
95+
<tr valign="top">
96+
<td><code>UseEcmaScript5Polyfill</code></td>
97+
<td><code title="System.Boolean">Boolean</code></td>
98+
<td><code>false</code></td>
99+
<td>Flag for whether to use the ECMAScript 5 Polyfill.</td>
100+
</tr>
101+
<tr valign="top">
102+
<td><code>UseJson2Library</code></td>
103+
<td><code title="System.Boolean">Boolean</code></td>
104+
<td><code>false</code></td>
105+
<td>Flag for whether to use the <a href="http://github.com/douglascrockford/JSON-js">JSON2</a> library</td>
106+
</tr>
107+
</tbody>
108+
</table>
84109

85-
In addition, we provide handling of the following exception types: `JsEngineLoadException` and `JsRuntimeException`.
86110

87111
## Release History
88112
See the [changelog](CHANGELOG.md).
@@ -92,7 +116,7 @@ See the [changelog](CHANGELOG.md).
92116

93117
## Credits
94118
* [SassAndCoffee.JavaScript](http://github.com/xpaulbettsx/SassAndCoffee) - [License: Microsoft Public License (Ms-PL)](http://github.com/paulcbetts/SassAndCoffee/blob/master/COPYING) Part of the code of this library served as the basis for the ActiveScript version of Chakra and Classic JavaScript Engine.
95-
* [Chakra Sample Hosts](http://github.com/panopticoncentral/chakra-host) - [License: Apache License 2.0 (Apache)](http://github.com/panopticoncentral/chakra-host/blob/master/LICENSE) C# example from this project served as the basis for the JsRT version of Chakra.
119+
* [Chakra Sample Hosts](http://github.com/panopticoncentral/chakra-host) - [License: Apache License 2.0 (Apache)](http://github.com/panopticoncentral/chakra-host/blob/master/LICENSE) C# example from this project served as the basis for the JsRT versions of Chakra.
96120
* [ECMAScript 5 Polyfill](http://nuget.org/packages/ES5) and [MDN JavaScript Polyfills](http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference) - Adds support for many of the new functions in ECMAScript 5 to downlevel browsers.
97121
* [Cross-Browser Split](http://blog.stevenlevithan.com/archives/cross-browser-split) - Adds ECMAScript compliant and uniform cross-browser split method.
98122
* [JSON2 library](http://github.com/douglascrockford/JSON-js) - Adds support of the JSON object from ECMAScript 5 to downlevel browsers.

0 commit comments

Comments
 (0)