Meziantou.Framework.WasmEngine
1.0.3
Prefix Reserved
dotnet add package Meziantou.Framework.WasmEngine --version 1.0.3
NuGet\Install-Package Meziantou.Framework.WasmEngine -Version 1.0.3
<PackageReference Include="Meziantou.Framework.WasmEngine" Version="1.0.3" />
<PackageVersion Include="Meziantou.Framework.WasmEngine" Version="1.0.3" />
<PackageReference Include="Meziantou.Framework.WasmEngine" />
paket add Meziantou.Framework.WasmEngine --version 1.0.3
#r "nuget: Meziantou.Framework.WasmEngine, 1.0.3"
#:package Meziantou.Framework.WasmEngine@1.0.3
#addin nuget:?package=Meziantou.Framework.WasmEngine&version=1.0.3
#tool nuget:?package=Meziantou.Framework.WasmEngine&version=1.0.3
Meziantou.Framework.WasmEngine
A WebAssembly engine written in .NET (no native library): compile, validate and instantiate
WebAssembly 3.0 modules (and the threads), give them .NET functions, read and write their memories, tables and globals,
throw and catch their exceptions, interrupt them, and trace them with an ActivitySource and a Meter.
// A runtime compiles the modules: create one and share it (it can be used from any thread)
using var runtime = new WasmRuntime();
var module = runtime.Compile(WasmRuntime.WatToWasm("""
(module
(func (export "add") (param i32 i32) (result i32)
(i32.add (local.get 0) (local.get 1))))
"""));
// A store holds the instances: use it from one thread at a time
using var store = runtime.CreateStore();
var instance = store.Instantiate(module);
var add = instance.GetFunction("add")!;
var results = add.Call(WasmValue.FromI32(40), WasmValue.FromI32(2));
Assert.Equal(42, results[0].I32);
Host functions
using var runtime = new WasmRuntime();
var module = runtime.Compile(WasmRuntime.WatToWasm("""
(module
(import "env" "log" (func $log (param i32)))
(func (export "run")
(call $log (i32.const 1))
(call $log (i32.const 2))))
"""));
using var store = runtime.CreateStore();
var logged = new List<int>();
var log = store.CreateFunction(
new WasmFunctionType([WasmValueType.I32], []),
(arguments, results) => logged.Add(arguments[0].I32));
// The imports are resolved by their module and name
var instance = store.Instantiate(module, new WasmImports().Define("env", "log", log));
instance.GetFunction("run")!.Call();
Assert.Equal([1, 2], logged);
Typed exports and imports
The source generator of the package writes a class per .wasm or .wat file of the AdditionalFiles items, with a
strongly typed method per exported function, and the host functions of the methods marked with [WasmImport]:
// fib.wat is an AdditionalFiles item of the project: the generator writes the FibModule class
using var runtime = new WasmRuntime();
using var store = runtime.CreateStore();
var fib = FibModule.Instantiate(store, FibModule.Compile(runtime));
Assert.Equal(6765, fib.Fib(20));
Components
The engine runs the components of the component model: the canonical ABI converts their strings, lists, records, variants, resources and streams to and from .NET values.
using var runtime = new WasmRuntime();
// A component that exports greet(name: string) -> string: the canonical ABI copies the strings between the
// memory of the component and .NET, the component allocates its memory with its realloc function
var component = runtime.CompileComponent(WasmRuntime.WatToWasm("""
(component
(core module $M
(memory (export "memory") 1)
(global $heap (mut i32) (i32.const 1024))
(func $realloc (export "realloc") (param i32 i32 i32 i32) (result i32)
(local $pointer i32)
(local.set $pointer (global.get $heap))
(global.set $heap (i32.add (global.get $heap) (local.get 3)))
(local.get $pointer))
(data (i32.const 16) "Hello, ")
(func (export "greet") (param $name i32) (param $length i32) (result i32)
(local $result i32)
(local.set $result (call $realloc (i32.const 0) (i32.const 0) (i32.const 1) (i32.add (local.get $length) (i32.const 7))))
(memory.copy (local.get $result) (i32.const 16) (i32.const 7))
(memory.copy (i32.add (local.get $result) (i32.const 7)) (local.get $name) (local.get $length))
;; The result is the address and the length of the string, at the address 0
(i32.store (i32.const 0) (local.get $result))
(i32.store (i32.const 4) (i32.add (local.get $length) (i32.const 7)))
(i32.const 0)))
(core instance $m (instantiate $M))
(func (export "greet") (param "name" string) (result string)
(canon lift (core func $m "greet") (memory (core memory $m "memory")) (realloc (core func $m "realloc")))))
"""));
using var store = runtime.CreateStore();
var instance = store.InstantiateComponent(component);
var greet = instance.GetFunction("greet")!;
var result = greet.Call(ComponentValue.FromString("World"));
Assert.Equal("Hello, World", result?.AsString());
Documentation
See the documentation: memories, tables and globals, exceptions and traps,
limits and interruption, shared memories, telemetry, conformance, the typed exports and imports, and the component
model. To run the programs compiled for WASI (wasm32-wasip1, and the components of WASI 0.2 and 0.3), use
Meziantou.Framework.WasmEngine.Wasi. To run the WebAssembly
modules of the scripts of Meziantou.Framework.WebPlatform, use
Meziantou.Framework.WebPlatform.Wasm.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
- No dependencies.
-
net11.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Meziantou.Framework.WasmEngine:
| Package | Downloads |
|---|---|
|
Meziantou.Framework.WebPlatform.Wasm
Runs the WebAssembly modules of the scripts of Meziantou.Framework.WebPlatform with Meziantou.Framework.WasmEngine (a WebAssemblyRuntime for the WebAssembly JavaScript API). |
|
|
Meziantou.Framework.WasmEngine.Wasi
WASI for Meziantou.Framework.WasmEngine: WASI Preview 1 (wasi_snapshot_preview1) for modules, and WASI 0.2 and 0.3 for components, with arguments, environment variables, clocks, random numbers, standard streams, exit codes, a sandboxed file system of preopened directories on the disk or in memory, and the sandboxed sockets and HTTP of WASI 0.3 on the network of the machine or in memory. |
GitHub repositories
This package is not used by any popular GitHub repositories.