Introducing new JavaScript Engine YantraJS for DotNet
Tags: javascript, showdev, dotnet
Published: 2021-10-27 08:46:31

We are happy to announce a brand new Open Source JavaScript engine completely written in C# for .NET.

## Managed JavaScript Runtime
YantraJS is a JavaScript runtime written in .NET Standard. Yantra has two components, Expression Compiler and JavaScript engine.

It is written in .NET standard, so it runs everywhere except iOS which enforces JIT restrictions. However, with help of an interpreter you can still use it on iOS.

## Why did we build it?
We wanted a JavaScript engine with complete support of ES6, including generators and async/await. And we want to officially support as a commercial product. YantraJS is open source.

## License
1. Apache 2.0 License

For more details, please visit [YantraJS Website](https://yantrajs.com)

## Features
1. Compiles JavaScript to .Net Assembly 
2. Strict Mode Only JavaScript*
3. Arrow functions
4. Classes
5. Enhanced object literals
6. Template strings and tagged templates
7. Destructuring
8. `let` `const`
9. Map, Set, WeakMap, WeakSet
10. Symbols
11. Subclassable built-ins
12. Binary and Octal literals
13. Module support
14. Null coalesce
15. Optional property chain `identifier?.[]`, `identifier?.(`, `identifier?.identifier`
16. Rest, Default and Spread Parameters
17. Generators, iterators, for..of
18. Async/Await
19. Optional parameters
20. Many ES5 + ES6 features
21. CommonJS Module Support
21. Easily marshal CLR Object to JavaScript and other way around
23. CSX Module support

`*` Most JavaScript today is available in strict mode, we do not feel any need to support non strict mode as modules are strict by default.

## Roadmap
1. Support for V8 Debugger Protocol
2. Increase ECMAScript conformance
3. Faster IL Serialization
4. Faster Debugging
5. Support for module pollyfills (ability to redirect default node modules, to support dual platform)

## ECMAScript Conformance
Currently we are seeing more than 70% conformance to ECMAScript, reaching 100% is little out of scope as it is very huge and Yantra is only one year old. We are focusing on supporting most used JavaScript patterns instead of targeting 100% compliance due to limited development bandwidth.

## Expression Compiler
YantraJS is built on custom Expression Compiler, which allows us to create expressions similar to that of Linq Expressions. Expression Compiler has several methods to generate IL, you can compile expression to `MethodBuilder`. Since there is no support for Linq to compile expressions to `MethodBuilder`, Yantra Expression Compiler was written from ground up to support saving IL to various ways.

## Engine Types
1. JSContext - plain JavaScript context
2. JSModuleContext - context with modules and clr support
3. YantraJSContext - context with modules, clr and CSX Module support

## How to use?

### Simple Execution
```c#
var context = new JSContext();

// create global function
context["add"] = new JSFunction((in Arguments a) => {
    return new JSNumber(
         (a[0]?.IntValue ?? 0) + (a[1]?.IntValue ?? 0)
    );
});

var result = context.FastEval("add(4,5)", "script.js");
```

### Wrap CLR Object
Custom CLR types can be wrapped in ClrProxy which will allow you to call any methods directly from JavaScript.

```c#
context["createUri"] = context.CreateFunction((in Arguments a) => {
    var uri = new Uri(a[0]?.ToString() 
          ?? throw context.NewReferenceError(
                   "At least one parameter expected");
    return new ClrProxy(uri);
}, "add");
var result = context.FastEval(
      "var uri = createUri('https://yantrajs.com'); uri.host");
Console.WriteLine(result);
```

For more information on how to use various types, please visit [YantraJS Examples](https://github.com/yantrajs/yantra/wiki/JavaScript-Engine-Example)

##  Alternative to Razor View in ASP.NET Core
We have created our website using JavaScript as view instead of Razor view, though it started as a simple application, but we realized that by using JavaScript as view, we can easily plugin Server Side Rendering and improve page delivery speed. However, using traditional JSDom is not yet supported due to very heavy dependency on various built in node modules. But you can easily create a wrapper with mocks to render content of your React/Angular components on server easily with YantraJS. Check out source code for our website at [Github Repository for YantraJS Website](https://github.com/yantrajs/web-site)

Originally Posted at [Introducing YantraJS - Web Atoms Blog](https://www.webatoms.in/blog/yantra-js/Introducing-new-JavaScript-Engine-YantraJS-for-DotNet-2g)