BT

Using Mono to Compile C# to WebAssembly

| by Jeff Martin Follow 17 Followers on Jan 30, 2018. Estimated reading time: 1 minute |

Since all major web browsers now include support for WebAssembly, developers can now look to having a new platform for which they can target their applications. The standard toolchain provided by the WebAssembly group will compile C/C++ to WebAssembly which doesn't help developers using other languages. Fortunately for C# programmers, the Mono Project is working on two different approaches to support WebAssembly.

The first approach utilizes the Mono IL interpreter to run managed code (like C#) at runtime. The goal of this approach is to support rapid prototyping and development. This second method would be best used when an application is ready to be shipped to a customer. This approach uses static compilation to produce a WASM file that for native execution by a browser that supports WebAssembly.

To create these WASM files, the Mono project has introduced a new tool, mono-wasm. It takes C# assemblies as input and generates LLVM bitcode for use with the LLVM WebAssembly backend. The resulting WASM file produced by mono-wasm includes all of the dependencies needed for the C# program's execution, including the mscorlib.dll, C library, and the Mono runtime. Since the LLVM linker recently added support for WebAssembly, mono-wasm supports incremental compilation. This greatly reduces compilation times as code that doesn't change (mscorlib.dll, Mono runtime, etc.) does not need to be recompiled while developing your application.

A lot of work has been accomplished by the project, but primary developer Laurent Sansonetti does point out that it is still pre-alpha. In the new near term, outstanding goals include fixing garbage collection and preparing a formal alpha release. Longer term the project will look at shrinking the size of WASM executables (the "hello world" example is 10 megabytes), updating the Mono compiler so that it can target WASM32 directly, and improving C#/JavaScript interoperation.

While this project is very much under active development, curious developers or those seeking to contribute can find the project's current code on GitHub.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT