All Projects → bytecodealliance → wasmtime-dotnet

bytecodealliance / wasmtime-dotnet

Licence: Apache-2.0 license
.NET embedding of Wasmtime https://bytecodealliance.github.io/wasmtime-dotnet/

Programming Languages

C#
18002 projects
WebAssembly
147 projects

Projects that are alternatives of or similar to wasmtime-dotnet

dotnet-opa-wasm
Call Open Policy Agent (OPA) policies in WASM (Web Assembly) from .NET Core
Stars: ✭ 36 (-86.67%)
Mutual labels:  wasmtime
Lunatic
Lunatic is an Erlang-inspired runtime for WebAssembly
Stars: ✭ 2,074 (+668.15%)
Mutual labels:  wasmtime
Wasmtime
Standalone JIT-style runtime for WebAssembly, using Cranelift
Stars: ✭ 6,413 (+2275.19%)
Mutual labels:  wasmtime
wasmtime-py
Python WebAssembly runtime powered by Wasmtime
Stars: ✭ 191 (-29.26%)
Mutual labels:  wasmtime
wasi-experimental-http
Experimental outbound HTTP support for WebAssembly and WASI
Stars: ✭ 107 (-60.37%)
Mutual labels:  wasmtime

wasmtime-dotnet

.NET embedding of Wasmtime

A Bytecode Alliance project

CI status Latest Version Documentation

Installation

You can add a package reference with the .NET SDK:

$ dotnet add package wasmtime

Introduction

For this introduction, we'll be using a simple WebAssembly module that imports a hello function and exports a run function:

(module
  (func $hello (import "" "hello"))
  (func (export "run") (call $hello))
)

To use this module from .NET, create a new console project:

$ mkdir wasmintro
$ cd wasmintro
$ dotnet new console

Next, add a reference to the Wasmtime package:

$ dotnet add package wasmtime

Replace the contents of Program.cs with the following code:

using System;
using Wasmtime;

namespace WasmIntro
{
    class Program
    {
        static void Main(string[] args)
        {
            using var engine = new Engine();

            using var module = Module.FromText(
                engine,
                "hello",
                "(module (func $hello (import \"\" \"hello\")) (func (export \"run\") (call $hello)))"
            );

            using var linker = new Linker(engine);
            using var store = new Store(engine);

            linker.Define(
                "",
                "hello",
                Function.FromCallback(store, () => Console.WriteLine("Hello from C#!"))
            );

            var instance = linker.Instantiate(store, module);
            var run = instance.GetAction("run");
            run();
        }
    }
}

An Engine is created and then a WebAssembly module is loaded from a string in WebAssembly text format.

A Linker defines a function called hello that simply prints a hello message.

The module is instantiated and the instance's run export is invoked.

To run the application, simply use dotnet:

$ dotnet run

This should print Hello from C#!.

Contributing

Building

Use dotnet to build the repository:

$ dotnet build Wasmtime.sln

This will download the latest development snapshot of Wasmtime for your platform.

Testing

Use dotnet to run the unit tests:

$ dotnet test Wasmtime.sln

Creating the NuGet package

Use dotnet to create a NuGet package:

$ cd src
$ dotnet pack Wasmtime.sln -c Release /p:Packing=true

This will create a .nupkg file in src/bin/Release.

By default, local builds will use a -dev suffix for the package to differentiate between official packages and development packages.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].