How to translate (some) C# to JavaScript
If you’ve ever used something like EF Core you may have noticed that methods like LINQ’s Where take an Expression<Func<...>> as the first argument, instead of simply a Func<...>:

Expressions were introduced in .NET 3.5 along with the rest of LINQ and they represent the code tree that a lambda is composed of. For example, if you had a lambda like () => a + b, this is how the resulting expression tree would look like:

.NET 4.0 later introduced ExpressionVisitor, which lets you traverse the expression tree. Take this visitor for example:
public class ConsolePrinterVisitor : ExpressionVisitor
{
//This is just a helper method to get the Expression, normally you would already have it
//so you'd just do ConsolePrinterVisitor.Visit(expression)
public void WriteLambda(Expression<Func<object>> expression)
{
this.Visit(expression);
}
protected override Expression VisitBinary(BinaryExpression node)
{
Console.Write("(");
base.Visit(node.Left);
Console.Write($" {GetOperationChar(node.NodeType)} ");
base.Visit(node.Right);
Console.Write(")");
return node;
}
protected override Expression VisitMember(MemberExpression node)
{
Console.Write(node.Member.Name);
return node;
}
private static string GetOperationChar(ExpressionType type)
{
switch (type)
{
case ExpressionType.Add:
return "+";
case ExpressionType.Subtract:
return "-";
//Add other operations
}
return null;
}
}
Usage:
int a = 1, b = 2, c = 3;
new ConsolePrinterVisitor().WriteLambda(() => a + b - c);
//Output: ((a + b) - c)
new ConsolePrinterVisitor().WriteLambda(() => a + (b - c));
//Output: (a + (b - c))
I’m sure you can already tell where JavaScript comes into all of this. By «simply» extending the visitor class to include method calls, field accesses and other language features you can successfully translate a C# lambda expression to a JS one. This is exactly what this class in my vue-aspvalidate library does in order to reuse a validator lambda in the ASP.NET Core back-end and in the JS/TS front-end! Here’s how that class is used:
Expression<Func<string, bool>> expr = o => o.Length > 4 && Regex.IsMatch(o, "^foo\\w+$", RegexOptions.Multiline | RegexOptions.IgnoreCase);
var js = expr.ToJs();
Console.WriteLine(js);
//Output: o=>o.length>4&&new RegExp("^foo\\w+$","gim").test(o)
//Beautified: o => o.length > 4 && new RegExp("^foo\\w+$", "gim").test(o)
For completeness’ sake, this is how the expression used in this example looks like in tree form:

Reading expressions is a powerful tool to have under your belt, but the real fun part comes with creating expression trees at runtime. This way you can dynamically generate an expression tree at runtime, compile it and run it at the exact same speed as normal, compiled code! This will come in the next post, so make sure to subscribe so you don’t miss it.
Comments
Post a Comment