tgjones / wasmtime-dotnet

.NET embedding of Wasmtime https://bytecodealliance.github.io/wasmtime-dotnet/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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 Core SDK:

$ dotnet add package --version 0.15.0-preview1 wasmtime

Note that the --version option is required because the package is currently prerelease.

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 --version 0.15.0-preview1 wasmtime

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

using System;
using Wasmtime;

namespace Tutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            using var store = new Store();

            using var module = store.LoadModuleText(
              "hello",
              "(module (func $hello (import \"\" \"hello\")) (func (export \"run\") (call $hello)))"
            );

            using var host = new Host(store);

            host.DefineFunction(
                "",
                "hello",
                () => Console.WriteLine("Hello from C#!")
            );

            using dynamic instance = host.Instantiate(module);
            instance.run();
        }
    }
}

A Store is created and the WebAssembly module, in text format, is loaded into the store.

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

The module is instantiated into the host and the module'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

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

Testing

Use dotnet to run the unit tests:

$ dotnet test

Creating the NuGet package

Use dotnet to create a NuGet package:

$ cd src
$ dotnet pack -c Release

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.

About

.NET embedding of Wasmtime https://bytecodealliance.github.io/wasmtime-dotnet/

License:Apache License 2.0


Languages

Language:C# 94.0%Language:WebAssembly 6.0%