Reimplement the C# API.
This commit reimplements the C# API in terms of a Wasmtime linker. It removes the custom binding implementation that was based on reflection in favor of the linker's implementation. This should make the C# API a little closer to the Rust API. The `Engine` and `Store` types have been hidden behind the `Host` type which is responsible for hosting WebAssembly module instances. Documentation and tests have been updated.
This commit is contained in:
@@ -3,29 +3,25 @@ using Wasmtime;
|
||||
|
||||
namespace HelloExample
|
||||
{
|
||||
class Host : IHost
|
||||
{
|
||||
public Instance Instance { get; set; }
|
||||
|
||||
[Import("print_global")]
|
||||
public void PrintGlobal()
|
||||
{
|
||||
Console.WriteLine($"The value of the global is: {Global.Value}.");
|
||||
}
|
||||
|
||||
[Import("global")]
|
||||
public readonly MutableGlobal<int> Global = new MutableGlobal<int>(1);
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
using var engine = new Engine();
|
||||
using var store = engine.CreateStore();
|
||||
using var module = store.CreateModule("global.wasm");
|
||||
using dynamic instance = module.Instantiate(new Host());
|
||||
using var host = new Host();
|
||||
|
||||
var global = host.DefineMutableGlobal("", "global", 1);
|
||||
|
||||
host.DefineFunction(
|
||||
"",
|
||||
"print_global",
|
||||
() => {
|
||||
Console.WriteLine($"The value of the global is: {global.Value}.");
|
||||
}
|
||||
);
|
||||
|
||||
using var module = host.LoadModule("global.wasm");
|
||||
|
||||
using dynamic instance = host.Instantiate(module);
|
||||
instance.run(20);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,26 +3,21 @@ using Wasmtime;
|
||||
|
||||
namespace HelloExample
|
||||
{
|
||||
class Host : IHost
|
||||
{
|
||||
public Instance Instance { get; set; }
|
||||
|
||||
[Import("hello")]
|
||||
public void SayHello()
|
||||
{
|
||||
Console.WriteLine("Hello from C#, WebAssembly!");
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
using var engine = new Engine();
|
||||
using var store = engine.CreateStore();
|
||||
using var module = store.CreateModule("hello.wasm");
|
||||
using dynamic instance = module.Instantiate(new Host());
|
||||
using var host = new Host();
|
||||
|
||||
host.DefineFunction(
|
||||
"",
|
||||
"hello",
|
||||
() => Console.WriteLine("Hello from C#, WebAssembly!")
|
||||
);
|
||||
|
||||
using var module = host.LoadModule("hello.wasm");
|
||||
|
||||
using dynamic instance = host.Instantiate(module);
|
||||
instance.run();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,27 +3,24 @@ using Wasmtime;
|
||||
|
||||
namespace HelloExample
|
||||
{
|
||||
class Host : IHost
|
||||
{
|
||||
public Instance Instance { get; set; }
|
||||
|
||||
[Import("log")]
|
||||
public void Log(int address, int length)
|
||||
{
|
||||
var message = Instance.Externs.Memories[0].ReadString(address, length);
|
||||
Console.WriteLine($"Message from WebAssembly: {message}");
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
using var engine = new Engine();
|
||||
using var store = engine.CreateStore();
|
||||
using var module = store.CreateModule("memory.wasm");
|
||||
using dynamic instance = module.Instantiate(new Host());
|
||||
using var host = new Host();
|
||||
|
||||
host.DefineFunction(
|
||||
"",
|
||||
"log",
|
||||
(Caller caller, int address, int length) => {
|
||||
var message = caller.GetMemory("mem").ReadString(address, length);
|
||||
Console.WriteLine($"Message from WebAssembly: {message}");
|
||||
}
|
||||
);
|
||||
|
||||
using var module = host.LoadModule("memory.wasm");
|
||||
|
||||
using dynamic instance = host.Instantiate(module);
|
||||
instance.run();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user