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:
@@ -5,18 +5,8 @@ namespace Wasmtime
|
||||
/// <summary>
|
||||
/// Represents a mutable WebAssembly global value.
|
||||
/// </summary>
|
||||
public class MutableGlobal<T>
|
||||
public class MutableGlobal<T> : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="MutableGlobal<T>" /> with the given initial value.
|
||||
/// </summary>
|
||||
/// <param name="initialValue">The initial value of the global.</param>
|
||||
public MutableGlobal(T initialValue)
|
||||
{
|
||||
InitialValue = initialValue;
|
||||
Kind = Interop.ToValueKind(typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The value of the global.
|
||||
/// </summary>
|
||||
@@ -32,10 +22,7 @@ namespace Wasmtime
|
||||
unsafe
|
||||
{
|
||||
var v = stackalloc Interop.wasm_val_t[1];
|
||||
|
||||
Interop.wasm_global_get(Handle.DangerousGetHandle(), v);
|
||||
|
||||
// TODO: figure out a way that doesn't box the value
|
||||
return (T)Interop.ToObject(v);
|
||||
}
|
||||
}
|
||||
@@ -46,7 +33,6 @@ namespace Wasmtime
|
||||
throw new InvalidOperationException("The global cannot be used before it is instantiated.");
|
||||
}
|
||||
|
||||
// TODO: figure out a way that doesn't box the value
|
||||
var v = Interop.ToValue(value, Kind);
|
||||
|
||||
unsafe
|
||||
@@ -56,10 +42,53 @@ namespace Wasmtime
|
||||
}
|
||||
}
|
||||
|
||||
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 MutableGlobal(Interop.StoreHandle store, T initialValue)
|
||||
{
|
||||
if (!Interop.TryGetValueKind(typeof(T), out var kind))
|
||||
{
|
||||
throw new WasmtimeException($"Mutable 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_VAR
|
||||
);
|
||||
|
||||
unsafe
|
||||
{
|
||||
Handle = Interop.wasm_global_new(store, globalType, &value);
|
||||
|
||||
if (Handle.IsInvalid)
|
||||
{
|
||||
throw new WasmtimeException("Failed to create mutable Wasmtime global.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal Interop.GlobalHandle Handle { get; set; }
|
||||
|
||||
internal T InitialValue { get; private set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user