Skip to content

Commit

Permalink
feature(c#): Adds enum lifting and lowering (bytecodealliance#821)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
jsturtevant authored Jan 24, 2024
1 parent 671c284 commit ce46cf4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
18 changes: 15 additions & 3 deletions crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ using System.Runtime.CompilerServices;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
";

Expand Down Expand Up @@ -1580,7 +1581,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
uwrite!(
self.src,
"
public static enum {name} {{
public enum {name} {{
{cases}
}}
"
Expand Down Expand Up @@ -1968,9 +1969,20 @@ impl Bindgen for FunctionBindgen<'_, '_> {

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

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

Instruction::EnumLift { .. } => todo!("EnumLift"),
Instruction::EnumLift { ty, .. } => {
let t = self.gen.type_name_with_qualifier(&Type::Id(*ty), true);
let op = &operands[0];
results.push(format!("({}){}", t, op));

uwriteln!(
self.src,
"Debug.Assert(Enum.IsDefined(typeof({}), {}));",
t,
op
);
}

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

Expand Down
24 changes: 24 additions & 0 deletions tests/codegen/simple-enum.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package foo:foo;

interface enums {
enum e1 {
a,
b,
c
}

enum e2 {
something,
else
}

e1-arg: func(x: e1);
e2-arg: func(x: e2);

e1-ret: func(x: e1) -> e2;
}

world the-world {
import enums;
export enums;
}

0 comments on commit ce46cf4

Please sign in to comment.