Skip to content

Commit

Permalink
add resource support to C# generator (#939)
Browse files Browse the repository at this point in the history
This adds resource support to the C# generator and also fixes a few
miscellaneous issues that were preventing tests from passing.

I believe the code generated for imported resources is reasonably ergonomic,
although I'm pretty new to C#, so I'm open to suggestions to make it more
idiomatic.

As with other languages, the code for exported resources is a bit less
ergonomic, I'm using an `interface` to represent the API, but I also want
implementations to inherit code which manages the resource handle, for which I'm
currently using a `class`.  The upshot is that implementing an exported resource
involves both extending a `class` and implementing an `interface`.  Again, I'm
open to suggestions about improving this.  Note that exporting resources tends
to be a lot less common in end-user code than importing them, so we probably
don't need to obsess over ergonomics in that case.



disable C# runtime tests on non-Windows platforms

As of this writing, they only work on Windows, although Linux support is on the
horizon.



Update lib.rs

Fixes lots of warnings and makes the C# code more idiomatic

rename rep_table.cs to RepTable.cs; address PR feedback



per-resource RepTables; use zero as handle sentinel



add comments explaining handle mgmt for exported resources

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
  • Loading branch information
dicej authored May 1, 2024
1 parent e59581b commit 66a3386
Show file tree
Hide file tree
Showing 18 changed files with 1,262 additions and 322 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/csharp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ wasm-metadata = { workspace = true }
heck = { workspace = true }
clap = { workspace = true, optional = true }
anyhow = { workspace = true }
indexmap = { workspace = true }

[dev-dependencies]
test-helpers = { path = '../test-helpers' }
Expand Down
46 changes: 46 additions & 0 deletions crates/csharp/src/RepTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* This class is used to assign a unique integer identifier to instances of
* exported resources, which the host will use as its "core representation" per
* https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md#definition-types.
* The identifier may be used to retrieve the corresponding instance e.g. when
* lifting a handle as part of the canonical ABI implementation.
*/
internal class RepTable<T> {
private List<object> list = new List<object>();
private int? firstVacant = null;

private class Vacant {
internal int? next;

internal Vacant(int? next) {
this.next = next;
}
}

internal int Add(T v) {
int rep;
if (firstVacant.HasValue) {
rep = firstVacant.Value;
firstVacant = ((Vacant) list[rep]).next;
list[rep] = v;
} else {
rep = list.Count;
list.Add(v);
}
return rep;
}

internal T Get(int rep) {
if (list[rep] is Vacant) {
throw new ArgumentException("invalid rep");
}
return (T) list[rep];
}

internal T Remove(int rep) {
var val = Get(rep);
list[rep] = new Vacant(firstVacant);
firstVacant = rep;
return (T) val;
}
}
Loading

0 comments on commit 66a3386

Please sign in to comment.