Skip to content

Commit ce46cf4

Browse files
authored
feature(c#): Adds enum lifting and lowering (#821)
* feature(c#): Adds enum lifting and lowering Signed-off-by: James Sturtevant <[email protected]> * use simplier version for conversion Signed-off-by: James Sturtevant <[email protected]> * Add Debug statement Signed-off-by: James Sturtevant <[email protected]> * Add correct qualifier Signed-off-by: James Sturtevant <[email protected]> --------- Signed-off-by: James Sturtevant <[email protected]>
1 parent 671c284 commit ce46cf4

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

crates/csharp/src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ using System.Runtime.CompilerServices;
3333
using System.Collections;
3434
using System.Runtime.InteropServices;
3535
using System.Text;
36+
using System.Diagnostics;
3637
3738
";
3839

@@ -1580,7 +1581,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
15801581
uwrite!(
15811582
self.src,
15821583
"
1583-
public static enum {name} {{
1584+
public enum {name} {{
15841585
{cases}
15851586
}}
15861587
"
@@ -1968,9 +1969,20 @@ impl Bindgen for FunctionBindgen<'_, '_> {
19681969

19691970
Instruction::ResultLift { .. } => todo!("ResultLift"),
19701971

1971-
Instruction::EnumLower { .. } => todo!("EnumLower"),
1972+
Instruction::EnumLower { .. } => results.push(format!("(int){}", operands[0])),
19721973

1973-
Instruction::EnumLift { .. } => todo!("EnumLift"),
1974+
Instruction::EnumLift { ty, .. } => {
1975+
let t = self.gen.type_name_with_qualifier(&Type::Id(*ty), true);
1976+
let op = &operands[0];
1977+
results.push(format!("({}){}", t, op));
1978+
1979+
uwriteln!(
1980+
self.src,
1981+
"Debug.Assert(Enum.IsDefined(typeof({}), {}));",
1982+
t,
1983+
op
1984+
);
1985+
}
19741986

19751987
Instruction::ListCanonLower { .. } => todo!("ListCanonLower"),
19761988

tests/codegen/simple-enum.wit

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package foo:foo;
2+
3+
interface enums {
4+
enum e1 {
5+
a,
6+
b,
7+
c
8+
}
9+
10+
enum e2 {
11+
something,
12+
else
13+
}
14+
15+
e1-arg: func(x: e1);
16+
e2-arg: func(x: e2);
17+
18+
e1-ret: func(x: e1) -> e2;
19+
}
20+
21+
world the-world {
22+
import enums;
23+
export enums;
24+
}

0 commit comments

Comments
 (0)