Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News DuoCode Uses Roslyn to Transpile C# into JavaScript

DuoCode Uses Roslyn to Transpile C# into JavaScript

This item in japanese

DuoCode is a C# to JavaScript compiler that promises to allow developers to build HTML5 applications using C# and the .NET framework inside of Visual Studio. InfoQ has interviewed its creators to learn more.

DuoCode claims to support a number of C# features, including strong-typing, class-based inheritance, reflections, generics, lambda expressions and Linq. Other advantages that DuoCode should provides are:

  • debugging the original C# code: DuoCode uses source mappings that should allow developers to see and step through the original C# code inside Visual Studio or the browser.

  • strongly-typed DOM and WebGL: this can be exemplified in the two examples below:

    using static DuoCode.Dom
    string userAgent = navigator.userAgent
    HTMLScriptElement script = (HTMLScriptElement)document.getElementById(scriptId);
    WebGLRenderingContext gl = canvas.getContext("webgl");
    gl.bindBuffer(GL.ARRAY_BUFFER, bCubeTextureCoords);
    gl.vertexAttribPointer(aTextureCoord, 2, GL.FLOAT, false, 0, 0);
    gl.bindTexture(GL.TEXTURE_2D, texture);
    gl.uniform1(uSampler, 0);
  • compilation speed: according to DuoCode, "most of the times, the compilation is even faster than the standard C# compilation of the same code."

  • support for Continuous Integration: DuoCode claims to be fully integrated with MSBuild and can be run as an MSBuild task, or as a command-line tool.

InfoQ has spoken to Stav Harel, manager of DuoCode launch.

Could you describe a few cases or scenarios where transpiling C# into JavaScript can be advantageous? What kind of customers do you have in mind?

Cross-compiling C# into JavaScript is very advantageous in allowing developers to use C# to develop JavaScript-HTML5 Web applications while enjoying the convenience of the C# language, Visual Studio, and debugging based on source-maps.

A few sample scenarios in which DuoCode can greatly benefit developers: Game development with WebGL; Server-side development with NodeJS; Building mobile applications with Cordova.

Moreover, it is the perfect solution for organizations that have already established their C# assets (projects and code, knowledge and experienced C# developers) to reuse their C# resources and efficiently develop JavaScript projects.

Could you provide some details about the way you use Roslyn and what benefits it brings?

DuoCode "understands" C# using the Roslyn compiler. Roslyn performs the code parsing, syntactic tree (AST) generation and contextual analysis. Then DuoCode takes over to perform the code translation and generates JavaScript code (along with source-maps) while completing language features that exist in C# and are missing in JavaScript.

Reliance on Roslyn has vital importance, as it allows DuoCode to keep up with latest technologies like C# 6.0 and Visual Studio 2015.

There are already a few transpilers from C# into JavaScript. What makes DuoCode unique in your view?

Several attempts to develop C#-to-JavaScript compilers have been introduced in the previous years, but they suffer from clear shortcomings.

Their support of C# is partial at best; their interoperability with existing JS libraries is insufficient; some of them produce a cumbersome code that is not readable (and looks like assembly code); some of them do not permit debugging in C#.

All existing solutions rely on old compilers and are maladapted to new features of Visual Studio 2015.

DuoCode is currently in closed beta. A request to join the program can be submitted from DuoCode web site.

Rate this Article