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:
@@ -8,75 +8,6 @@ namespace Wasmtime
|
||||
/// </summary>
|
||||
public class Module : IDisposable
|
||||
{
|
||||
internal Module(Store store, string name, byte[] bytes)
|
||||
{
|
||||
if (store.Handle.IsInvalid)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(store));
|
||||
}
|
||||
|
||||
unsafe
|
||||
{
|
||||
fixed (byte *ptr = bytes)
|
||||
{
|
||||
Interop.wasm_byte_vec_t vec;
|
||||
vec.size = (UIntPtr)bytes.Length;
|
||||
vec.data = ptr;
|
||||
|
||||
Handle = Interop.wasm_module_new(store.Handle, ref vec);
|
||||
}
|
||||
|
||||
if (Handle.IsInvalid)
|
||||
{
|
||||
throw new WasmtimeException($"WebAssembly module '{name}' is not valid.");
|
||||
}
|
||||
}
|
||||
|
||||
Store = store;
|
||||
Name = name;
|
||||
Imports = new Wasmtime.Imports.Imports(this);
|
||||
Exports = new Wasmtime.Exports.Exports(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantiates a WebAssembly module for the given host.
|
||||
/// </summary>
|
||||
/// <param name="host">The host to use for the WebAssembly module's instance.</param>
|
||||
/// <returns>Returns a new <see cref="Instance" />.</returns>
|
||||
public Instance Instantiate(IHost host = null)
|
||||
{
|
||||
return Instantiate(null, host);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantiates a WebAssembly module for the given host.
|
||||
/// </summary>
|
||||
/// <param name="wasi">The WASI instance to use for WASI imports.</param>
|
||||
/// <param name="host">The host to use for the WebAssembly module's instance.</param>
|
||||
/// <returns>Returns a new <see cref="Instance" />.</returns>
|
||||
public Instance Instantiate(Wasi wasi, IHost host = null)
|
||||
{
|
||||
if (!(host?.Instance is null))
|
||||
{
|
||||
throw new InvalidOperationException("The host has already been associated with an instantiated module.");
|
||||
}
|
||||
|
||||
var instance = new Instance(this, wasi, host);
|
||||
|
||||
if (!(host is null))
|
||||
{
|
||||
host.Instance = instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="Store"/> associated with the module.
|
||||
/// </summary>
|
||||
public Store Store { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The name of the module.
|
||||
/// </summary>
|
||||
@@ -108,6 +39,30 @@ namespace Wasmtime
|
||||
}
|
||||
}
|
||||
|
||||
internal Module(Interop.StoreHandle store, string name, byte[] bytes)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
fixed (byte *ptr = bytes)
|
||||
{
|
||||
Interop.wasm_byte_vec_t vec;
|
||||
vec.size = (UIntPtr)bytes.Length;
|
||||
vec.data = ptr;
|
||||
|
||||
Handle = Interop.wasm_module_new(store, ref vec);
|
||||
}
|
||||
|
||||
if (Handle.IsInvalid)
|
||||
{
|
||||
throw new WasmtimeException($"WebAssembly module '{name}' is not valid.");
|
||||
}
|
||||
}
|
||||
|
||||
Name = name;
|
||||
Imports = new Wasmtime.Imports.Imports(this);
|
||||
Exports = new Wasmtime.Exports.Exports(this);
|
||||
}
|
||||
|
||||
internal Interop.ModuleHandle Handle { get; private set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user