Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added TableKind for tables #262

Merged
merged 4 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 33 additions & 3 deletions src/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@

namespace Wasmtime
{
/// <summary>
/// Represents the possible kinds of WebAssembly values stored in a table
/// </summary>
public enum TableKind
{
/// <summary>
/// The value is a function reference.
/// </summary>
FuncRef = ValueKind.FuncRef,

/// <summary>
/// The value is an external reference.
/// </summary>
ExternRef = ValueKind.ExternRef,
}

/// <summary>
/// Represents a WebAssembly table.
/// </summary>
Expand All @@ -17,14 +33,28 @@ public class Table : IExternal
/// <param name="initialValue">The initial value for elements in the table.</param>
/// <param name="initial">The number of initial elements in the table.</param>
/// <param name="maximum">The maximum number of elements in the table.</param>
[Obsolete("Replace ValueKind parameter with TableKind")]
public Table(Store store, ValueKind kind, object? initialValue, uint initial, uint maximum = uint.MaxValue)
: this(store, (TableKind)kind, initialValue, initial, maximum)
{
}

/// <summary>
/// Creates a new WebAssembly table.
/// </summary>
/// <param name="store">The store to create the table in.</param>
/// <param name="kind">The value kind for the elements in the table.</param>
/// <param name="initialValue">The initial value for elements in the table.</param>
/// <param name="initial">The number of initial elements in the table.</param>
/// <param name="maximum">The maximum number of elements in the table.</param>
public Table(Store store, TableKind kind, object? initialValue, uint initial, uint maximum = uint.MaxValue)
{
if (store is null)
{
throw new ArgumentNullException(nameof(store));
}

if (kind != ValueKind.ExternRef && kind != ValueKind.FuncRef)
if (kind != TableKind.ExternRef && kind != TableKind.FuncRef)
{
throw new WasmtimeException($"Table elements must be externref or funcref.");
}
Expand Down Expand Up @@ -63,7 +93,7 @@ public Table(Store store, ValueKind kind, object? initialValue, uint initial, ui
/// Gets the value kind of the table.
/// </summary>
/// <value></value>
public ValueKind Kind { get; private set; }
public TableKind Kind { get; private set; }

/// <summary>
/// The minimum table element size.
Expand Down Expand Up @@ -154,7 +184,7 @@ internal Table(Store store, ExternTable table)
using var type = new TypeHandle(Native.wasmtime_table_type(store.Context.handle, this.table));
GC.KeepAlive(store);

this.Kind = ValueType.ToKind(Native.wasm_tabletype_element(type.DangerousGetHandle()));
this.Kind = (TableKind)ValueType.ToKind(Native.wasm_tabletype_element(type.DangerousGetHandle()));

unsafe
{
Expand Down
10 changes: 10 additions & 0 deletions src/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public static bool IsAssignableFrom(this ValueKind kind, Type type)

internal static class ValueType
{
public static IntPtr FromKind(TableKind kind)
{
return FromKind((ValueKind)kind);
}

public static IntPtr FromKind(ValueKind kind)
{
switch (kind)
Expand Down Expand Up @@ -251,6 +256,11 @@ public ValueBox ToValueBox()
}
}

public static Value FromObject(object? o, TableKind kind)
{
return FromObject(o, (ValueKind)kind);
}

public static Value FromObject(object? o, ValueKind kind)
{
var value = new Value();
Expand Down
6 changes: 3 additions & 3 deletions tests/TableExportsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ public void ItCreatesExternsForTheTables()

var table1 = instance.GetTable("table1");
table1.Should().NotBeNull();
table1.Kind.Should().Be(ValueKind.FuncRef);
table1.Kind.Should().Be(TableKind.FuncRef);
table1.Minimum.Should().Be(1);
table1.Maximum.Should().Be(10);

var table2 = instance.GetTable("table2");
table2.Should().NotBeNull();
table2.Kind.Should().Be(ValueKind.FuncRef);
table2.Kind.Should().Be(TableKind.FuncRef);
table2.Minimum.Should().Be(10);
table2.Maximum.Should().Be(uint.MaxValue);

var table3 = instance.GetTable("table3");
table3.Should().NotBeNull();
table3.Kind.Should().Be(ValueKind.FuncRef);
table3.Kind.Should().Be(TableKind.FuncRef);
table3.Minimum.Should().Be(100);
table3.Maximum.Should().Be(1000);
}
Expand Down