EmbeddedScripts library provides unified way to run scripts written on C#, JS or Lua using different scripting engines in your .NET 5 application.
- C#: Roslyn's CSharp.Scripting and CSharpCompilation and Mono Evaluator. Mono Evaluator implements some kind of subset of C# with additional language features and limitations. For more informaion refer the official docs.
- JS: Jint, ChakraCore, ClearScriptV8
- Python: pythonnet
- Lua: Moonsharp
Script engine | Windows | Linux | macOS | Android | iOS |
---|---|---|---|---|---|
Roslyn (scripting) | ✔ | ✔ | ? | ✔¹ | ✔ |
Roslyn (compiler) | ✔ | ✔ | ? | ✔ | ✔ |
Mono Evaluator | ✔ | ✔ | ? | ? | ? |
Jint | ✔ | ✔ | ? | ? | ? |
ChakraCore | ✔ | ✔ | ? | ? | ? |
ClearScriptV8 | ✔ | ✔ | ? | ? | ? |
Moonsharp | ✔ | ✔ | ? | ? | ? |
Pythonnet² | ✔ | ✔ | ? | ? | ? |
- Android requires to install additional nuget package
System.Runtime.Loader
. - Pythonnet requres installed python in your environment. To setup runner you have to provide path to python library via
PythonNetRunner.PythonDll
static field wich is synonim to PythonDLL static field of pythonnet library. More on pythonnet specifity you can read in docs.
Just create one of runners and call RunCodeAsync
method:
var code = @"
let a = 1;
let b = 2;
let c = a + b;
";
var runner = new JintCodeRunner();
await runner.RunCodeAsync(code);
ChakraCoreRunner
and ClearScriptV8Runner
implement IDisposable
interface, so you need to call Dispose
method
after usage of them or use them inside using
scope.
Besides simple script running you can expose some object from .NET to script using Register
method
var countOfCalls = 0;
Action func = () => countOfCalls++;
runner.Register<Action>(func, "func");
await runner.RunAsync("func()");
// coutOfCalls will be equal to 1
You can chain registration calls
runner
.Register<Func<int, int, int>>((a, b) => a + b, "add")
.Register<Action<string>>(Console.WriteLine, "log");
All runners, except CompiledCodeRunner
, supports expression evaluation using Evaluate
method.
var sum = await runner.EvaluateAsync<int>(1 + 2);
// sum will be equal to 3
Every runner has its own list of supported .NET types
Script engine | Supported types |
---|---|
Roslyn (scripting) | All |
Roslyn (compiler) | All |
Mono Evaluator | All |
Jint | Check Jint's readme |
ChakraCore | Only primitives (string , bool , numeric types) and synchronous Func /Action |
ClearScriptV8 | Check ClearScript's docs |
Moonsharp | Check Moonsharp's docs |
and marshalling logic.
Script engine | Marshaling logic |
---|---|
Roslyn (scripting) | One-to-one |
Roslyn (compiler) | One-to-one |
Mono Evaluator | One-to-one |
Jint | Check Jint's readme |
ChakraCore | string <-> string , bool <-> boolean , numeric types -> number and number -> double or int . You can't marshal JS function to Action or Func at this moment |
ClearScriptV8 | From .NET to JS, From JS to .NET |
Moonsharp | Check Moonsharp's docs |
Pythonnet | Check pythonnet website |