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:
Peter Huene
2020-03-24 18:20:22 -07:00
parent 0d5d63fdb1
commit cf1d9ee857
38 changed files with 2149 additions and 2213 deletions

View File

@@ -5,18 +5,8 @@ namespace Wasmtime
/// <summary>
/// Represents a constant WebAssembly global value.
/// </summary>
public class Global<T>
public class Global<T> : IDisposable
{
/// <summary>
/// Creates a new <see cref="Global&lt;T&gt;" /> with the given initial value.
/// </summary>
/// <param name="initialValue">The initial value of the global.</param>
public Global(T initialValue)
{
InitialValue = initialValue;
Kind = Interop.ToValueKind(typeof(T));
}
/// <summary>
/// The value of the global.
/// </summary>
@@ -35,16 +25,58 @@ namespace Wasmtime
Interop.wasm_global_get(Handle.DangerousGetHandle(), v);
// TODO: figure out a way that doesn't box the value
return (T)Interop.ToObject(v);
}
}
}
internal ValueKind Kind { get; private set; }
/// <summary>
/// Gets the value kind of the global.
/// </summary>
/// <value></value>
public ValueKind Kind { get; private set; }
/// <inheritdoc/>
public void Dispose()
{
if (!Handle.IsInvalid)
{
Handle.Dispose();
Handle.SetHandleAsInvalid();
}
}
internal Global(Interop.StoreHandle store, T initialValue)
{
if (!Interop.TryGetValueKind(typeof(T), out var kind))
{
throw new WasmtimeException($"Global variables cannot be of type '{typeof(T).ToString()}'.");
}
Kind = kind;
var value = Interop.ToValue((object)initialValue, Kind);
var valueType = Interop.wasm_valtype_new(value.kind);
var valueTypeHandle = valueType.DangerousGetHandle();
valueType.SetHandleAsInvalid();
using var globalType = Interop.wasm_globaltype_new(
valueTypeHandle,
Interop.wasm_mutability_t.WASM_CONST
);
unsafe
{
Handle = Interop.wasm_global_new(store, globalType, &value);
if (Handle.IsInvalid)
{
throw new WasmtimeException("Failed to create Wasmtime global.");
}
}
}
internal Interop.GlobalHandle Handle { get; set; }
internal T InitialValue { get; private set; }
}
}