Code Monkey home page Code Monkey logo

embeddedscripts's Introduction

EmbeddedScripts

EmbeddedScripts library provides unified way to run scripts written on C#, JS or Lua using different scripting engines in your .NET 5 application.

Supported engines:

Supported platforms

Script engine Windows Linux macOS Android iOS
Roslyn (scripting) ? ✔¹
Roslyn (compiler) ?
Mono Evaluator⁴ ? ? ?
Jint ? ? ?
ChakraCore³ ? ? ?
ClearScriptV8³ ? ? ?
Moonsharp ? ? ?
Pythonnet² ?
  1. Android requires to install additional nuget package System.Runtime.Loader.
  2. Pythonnet requires 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.
  3. You have to install native binaries for you operating system. We recommend these binaries for ChakraCore and these for ClearScriptV8.
  4. Mono was added as an experimental engine for comparision with Roslyn scripting. It is not thread safe and doesn't provide all features of C#. We don't recommend it for use in production code.

Basic usage

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 stringstring, boolboolean, numeric types → number and numberdouble 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

Not all runners are thread safe, to use some of them with multithreading you have to deal with thread synchronization.

Engine Thread safety
Roslyn (scripting)
Roslyn (compiler)
Mono Evaluator
Jint
ChakraCore
ClearScriptV8
Moonsharp

Performance

We have written benchmark tests to investigate how fast runners works in some situations such as interoperating calls between .NET and scripting engine, integer and floating point computations. Results of benchmark test you can see here.

embeddedscripts's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

bubdm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.