Introducing new JavaScript Engine YantraJS for DotNet
Akash Kava
Posted on October 27, 2021
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
- Apache 2.0 License
For more details, please visit YantraJS Website
Features
- Compiles JavaScript to .Net Assembly
- Strict Mode Only JavaScript*
- Arrow functions
- Classes
- Enhanced object literals
- Template strings and tagged templates
- Destructuring
-
let
const
- Map, Set, WeakMap, WeakSet
- Symbols
- Subclassable built-ins
- Binary and Octal literals
- Module support
- Null coalesce
- Optional property chain
identifier?.[]
,identifier?.(
,identifier?.identifier
- Rest, Default and Spread Parameters
- Generators, iterators, for..of
- Async/Await
- Optional parameters
- Many ES5 + ES6 features
- CommonJS Module Support
- Easily marshal CLR Object to JavaScript and other way around
- 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
- Support for V8 Debugger Protocol
- Increase ECMAScript conformance
- Faster IL Serialization
- Faster Debugging
- 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
- JSContext - plain JavaScript context
- JSModuleContext - context with modules and clr support
- YantraJSContext - context with modules, clr and CSX Module support
How to use?
Simple Execution
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.
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
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
Originally Posted at Introducing YantraJS - Web Atoms Blog
Posted on October 27, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.