Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Using Mono to Compile C# to WebAssembly

Using Mono to Compile C# to WebAssembly

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