Introduction
editMeziantou.Framework.WasmEngine runs WebAssembly modules inside a .NET application. It is written in .NET, without native library: it decodes and validates the modules, interprets their code, and compiles the functions that run often to .NET code (IL) when the platform supports dynamic code. The interpreter runs everywhere .NET runs, including Native AOT applications.
dotnet package add Meziantou.Framework.WasmEngineWhat you can do with it
- Compile, validate and instantiate modules, from their binary or text format. See Getting started.
- Give .NET functions to the modules as imports, by position or by module and name. See Host functions and imports.
- Read and write the memories, tables and globals of the instances. See Memories, tables and globals.
- Throw and catch WebAssembly exceptions from .NET, and handle the traps. See Exceptions and traps.
- Interrupt long-running code, limit the stack, the memories and the tables, and choose when the functions are compiled to .NET code. See Limits, interruption and compilation.
- Share memories between threads, with the atomic wait and notify instructions. See Shared memories.
- Trace the compilations and the instantiations, and collect metrics. See Telemetry.
- Call the exports with strongly typed methods, and write the host functions as .NET methods, with the source generator of the package. See Typed exports and imports.
The engine passes the official WebAssembly test suite: see Conformance.
Language support
The features of WebAssembly 3.0 are enabled: the reference types, the typed function references, the GC types (structs, arrays, i31ref), the exceptions, the tail calls, memory64, the multiple memories, the SIMD and relaxed SIMD instructions, and the wide arithmetic. The threads (the shared memories and the atomic instructions) are enabled too. The legacy exception handling instructions (try, catch, catch_all, rethrow, delegate), which toolchains such as Emscripten still emit, are supported too.
Limitations
- WASI is not provided: the modules reach your application only through their imports.
- The component model is not supported.
- The memories are .NET arrays: a memory cannot be larger than the maximum length of a .NET array (about 2 GiB).
- The interpreted code is much slower than native code. The compiled functions are faster, but they are compiled only where dynamic code is supported (not in Native AOT applications).
To run the WebAssembly modules of the scripts of Meziantou.Framework.WebPlatform (the WebAssembly namespace of JavaScript), use Meziantou.Framework.WebPlatform.Wasm, which is based on this engine: see WebAssembly.