BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Wasmtime Enables Running WebAssembly from .NET Programs

Wasmtime Enables Running WebAssembly from .NET Programs

While momentum around WebAssembly keeps growing, WebAssembly runtime Wasmtime just added support for .NET Core, aimed to enable to run WebAssembly code from within .NET Core programs.

WebAssembly is a safe, portable, low-level code format designed for efficient execution and compact representation. WebAssembly brings portability to an extreme by attempting to be as hardware-independent, language-independent, and platform-independent as possible.

According to Mozilla engineer Peter Huene, integrating WebAssembly with .NET enables easier code sharing and better untrusted code isolation.

Huene says sharing WebAssembly modules is significantly easier than sharing native libraries, e.g. C libraries, which requires platform-specific builds. Instead, WebAssembly modules can be distributed without modifications.

As an example of this, Huene shows how you can create a library in Rust and compile it to WebAssembly to integrate it with .NET without concerns for the actual processor architecture or operating system your .NET program will run on. This workflow is not yet perfectly streamlined, since you have to deal with a number of low-level details such as converting between values, but things should improve in the future, says Huene, thanks to the upcoming WebAssembly interface types proposal. This will improve interoperability and simplify the exchange of complex data types between WebAssembly and .NET without requiring too much of glue code to do value marshalling and making a WebAssembly module appear just as any other .NET package.

Untrusted code isolation is made possible by WebAssembly's requirement of explicitly importing all external functions that a module is allowed to use and only accessing a region of memory reserved by the host platform. This makes WebAssembly modules effectively run inside a sandbox that enables the execution of foreign plugins with the guarantee they cannot access the host platform indiscriminately.

In his article, Huene provides lots of valuable information to any developer aiming to use WebAssembly from within a .NET program and covering all aspects of the process. This includes creating the WebAssembly code to solve your problem; inspecting which functionality the module requires from the host and which it provides to the host; creating a .NET project and importing the WebAssembly module into it as well as exposing .NET functions to the WebAssembly module.

Wasmtime is not the only WebAssembly runtime that aims to enable running WebAssembly programs outside of the browser. In particular, it is worth mentioning Wasmer here, which is as general purpose as Wasmtime, and WebAssembly Micro Runtime, which is instead aimed at small-footprint devices. Wasmer can also be used on .NET thanks to Wasmer-sharp, created by Miguel de Icaza. Both Wasmtime and Wasmer are under active development and the two projects are somewhat competing against each other although their goals are not entirely overlapping.

Rate this Article

Adoption
Style

BT