Skip to content

A rough comparison to other transpilers?

Nihal Talur edited this page Sep 22, 2016 · 4 revisions

WARNING: This page is written in a relatively sloppy manner. I might rewrite it in the future.

I was asked how SharpJS compares to other transpilers, such as Bridge.NET:

First, SharpJS is built on JSIL. It says so in the project readme and a bunch of other places as well.

JSIL is an awesome IL to JS compiler. Unlike Bridge.NET, which I believe uses Roslyn analysis on the C# code (let me know if i'm wrong or outdated), JSIL actually creates what's basically a .NET VM from decompiled code. The advantage of JSIL is that it just supports more stuff, you're not limited to just C#. JSIL is actually an IL-to-JS converter. JSIL's main purpose seems to be bringing XNA games to the web; there's not much else you can do with the current codebase. That's where SharpJS comes in. It builds on JSIL by adding a bunch of libraries that create an abstraction layer for JS/DOM accessible through C#. For example, there are experimental jQuery bindings in SharpJS. When you compile your app, JSIL converts the decompiled code to JS. A SharpJS app references the SharpJS libraries, which provide an abstraction layer for UI. Basically, here's how it works:

Your App (C#) -> SharpJS UI Library (C#) -> SharpJS DOM/JS bindings (C# with JS interop) -> JSIL (JS) -> browser's JS runtime.

Additionally, I was inspired to make SharpJS because of http://www.cshtml5.com/, a proprietary product. Another advantage of using JSIL as the runtime is that you get access to a much larger chunk of the standard library. Since JSIL/SharpJS actually decompiles your assembly, you can reference any framework library or any library you have a DLL for. It even supports P/Invoke with Emscripten. On the other hand, Bridge.NET (AFAIK) translates C# to JS.

------[My Opinion]------

If you think about it, Bridge and JSIL/SharpJS have fundamentally different purposes, though they do seem quite similar at first glance.

JSIL is more useful for porting already existing C# to the web; you shouldn't have many issues with that (all of the framework that is viable to support is supported) and even P/Invoke will still be available. JSIL literally translates mscorlib.dll, System.dll, and anything else necessary to JS. Bridge.NET on the other hand seems to be more useful where you would like C# syntax/language features for a web application, more like TypeScript. You get better syntax that you can transpile to JS (an ugly language by itself). Overall, which one you use depends on your goals. Each approach has its own disadvantages and advantages. Finally, to clarify, SharpJS uses JSIL as the actual compilation engine. SharpJS is primarily composed of a bunch of UI abstraction layers.

As a fun experimental project, when I had finished a basic UI library for SharpJS, I wrote a library called WinFormsCompat (It's part of SharpJS). Basically, it mirrors the System.Windows.Forms API, but actually renders it with ExaPhaser.WebForms, the first-generation SharpJS UI framework (A second generation framework is in progress). The result is that you can take an existing WinForms app, remove the System.Windows.Forms.dll reference, and reference the WinFormsCompat library instead. When compiled with JSIL/SharpJS, you can actually roughly run the WinForms app in the browser. There is plenty of additional code in WinFormsCompat to take care of windowing and layout (jQuery UI is used for dragging windows), and you end up with a rudimentary desktop-like UI with multiple windows, close buttons, and rough replicas of WinForms controls. You can even use the WinForms designer to make your UI, then recompile with the WinFormsCompat library, and your winforms app will be rendered in the browser. If you're just curious to see how that looks, let me know, I can make a demo app. There's a demo on the project wiki, but that is an outdated version of the library before I added freeform windows and resizing.