|
@@ -15,7 +15,7 @@ It's available on nuget at https://www.nuget.org/packages/Jint
|
|
|
|
|
|
This example defines a new value named `log` pointing to `Console.WriteLine`, then executes
|
|
|
a script calling `log('Hello World!')`.
|
|
|
-
|
|
|
+```csharp
|
|
|
var engine = new Engine()
|
|
|
.SetValue("log", new Action<object>(Console.WriteLine))
|
|
|
;
|
|
@@ -27,18 +27,18 @@ a script calling `log('Hello World!')`.
|
|
|
|
|
|
hello();
|
|
|
");
|
|
|
-
|
|
|
+```
|
|
|
Here, the variable `x` is set to `3` and `x * x` is executed in JavaScript. The result is returned to .NET directly, in this case as a `double` value `9`.
|
|
|
-
|
|
|
+```csharp
|
|
|
var square = new Engine()
|
|
|
.SetValue("x", 3) // define a new variable
|
|
|
.Execute("x * x") // execute a statement
|
|
|
.GetCompletionValue() // get the latest statement completion value
|
|
|
.ToObject() // converts the value to .NET
|
|
|
;
|
|
|
-
|
|
|
+```
|
|
|
You can also directly pass POCOs or anonymous objects and use them from JavaScript. In this example for instance a new `Person` instance is manipulated from JavaScript.
|
|
|
-
|
|
|
+```csharp
|
|
|
var p = new Person {
|
|
|
Name = "Mickey Mouse"
|
|
|
};
|
|
@@ -47,9 +47,9 @@ You can also directly pass POCOs or anonymous objects and use them from JavaScri
|
|
|
.SetValue("p", p)
|
|
|
.Execute("p.Name === 'Mickey Mouse')
|
|
|
;
|
|
|
-
|
|
|
+```
|
|
|
If you need to pass a JavaScript callback to the CLR, then it will be converted to a `Delegate`. You can also use it as a parameter of a CLR method from JavaScript.
|
|
|
-
|
|
|
+```csharp
|
|
|
var function = new Engine()
|
|
|
.Execute("function add(a, b) { return this + a + b; }");
|
|
|
.GetGlobalValue("add")
|
|
@@ -59,37 +59,37 @@ If you need to pass a JavaScript callback to the CLR, then it will be converted
|
|
|
new JsValue("foo") /* thisArg */,
|
|
|
new JsValue[] { 1, "bar" } /* arguments */,
|
|
|
); // "foo1bar"
|
|
|
-
|
|
|
+```
|
|
|
## Accessing .NET assemblies and classes
|
|
|
|
|
|
You can allow an engine to access any .NET class by configuring the engine instance like this:
|
|
|
-
|
|
|
+```csharp
|
|
|
var engine = new Engine(cfg => cfg.AllowClr())
|
|
|
-
|
|
|
+```
|
|
|
Then you have access to the `System` namespace as a global value. Here is how it's used in the context on the command line utility:
|
|
|
-
|
|
|
+```javascript
|
|
|
jint> var file = new System.IO.File('log.txt');
|
|
|
jint> file.WriteLine('Hello World !');
|
|
|
jint> file.Dispose();
|
|
|
-
|
|
|
+```
|
|
|
And even create shortcuts to commong .NET methods
|
|
|
-
|
|
|
+```javascript
|
|
|
jint> var log = System.Console.WriteLine;
|
|
|
jint> log('Hello World !');
|
|
|
=> "Hello World !"
|
|
|
-
|
|
|
+```
|
|
|
When allowing the CLR, you can optionnally pass custom assemblies to load types from.
|
|
|
-
|
|
|
+```csharp
|
|
|
var engine = new Engine(cfg => cfg
|
|
|
.AllowClr(typeof(Bar).Assembly)
|
|
|
)
|
|
|
-
|
|
|
+```
|
|
|
and then to assign local namespaces the same way `System` does it for you, use `importNamespace`
|
|
|
-
|
|
|
- var Foo = importNamespace('Foo');
|
|
|
- var bar = new Foo.Bar();
|
|
|
- log(bar.ToString());
|
|
|
-
|
|
|
+```javascript
|
|
|
+ jint> var Foo = importNamespace('Foo');
|
|
|
+ jint> var bar = new Foo.Bar();
|
|
|
+ jint> log(bar.ToString());
|
|
|
+```
|
|
|
## Implemented features:
|
|
|
|
|
|
- ECMAScript 5.1 test suite (http://test262.ecmascript.org/)
|