Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions src/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ public Config WithMultiMemory(bool enable)
return this;
}

/// <summary>
/// Sets whether or not enable WebAssembly memory64 support.
/// </summary>
/// <param name="enable">True to enable WebAssembly memory64 support or false to disable.</param>
/// <returns>Returns the current config.</returns>
public Config WithMemory64(bool enable)
{
Native.wasmtime_config_wasm_memory64_set(handle, enable);
return this;
}

/// <summary>
/// Sets the compiler strategy to use.
/// </summary>
Expand Down Expand Up @@ -362,43 +373,46 @@ private static class Native
public static extern void wasm_config_delete(IntPtr config);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_debug_info_set(Handle config, bool enable);
public static extern void wasmtime_config_debug_info_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_epoch_interruption_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_epoch_interruption_set(Handle config, bool enable);
public static extern void wasmtime_config_consume_fuel_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_consume_fuel_set(Handle config, bool enable);
public static extern void wasmtime_config_max_wasm_stack_set(Handle config, nuint size);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_max_wasm_stack_set(Handle config, UIntPtr size);
public static extern void wasmtime_config_wasm_threads_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_threads_set(Handle config, bool enable);
public static extern void wasmtime_config_wasm_reference_types_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_reference_types_set(Handle config, bool enable);
public static extern void wasmtime_config_wasm_simd_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_simd_set(Handle config, bool enable);
public static extern void wasmtime_config_wasm_bulk_memory_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_bulk_memory_set(Handle config, bool enable);
public static extern void wasmtime_config_wasm_multi_value_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_multi_value_set(Handle config, bool enable);
public static extern void wasmtime_config_wasm_multi_memory_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_multi_memory_set(Handle config, bool enable);
public static extern void wasmtime_config_wasm_memory64_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_strategy_set(Handle config, byte strategy);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_cranelift_debug_verifier_set(Handle config, bool enable);
public static extern void wasmtime_config_cranelift_debug_verifier_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_cranelift_nan_canonicalization_set(Handle config, bool enable);
public static extern void wasmtime_config_cranelift_nan_canonicalization_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_cranelift_opt_level_set(Handle config, byte level);
Expand Down
27 changes: 20 additions & 7 deletions src/Export.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,34 @@ internal MemoryExport(IntPtr exportType, IntPtr externType) : base(exportType)

unsafe
{
var limits = Memory.Native.wasm_memorytype_limits(type);
Minimum = limits->min;
Maximum = limits->max;
Minimum = (long)Memory.Native.wasmtime_memorytype_minimum(type);

if (Memory.Native.wasmtime_memorytype_maximum(type, out ulong max))
{
Maximum = (long)max;
}

Is64Bit = Memory.Native.wasmtime_memorytype_is64(type);
}
}

/// <summary>
/// The minimum memory size (in WebAssembly page units).
/// Gets the minimum memory size (in WebAssembly page units).
/// </summary>
public uint Minimum { get; private set; }
/// <value>The minimum memory size (in WebAssembly page units).</value>
public long Minimum { get; }

/// <summary>
/// The maximum memory size (in WebAssembly page units).
/// Gets the maximum memory size (in WebAssembly page units).
/// </summary>
public uint Maximum { get; private set; }
/// <value>The maximum memory size (in WebAssembly page units), or <c>null</c> if no maximum is specified.</value>
public long? Maximum { get; }

/// <summary>
/// Gets a value that indicates whether this type of memory represents a 64-bit memory.
/// </summary>
/// <value><c>true</c> if this type of memory represents a 64-bit memory, <c>false</c> if it represents a 32-bit memory.</value>
public bool Is64Bit { get; }

private static class Native
{
Expand Down
27 changes: 20 additions & 7 deletions src/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,34 @@ internal MemoryImport(IntPtr importType, IntPtr externType) : base(importType)

unsafe
{
var limits = Memory.Native.wasm_memorytype_limits(type);
Minimum = limits->min;
Maximum = limits->max;
Minimum = (long)Memory.Native.wasmtime_memorytype_minimum(type);

if (Memory.Native.wasmtime_memorytype_maximum(type, out ulong max))
{
Maximum = (long)max;
}

Is64Bit = Memory.Native.wasmtime_memorytype_is64(type);
}
}

/// <summary>
/// The minimum memory size (in WebAssembly page units).
/// Gets the minimum memory size (in WebAssembly page units).
/// </summary>
public uint Minimum { get; private set; }
/// <value>The minimum memory size (in WebAssembly page units).</value>
public long Minimum { get; }

/// <summary>
/// The maximum memory size (in WebAssembly page units).
/// Gets the maximum memory size (in WebAssembly page units).
/// </summary>
public uint Maximum { get; private set; }
/// <value>The maximum memory size (in WebAssembly page units), or <c>null</c> if no maximum is specified.</value>
public long? Maximum { get; }

/// <summary>
/// Gets a value that indicates whether this type of memory represents a 64-bit memory.
/// </summary>
/// <value><c>true</c> if this type of memory represents a 64-bit memory, <c>false</c> if it represents a 32-bit memory.</value>
public bool Is64Bit { get; }

private static class Native
{
Expand Down
91 changes: 59 additions & 32 deletions src/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,25 @@ public class Memory : IExternal
/// </summary>
/// <param name="store">The store to create the memory in.</param>
/// <param name="minimum">The minimum number of WebAssembly pages.</param>
/// <param name="maximum">The maximum number of WebAssembly pages.</param>
public Memory(IStore store, uint minimum = 0, uint maximum = uint.MaxValue)
/// <param name="maximum">The maximum number of WebAssembly pages, or <c>null</c> to not specify a maximum.</param>
/// <param name="is64Bit"><c>true</c> when memory type represents a 64-bit memory, <c>false</c> when it represents a 32-bit memory.</param>
public Memory(IStore store, long minimum = 0, long? maximum = null, bool is64Bit = false)
{
if (store is null)
{
throw new ArgumentNullException(nameof(store));
}

if (minimum < 0)
{
throw new ArgumentOutOfRangeException(nameof(minimum));
}

if (maximum < 0)
{
throw new ArgumentOutOfRangeException(nameof(maximum));
}

if (maximum < minimum)
{
throw new ArgumentException("The maximum cannot be less than the minimum.", nameof(maximum));
Expand All @@ -32,14 +43,12 @@ public Memory(IStore store, uint minimum = 0, uint maximum = uint.MaxValue)
this.store = store;
Minimum = minimum;
Maximum = maximum;
Is64Bit = is64Bit;

unsafe
{
var limits = new Native.Limits();
limits.min = minimum;
limits.max = maximum;
using var type = new TypeHandle(Native.wasmtime_memorytype_new((ulong)minimum, maximum is not null, (ulong)(maximum ?? 0), is64Bit));

using var type = new TypeHandle(Native.wasm_memorytype_new(limits));
var error = Native.wasmtime_memory_new(store.Context.handle, type, out this.memory);
if (error != IntPtr.Zero)
{
Expand All @@ -54,22 +63,30 @@ public Memory(IStore store, uint minimum = 0, uint maximum = uint.MaxValue)
public const int PageSize = 65536;

/// <summary>
/// The minimum memory size (in WebAssembly page units).
/// Gets the minimum memory size (in WebAssembly page units).
/// </summary>
public uint Minimum { get; private set; }
/// <value>The minimum memory size (in WebAssembly page units).</value>
public long Minimum { get; }

/// <summary>
/// The maximum memory size (in WebAssembly page units).
/// Gets the maximum memory size (in WebAssembly page units).
/// </summary>
public uint Maximum { get; private set; }
/// <value>The maximum memory size (in WebAssembly page units), or <c>null</c> if no maximum is specified.</value>
public long? Maximum { get; }

/// <summary>
/// Gets a value that indicates whether this type of memory represents a 64-bit memory.
/// </summary>
/// <value><c>true</c> if this type of memory represents a 64-bit memory, <c>false</c> otherwise.</value>
public bool Is64Bit { get; }

/// <summary>
/// Gets the current size of the memory, in WebAssembly page units.
/// </summary>
/// <returns>Returns the current size of the memory, in WebAssembly page units.</returns>
public uint GetSize()
public long GetSize()
{
return Native.wasmtime_memory_size(store.Context.handle, this.memory);
return (long)Native.wasmtime_memory_size(store.Context.handle, this.memory);
}

/// <summary>
Expand All @@ -78,7 +95,7 @@ public uint GetSize()
/// <returns>Returns the current length of the memory, in bytes.</returns>
public long GetLength()
{
return checked((long)(nuint)Native.wasmtime_memory_data_size(store.Context.handle, this.memory));
return checked((long)Native.wasmtime_memory_data_size(store.Context.handle, this.memory));
}

/// <summary>
Expand Down Expand Up @@ -485,20 +502,25 @@ public void WriteDouble(long address, double value)
/// <param name="delta">The number of WebAssembly pages to grow the memory by.</param>
/// <returns>Returns the previous size of the Webassembly memory, in pages.</returns>
/// <remarks>This method will invalidate previously returned values from `GetSpan`.</remarks>
public uint Grow(uint delta)
public long Grow(long delta)
{
if (store is null)
{
throw new ArgumentNullException(nameof(store));
}

var error = Native.wasmtime_memory_grow(store.Context.handle, this.memory, delta, out var prev);
if (delta < 0)
{
throw new ArgumentOutOfRangeException(nameof(delta));
}

var error = Native.wasmtime_memory_grow(store.Context.handle, this.memory, (ulong)delta, out var prev);
if (error != IntPtr.Zero)
{
throw WasmtimeException.FromOwnedError(error);
}

return prev;
return (long)prev;
}

Extern IExternal.AsExtern()
Expand All @@ -518,9 +540,14 @@ internal Memory(IStore store, ExternMemory memory)

unsafe
{
var limits = Native.wasm_memorytype_limits(type.DangerousGetHandle());
Minimum = limits->min;
Maximum = limits->max;
Minimum = (long)Native.wasmtime_memorytype_minimum(type.DangerousGetHandle());

if (Native.wasmtime_memorytype_maximum(type.DangerousGetHandle(), out ulong max))
{
Maximum = (long)max;
}

Is64Bit = Native.wasmtime_memorytype_is64(type.DangerousGetHandle());
}
}

Expand All @@ -541,37 +568,37 @@ protected override bool ReleaseHandle()

internal static class Native
{
[StructLayout(LayoutKind.Sequential)]
internal struct Limits
{
public uint min;

public uint max;
}

[DllImport(Engine.LibraryName)]
public static extern IntPtr wasmtime_memory_new(IntPtr context, TypeHandle type, out ExternMemory memory);

[DllImport(Engine.LibraryName)]
public static unsafe extern byte* wasmtime_memory_data(IntPtr context, in ExternMemory memory);

[DllImport(Engine.LibraryName)]
public static extern UIntPtr wasmtime_memory_data_size(IntPtr context, in ExternMemory memory);
public static extern nuint wasmtime_memory_data_size(IntPtr context, in ExternMemory memory);

[DllImport(Engine.LibraryName)]
public static extern uint wasmtime_memory_size(IntPtr context, in ExternMemory memory);
public static extern ulong wasmtime_memory_size(IntPtr context, in ExternMemory memory);

[DllImport(Engine.LibraryName)]
public static extern IntPtr wasmtime_memory_grow(IntPtr context, in ExternMemory memory, uint delta, out uint prev);
public static extern IntPtr wasmtime_memory_grow(IntPtr context, in ExternMemory memory, ulong delta, out ulong prev);

[DllImport(Engine.LibraryName)]
public static extern IntPtr wasmtime_memory_type(IntPtr context, in ExternMemory memory);

[DllImport(Engine.LibraryName)]
public static extern IntPtr wasm_memorytype_new(in Limits limits);
public static extern IntPtr wasmtime_memorytype_new(ulong min, [MarshalAs(UnmanagedType.I1)] bool max_present, ulong max, [MarshalAs(UnmanagedType.I1)] bool is_64);

[DllImport(Engine.LibraryName)]
public static unsafe extern ulong wasmtime_memorytype_minimum(IntPtr type);

[DllImport(Engine.LibraryName)]
[return: MarshalAs(UnmanagedType.I1)]
public static unsafe extern bool wasmtime_memorytype_maximum(IntPtr type, out ulong max);

[DllImport(Engine.LibraryName)]
public static unsafe extern Limits* wasm_memorytype_limits(IntPtr type);
[return: MarshalAs(UnmanagedType.I1)]
public static unsafe extern bool wasmtime_memorytype_is64(IntPtr type);

[DllImport(Engine.LibraryName)]
public static extern void wasm_memorytype_delete(IntPtr handle);
Expand Down
Loading