Skip to content

A .NET9+ AOT compatible wrapper over the Microsoft "Chakra" JavaScript engine (aka JScript9.dll). => It's just one 2500 lines of C# file that allows you to run Javascript code from .NET

License

Notifications You must be signed in to change notification settings

smourier/JsRuntimeAot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JsRuntimeAot

A .NET9+ AOT compatible wrapper over the Microsoft "Chakra" JavaScript engine (aka JScript9.dll).

=> It's just one 2500 lines of C# file that allows you to run Javascript code from .NET.

Why?

Obviously, the Chakra Javascript Engine is deprecated, however using it has some advantages:

  • It still works fine for most use cases.
  • It's performance is really decent.
  • It's installed in Windows (x86, x64, Arm64), so you don't have to distribute any native binaries, contrary to Clearscript for example.
  • Although its deprecated, it's still maintained by Microsoft (I guess, at least for security fixes).
  • It has VARIANT <=> Javascript object conversion, which ChakraCore hasn't.
  • This all allows for easier .NET AOT compatiblity, which is, AFAIK, not currently the case with 100% .NET Javascript implementations.

So if one just needs "some level of javascript support" closely integrated with a modern .NET application, with zero deployment impact, it can be very useful.

How to use?

Here is some sample code:

var input = "1+2";
var sum = JsRuntime.Eval(input);
Console.WriteLine($"{input} => {sum}");

input = "eval(1+2)";
sum = JsRuntime.Eval(input);
Console.WriteLine($"{input} => {sum}");

using var rt = new JsRuntime();
rt.WithContext(ctx =>
{
    input = "function hello(n) { return 'héééééllooooo'; }";
    rt.RunScript(input);
    var result = ctx.GlobalObject.CallFunction("hello");
    Console.WriteLine($"{input} => {result}");
});

rt.WithContext(ctx =>
{
    input = "function square(n) { return n * n; }";
    rt.RunScript(input);
    var sw = Stopwatch.StartNew();
    var glo = ctx.GlobalObject;
    var max = 1_000_000;
    for (var i = 0; i < max; i++)
    {
        var result = glo.CallFunction("square", null, 5);
        //Console.WriteLine(i + ":" + result);
    }
    Console.WriteLine($"{input} * {max} elapsed => {sw}.");
});

About

A .NET9+ AOT compatible wrapper over the Microsoft "Chakra" JavaScript engine (aka JScript9.dll). => It's just one 2500 lines of C# file that allows you to run Javascript code from .NET

Topics

Resources

License

Stars

Watchers

Forks

Languages