Skip to content

Commit cf0ef9d

Browse files
Added TableKind for tables (#262)
* Replaced `ValueKind` with `TableKind` in `Table`. This properly represents the range of type a `Table` may contain. * Added `Obsolete` annotation to Table constructor that accepts `ValueKind` to steer people towards the new constructor. * Fixed all uses of Obsolete constructor in tests * Update table example. --------- Co-authored-by: Peter Huene <[email protected]>
1 parent 3d4a3e3 commit cf0ef9d

File tree

6 files changed

+56
-16
lines changed

6 files changed

+56
-16
lines changed

examples/table/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using var linker = new Linker(engine);
77
using var store = new Store(engine);
88

9-
var table = new Table(store, ValueKind.FuncRef, null, 4);
9+
var table = new Table(store, TableKind.FuncRef, null, 4);
1010

1111
table.SetElement(0, Function.FromCallback(store, (int a, int b) => a + b));
1212
table.SetElement(1, Function.FromCallback(store, (int a, int b) => a - b));

src/Table.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44

55
namespace Wasmtime
66
{
7+
/// <summary>
8+
/// Represents the possible kinds of WebAssembly values stored in a table
9+
/// </summary>
10+
public enum TableKind
11+
{
12+
/// <summary>
13+
/// The value is a function reference.
14+
/// </summary>
15+
FuncRef = ValueKind.FuncRef,
16+
17+
/// <summary>
18+
/// The value is an external reference.
19+
/// </summary>
20+
ExternRef = ValueKind.ExternRef,
21+
}
22+
723
/// <summary>
824
/// Represents a WebAssembly table.
925
/// </summary>
@@ -17,14 +33,28 @@ public class Table : IExternal
1733
/// <param name="initialValue">The initial value for elements in the table.</param>
1834
/// <param name="initial">The number of initial elements in the table.</param>
1935
/// <param name="maximum">The maximum number of elements in the table.</param>
36+
[Obsolete("Replace ValueKind parameter with TableKind")]
2037
public Table(Store store, ValueKind kind, object? initialValue, uint initial, uint maximum = uint.MaxValue)
38+
: this(store, (TableKind)kind, initialValue, initial, maximum)
39+
{
40+
}
41+
42+
/// <summary>
43+
/// Creates a new WebAssembly table.
44+
/// </summary>
45+
/// <param name="store">The store to create the table in.</param>
46+
/// <param name="kind">The value kind for the elements in the table.</param>
47+
/// <param name="initialValue">The initial value for elements in the table.</param>
48+
/// <param name="initial">The number of initial elements in the table.</param>
49+
/// <param name="maximum">The maximum number of elements in the table.</param>
50+
public Table(Store store, TableKind kind, object? initialValue, uint initial, uint maximum = uint.MaxValue)
2151
{
2252
if (store is null)
2353
{
2454
throw new ArgumentNullException(nameof(store));
2555
}
2656

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

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

157-
this.Kind = ValueType.ToKind(Native.wasm_tabletype_element(type.DangerousGetHandle()));
187+
this.Kind = (TableKind)ValueType.ToKind(Native.wasm_tabletype_element(type.DangerousGetHandle()));
158188

159189
unsafe
160190
{

src/Value.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public static bool IsAssignableFrom(this ValueKind kind, Type type)
5959

6060
internal static class ValueType
6161
{
62+
public static IntPtr FromKind(TableKind kind)
63+
{
64+
return FromKind((ValueKind)kind);
65+
}
66+
6267
public static IntPtr FromKind(ValueKind kind)
6368
{
6469
switch (kind)
@@ -251,6 +256,11 @@ public ValueBox ToValueBox()
251256
}
252257
}
253258

259+
public static Value FromObject(object? o, TableKind kind)
260+
{
261+
return FromObject(o, (ValueKind)kind);
262+
}
263+
254264
public static Value FromObject(object? o, ValueKind kind)
255265
{
256266
var value = new Value();

tests/StoreTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void ItLimitsTableElements()
3939
{
4040
Store.SetLimits(tableElements: 5);
4141

42-
var table = new Table(Store, ValueKind.ExternRef, null, 0);
42+
var table = new Table(Store, TableKind.ExternRef, null, 0);
4343
table.GetSize().Should().Be(0);
4444

4545
table.Grow(5, null);

tests/TableExportsTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ public void ItCreatesExternsForTheTables()
5050

5151
var table1 = instance.GetTable("table1");
5252
table1.Should().NotBeNull();
53-
table1.Kind.Should().Be(ValueKind.FuncRef);
53+
table1.Kind.Should().Be(TableKind.FuncRef);
5454
table1.Minimum.Should().Be(1);
5555
table1.Maximum.Should().Be(10);
5656

5757
var table2 = instance.GetTable("table2");
5858
table2.Should().NotBeNull();
59-
table2.Kind.Should().Be(ValueKind.FuncRef);
59+
table2.Kind.Should().Be(TableKind.FuncRef);
6060
table2.Minimum.Should().Be(10);
6161
table2.Maximum.Should().Be(uint.MaxValue);
6262

6363
var table3 = instance.GetTable("table3");
6464
table3.Should().NotBeNull();
65-
table3.Kind.Should().Be(ValueKind.FuncRef);
65+
table3.Kind.Should().Be(TableKind.FuncRef);
6666
table3.Minimum.Should().Be(100);
6767
table3.Maximum.Should().Be(1000);
6868
}

tests/TableImportBindingTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public void ItFailsToInstantiateWithMissingImport()
3838
[Fact]
3939
public void ItFailsToInstantiateWithTableTypeMismatch()
4040
{
41-
var funcs = new Table(Store, ValueKind.ExternRef, null, 10);
42-
var externs = new Table(Store, ValueKind.ExternRef, null, 10);
41+
var funcs = new Table(Store, TableKind.ExternRef, null, 10);
42+
var externs = new Table(Store, TableKind.ExternRef, null, 10);
4343

4444
Linker.Define("", "funcs", funcs);
4545
Linker.Define("", "externs", externs);
@@ -55,8 +55,8 @@ public void ItFailsToInstantiateWithTableTypeMismatch()
5555
[Fact]
5656
public void ItFailsToInstantiateWithTableLimitsMismatch()
5757
{
58-
var funcs = new Table(Store, ValueKind.FuncRef, null, 10);
59-
var externs = new Table(Store, ValueKind.ExternRef, null, 1);
58+
var funcs = new Table(Store, TableKind.FuncRef, null, 10);
59+
var externs = new Table(Store, TableKind.ExternRef, null, 1);
6060

6161
Linker.Define("", "funcs", funcs);
6262
Linker.Define("", "externs", externs);
@@ -72,8 +72,8 @@ public void ItFailsToInstantiateWithTableLimitsMismatch()
7272
[Fact]
7373
public void ItBindsTheTableCorrectly()
7474
{
75-
var funcs = new Table(Store, ValueKind.FuncRef, null, 10);
76-
var externs = new Table(Store, ValueKind.ExternRef, null, 10);
75+
var funcs = new Table(Store, TableKind.FuncRef, null, 10);
76+
var externs = new Table(Store, TableKind.ExternRef, null, 10);
7777

7878
Linker.Define("", "funcs", funcs);
7979
Linker.Define("", "externs", externs);
@@ -120,8 +120,8 @@ public void ItBindsTheTableCorrectly()
120120
[Fact]
121121
public void ItGrowsATable()
122122
{
123-
var funcs = new Table(Store, ValueKind.FuncRef, null, 10, 20);
124-
var externs = new Table(Store, ValueKind.ExternRef, null, 10, 20);
123+
var funcs = new Table(Store, TableKind.FuncRef, null, 10, 20);
124+
var externs = new Table(Store, TableKind.ExternRef, null, 10, 20);
125125

126126
Linker.Define("", "funcs", funcs);
127127
Linker.Define("", "externs", externs);

0 commit comments

Comments
 (0)