Jint is a Javascript interpreter for .NET. Jint doesn't compile Javascript to .NET bytecode and in this sense might be best suited for projects requiring to run relatively small scripts faster, or which need to run on different platforms. It's available on nuget at https://www.nuget.org/packages/Jint
- Full 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
This example defines a new value named log pointing to Console.WriteLine, then executes
a script calling log('Hello World!').
varengine=newEngine().SetValue("log",newAction<object>(Console.WriteLine));engine.Execute(@" function hello() { 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.
varsquare=newEngine().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.
varp=newPerson{Name="Mickey Mouse"};varengine=newEngine().SetValue("p",p).Execute("p.Name === 'Mickey Mouse')
;You can invoke JavaScript function reference
varadd=newEngine().Execute("function add(a, b) { return a + b; }");.GetValue("add");add.Invoke(1,2);// -> 3or directly by name
varengine=newEngine().Execute("function add(a, b) { return a + b; }");engine.Invoke("add",1,2);// -> 3You can allow an engine to access any .NET class by configuring the engine instance like this:
varengine=newEngine(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:
jint>varfile=newSystem.IO.File('log.txt');jint>file.WriteLine('Hello World !');jint>file.Dispose();And even create shortcuts to commong .NET methods
jint>varlog=System.Console.WriteLine;jint>log('Hello World !');=>"Hello World !"When allowing the CLR, you can optionally pass custom assemblies to load types from.
varengine=newEngine(cfg =>cfg.AllowClr(typeof(Bar).Assembly));and then to assign local namespaces the same way System does it for you, use importNamespace
jint>varFoo=importNamespace('Foo');jint>varbar=newFoo.Bar();jint>log(bar.ToString());Generic types are also supported. Here is how to declare, instantiate and use a List<string>:
jint>varListOfString=System.Collections.Generic.List(System.String);jint>varlist=newListOfString();jint>list.Add('foo');jint>list.Add(1);// automatically converted to Stringjint>list.Count;// 2- ECMAScript 5.1 test suite (http://test262.ecmascript.org/)
- Manipulate CLR objects from JavaScript, including:
- Single values
- Objects
- Properties
- Methods
- Delegates
- Anonymous objects
- Convert JavaScript values to CLR objects
- Primitive values
- Object -> expando objects (
IDictionary<string, object>and dynamic) - Array -> object[]
- Date -> DateTime
- number -> double
- string -> string
- boolean -> bool
- Regex -> RegExp
- Function -> Delegate