Skip to content

Commit

Permalink
Replaced Caller with a ref struct
Browse files Browse the repository at this point in the history
  • Loading branch information
martindevans committed Jan 25, 2023
1 parent c35e09c commit 3917d22
Show file tree
Hide file tree
Showing 7 changed files with 1,069 additions and 868 deletions.
64 changes: 17 additions & 47 deletions src/Caller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Wasmtime
/// <summary>
/// Represents caller information for a function.
/// </summary>
public class Caller : IDisposable
public readonly ref struct Caller
{
internal Caller(IntPtr handle)
{
Expand All @@ -17,7 +17,8 @@ internal Caller(IntPtr handle)
}

this.handle = handle;
store = Context.Store;
this.context = new StoreContext(Native.wasmtime_caller_context(handle));
this.store = context.Store;
}

/// <summary>
Expand All @@ -33,7 +34,7 @@ internal Caller(IntPtr handle)

fixed (byte* ptr = bytes)
{
if (!Native.wasmtime_caller_export_get(NativeHandle, ptr, (UIntPtr)bytes.Length, out var item))
if (!Native.wasmtime_caller_export_get(handle, ptr, (UIntPtr)bytes.Length, out var item))
{
return null;
}
Expand All @@ -44,7 +45,7 @@ internal Caller(IntPtr handle)
return null;
}

return new Memory(Store, item.of.memory);
return new Memory(store, item.of.memory);
}
}
}
Expand All @@ -62,7 +63,7 @@ internal Caller(IntPtr handle)

fixed (byte* ptr = bytes)
{
if (!Native.wasmtime_caller_export_get(NativeHandle, ptr, (UIntPtr)bytes.Length, out var item))
if (!Native.wasmtime_caller_export_get(handle, ptr, (UIntPtr)bytes.Length, out var item))
{
return null;
}
Expand All @@ -73,53 +74,21 @@ internal Caller(IntPtr handle)
return null;
}

return new Function(Store, item.of.func);
return new Function(store, item.of.func);
}
}
}

/// <inheritdoc/>
public void Dispose()
{
this.handle = IntPtr.Zero;
}

/// <summary>
/// Gets the <see cref="Store"/> associated with this caller.
/// </summary>
public Store Store
{
get
{
if (this.handle == IntPtr.Zero)
{
throw new ObjectDisposedException(typeof(Caller).FullName);
}

return this.store;
}
}

internal StoreContext Context => new StoreContext(Native.wasmtime_caller_context(NativeHandle));

private IntPtr NativeHandle
{
get
{
if (this.handle == IntPtr.Zero)
{
throw new ObjectDisposedException(typeof(Caller).FullName);
}

return this.handle;
}
}
public Store Store => store;

/// <summary>
/// Adds fuel to this store for WebAssembly code to consume while executing.
/// </summary>
/// <param name="fuel">The fuel to add to the store.</param>
public void AddFuel(ulong fuel) => Context.AddFuel(fuel);
public void AddFuel(ulong fuel) => context.AddFuel(fuel);

/// <summary>
/// Synthetically consumes fuel from this store.
Expand All @@ -136,36 +105,37 @@ private IntPtr NativeHandle
/// <param name="fuel">The fuel to consume from the store.</param>
/// <returns>Returns the remaining amount of fuel.</returns>
/// <exception cref="WasmtimeException">Thrown if more fuel is consumed than the store currently has.</exception>
public ulong ConsumeFuel(ulong fuel) => Context.ConsumeFuel(fuel);
public ulong ConsumeFuel(ulong fuel) => context.ConsumeFuel(fuel);

/// <summary>
/// Gets the fuel consumed by the executing WebAssembly code.
/// </summary>
/// <returns>Returns the fuel consumed by the executing WebAssembly code or 0 if fuel consumption was not enabled.</returns>
public ulong GetConsumedFuel() => Context.GetConsumedFuel();
public ulong GetConsumedFuel() => context.GetConsumedFuel();

/// <summary>
/// Gets the user-defined data from the Store.
/// </summary>
/// <returns>An object represeting the user defined data from this Store</returns>
public object? GetData() => Store.GetData();
public object? GetData() => store.GetData();

/// <summary>
/// Replaces the user-defined data in the Store.
/// </summary>
public void SetData(object? data) => Store.SetData(data);
public void SetData(object? data) => store.SetData(data);

internal static class Native
{
[DllImport(Engine.LibraryName)]
[return: MarshalAs(UnmanagedType.I1)]
public static unsafe extern bool wasmtime_caller_export_get(IntPtr caller, byte* name, UIntPtr len, out Extern item);
public static extern unsafe bool wasmtime_caller_export_get(IntPtr caller, byte* name, UIntPtr len, out Extern item);

[DllImport(Engine.LibraryName)]
public static extern IntPtr wasmtime_caller_context(IntPtr caller);
}

private IntPtr handle;
private readonly Store store;
private readonly IntPtr handle;
internal readonly Store store;
internal readonly StoreContext context;
}
}
132 changes: 132 additions & 0 deletions src/Delegates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
namespace Wasmtime;

/// <summary>
/// Action accepting a caller
/// </summary>
public delegate void CallerAction(Caller caller);

/// <summary>
/// Action accepting a caller and 1 parameter
/// </summary>
public delegate void CallerAction<in T>(Caller caller, T arg);

/// <summary>
/// Action accepting a caller and 2 parameters
/// </summary>
public delegate void CallerAction<in T1, in T2>(Caller caller, T1 arg1, T2 arg2);

/// <summary>
/// Action accepting a caller and 3 parameters
/// </summary>
public delegate void CallerAction<in T1, in T2, in T3>(Caller caller, T1 arg1, T2 arg2, T3 arg3);

/// <summary>
/// Action accepting a caller and 4 parameters
/// </summary>
public delegate void CallerAction<in T1, in T2, in T3, in T4>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4);

/// <summary>
/// Action accepting a caller and 5 parameters
/// </summary>
public delegate void CallerAction<in T1, in T2, in T3, in T4, in T5>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);

/// <summary>
/// Action accepting a caller and 6 parameters
/// </summary>
public delegate void CallerAction<in T1, in T2, in T3, in T4, in T5, in T6>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6);

/// <summary>
/// Action accepting a caller and 7 parameters
/// </summary>
public delegate void CallerAction<in T1, in T2, in T3, in T4, in T5, in T6, in T7>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7);

/// <summary>
/// Action accepting a caller and 8 parameters
/// </summary>
public delegate void CallerAction<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8);

/// <summary>
/// Action accepting a caller and 9 parameters
/// </summary>
public delegate void CallerAction<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9);

/// <summary>
/// Action accepting a caller and 10 parameters
/// </summary>
public delegate void CallerAction<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10);

/// <summary>
/// Action accepting a caller and 11 parameters
/// </summary>
public delegate void CallerAction<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11);

/// <summary>
/// Action accepting a caller and 12 parameters
/// </summary>
public delegate void CallerAction<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12);

/// <summary>
/// Func accepting a caller
/// </summary>
public delegate TResult CallerFunc<out TResult>(Caller caller);

/// <summary>
/// Func accepting a caller and 1 parameter
/// </summary>
public delegate TResult CallerFunc<in T, out TResult>(Caller caller, T arg);

/// <summary>
/// Func accepting a caller and 2 parameters
/// </summary>
public delegate TResult CallerFunc<in T1, in T2, out TResult>(Caller caller, T1 arg1, T2 arg2);

/// <summary>
/// Func accepting a caller and 3 parameters
/// </summary>
public delegate TResult CallerFunc<in T1, in T2, in T3, out TResult>(Caller caller, T1 arg1, T2 arg2, T3 arg3);

/// <summary>
/// Func accepting a caller and 4 parameters
/// </summary>
public delegate TResult CallerFunc<in T1, in T2, in T3, in T4, out TResult>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4);

/// <summary>
/// Func accepting a caller and 5 parameters
/// </summary>
public delegate TResult CallerFunc<in T1, in T2, in T3, in T4, in T5, out TResult>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);

/// <summary>
/// Func accepting a caller and 6 parameters
/// </summary>
public delegate TResult CallerFunc<in T1, in T2, in T3, in T4, in T5, in T6, out TResult>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6);

/// <summary>
/// Func accepting a caller and 7 parameters
/// </summary>
public delegate TResult CallerFunc<in T1, in T2, in T3, in T4, in T5, in T6, in T7, out TResult>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7);

/// <summary>
/// Func accepting a caller and 8 parameters
/// </summary>
public delegate TResult CallerFunc<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, out TResult>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8);

/// <summary>
/// Func accepting a caller and 9 parameters
/// </summary>
public delegate TResult CallerFunc<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, out TResult>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9);

/// <summary>
/// Func accepting a caller and 10 parameters
/// </summary>
public delegate TResult CallerFunc<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, out TResult>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10);

/// <summary>
/// Func accepting a caller and 11 parameters
/// </summary>
public delegate TResult CallerFunc<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, out TResult>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11);

/// <summary>
/// Func accepting a caller and 12 parameters
/// </summary>
public delegate TResult CallerFunc<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, out TResult>(Caller caller, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12);

Loading

0 comments on commit 3917d22

Please sign in to comment.