Skip to content

Commit 66a3386

Browse files
authored
add resource support to C# generator (#939)
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 <[email protected]>
1 parent e59581b commit 66a3386

File tree

18 files changed

+1262
-322
lines changed

18 files changed

+1262
-322
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/csharp/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ wasm-metadata = { workspace = true }
2323
heck = { workspace = true }
2424
clap = { workspace = true, optional = true }
2525
anyhow = { workspace = true }
26+
indexmap = { workspace = true }
2627

2728
[dev-dependencies]
2829
test-helpers = { path = '../test-helpers' }

crates/csharp/src/RepTable.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* This class is used to assign a unique integer identifier to instances of
3+
* exported resources, which the host will use as its "core representation" per
4+
* https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md#definition-types.
5+
* The identifier may be used to retrieve the corresponding instance e.g. when
6+
* lifting a handle as part of the canonical ABI implementation.
7+
*/
8+
internal class RepTable<T> {
9+
private List<object> list = new List<object>();
10+
private int? firstVacant = null;
11+
12+
private class Vacant {
13+
internal int? next;
14+
15+
internal Vacant(int? next) {
16+
this.next = next;
17+
}
18+
}
19+
20+
internal int Add(T v) {
21+
int rep;
22+
if (firstVacant.HasValue) {
23+
rep = firstVacant.Value;
24+
firstVacant = ((Vacant) list[rep]).next;
25+
list[rep] = v;
26+
} else {
27+
rep = list.Count;
28+
list.Add(v);
29+
}
30+
return rep;
31+
}
32+
33+
internal T Get(int rep) {
34+
if (list[rep] is Vacant) {
35+
throw new ArgumentException("invalid rep");
36+
}
37+
return (T) list[rep];
38+
}
39+
40+
internal T Remove(int rep) {
41+
var val = Get(rep);
42+
list[rep] = new Vacant(firstVacant);
43+
firstVacant = rep;
44+
return (T) val;
45+
}
46+
}

0 commit comments

Comments
 (0)