|
| 1 | +# Description |
| 2 | +ClearScript is a library that makes it easy to add scripting to your .NET applications. It currently supports JavaScript (via [V8](https://developers.google.com/v8/) and [JScript](http://msdn.microsoft.com/en-us/library/hbxc2t98(v=vs.84).aspx)) and [VBScript](http://msdn.microsoft.com/en-us/library/t0aew7h6(v=vs.84).aspx). |
| 3 | + |
| 4 | +# Features |
| 5 | +* Simple usage; create a script engine, add your objects and/or types, run scripts |
| 6 | +* Support for several script engines: [Google's V8](https://developers.google.com/v8/), [Microsoft's JScript](http://msdn.microsoft.com/en-us/library/hbxc2t98(v=vs.84).aspx) and [VBScript](http://msdn.microsoft.com/en-us/library/t0aew7h6(v=vs.84).aspx) |
| 7 | +* Exposed resources require no modification, decoration, or special coding of any kind |
| 8 | +* Scripts get simple access to most of the features of exposed objects and types: |
| 9 | + * Methods, properties, fields, events |
| 10 | + * (Objects) Indexers, extension methods, conversion operators, explicitly implemented interfaces |
| 11 | + * (Types) Constructors, nested types |
| 12 | +* Full support for generic types and methods, including C#-like type inference and explicit type arguments |
| 13 | +* Scripts can invoke methods with output parameters, optional parameters, and parameter arrays |
| 14 | +* Script delegates enable callbacks into script code |
| 15 | +* Support for exposing all the types defined in one or more assemblies in one step |
| 16 | +* Optional support for importing types and assemblies from script code |
| 17 | +* The host can invoke script functions and access script objects directly |
| 18 | +* Full support for script debugging |
| 19 | + |
| 20 | +# Examples |
| 21 | +``` C# |
| 22 | +using System; |
| 23 | +using Microsoft.ClearScript; |
| 24 | +using Microsoft.ClearScript.JavaScript; |
| 25 | +using Microsoft.ClearScript.V8; |
| 26 | + |
| 27 | +// create a script engine |
| 28 | +using (var engine = new V8ScriptEngine()) |
| 29 | +{ |
| 30 | + // expose a host type |
| 31 | + engine.AddHostType("Console", typeof(Console)); |
| 32 | + engine.Execute("Console.WriteLine('{0} is an interesting number.', Math.PI)"); |
| 33 | + |
| 34 | + // expose a host object |
| 35 | + engine.AddHostObject("random", new Random()); |
| 36 | + engine.Execute("Console.WriteLine(random.NextDouble())"); |
| 37 | + |
| 38 | + // expose entire assemblies |
| 39 | + engine.AddHostObject("lib", new HostTypeCollection("mscorlib", "System.Core")); |
| 40 | + engine.Execute("Console.WriteLine(lib.System.DateTime.Now)"); |
| 41 | + |
| 42 | + // create a host object from script |
| 43 | + engine.Execute(@" |
| 44 | + birthday = new lib.System.DateTime(2007, 5, 22); |
| 45 | + Console.WriteLine(birthday.ToLongDateString()); |
| 46 | + "); |
| 47 | + |
| 48 | + // use a generic class from script |
| 49 | + engine.Execute(@" |
| 50 | + Dictionary = lib.System.Collections.Generic.Dictionary; |
| 51 | + dict = new Dictionary(lib.System.String, lib.System.Int32); |
| 52 | + dict.Add('foo', 123); |
| 53 | + "); |
| 54 | + |
| 55 | + // call a host method with an output parameter |
| 56 | + engine.AddHostObject("host", new HostFunctions()); |
| 57 | + engine.Execute(@" |
| 58 | + intVar = host.newVar(lib.System.Int32); |
| 59 | + found = dict.TryGetValue('foo', intVar.out); |
| 60 | + Console.WriteLine('{0} {1}', found, intVar); |
| 61 | + "); |
| 62 | + |
| 63 | + // create and populate a host array |
| 64 | + engine.Execute(@" |
| 65 | + numbers = host.newArr(lib.System.Int32, 20); |
| 66 | + for (var i = 0; i < numbers.Length; i++) { numbers[i] = i; } |
| 67 | + Console.WriteLine(lib.System.String.Join(', ', numbers)); |
| 68 | + "); |
| 69 | + |
| 70 | + // create a script delegate |
| 71 | + engine.Execute(@" |
| 72 | + Filter = lib.System.Func(lib.System.Int32, lib.System.Boolean); |
| 73 | + oddFilter = new Filter(function(value) { |
| 74 | + return (value & 1) ? true : false; |
| 75 | + }); |
| 76 | + "); |
| 77 | + |
| 78 | + // use LINQ from script |
| 79 | + engine.Execute(@" |
| 80 | + oddNumbers = numbers.Where(oddFilter); |
| 81 | + Console.WriteLine(lib.System.String.Join(', ', oddNumbers)); |
| 82 | + "); |
| 83 | + |
| 84 | + // use a dynamic host object |
| 85 | + engine.Execute(@" |
| 86 | + expando = new lib.System.Dynamic.ExpandoObject(); |
| 87 | + expando.foo = 123; |
| 88 | + expando.bar = 'qux'; |
| 89 | + delete expando.foo; |
| 90 | + "); |
| 91 | + |
| 92 | + // call a script function |
| 93 | + engine.Execute("function print(x) { Console.WriteLine(x); }"); |
| 94 | + engine.Script.print(DateTime.Now.DayOfWeek); |
| 95 | + |
| 96 | + // examine a script object |
| 97 | + engine.Execute("person = { name: 'Fred', age: 5 }"); |
| 98 | + Console.WriteLine(engine.Script.person.name); |
| 99 | + |
| 100 | + // read a JavaScript typed array |
| 101 | + engine.Execute("values = new Int32Array([1, 2, 3, 4, 5])"); |
| 102 | + var values = (ITypedArray<int>)engine.Script.values; |
| 103 | + Console.WriteLine(string.Join(", ", values.ToArray())); |
| 104 | +} |
| 105 | +``` |
0 commit comments