Skip to content

Commit c2e7892

Browse files
authored
Use a generic Exception type to make handling easier (#1109)
Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
1 parent ad6dbc5 commit c2e7892

File tree

2 files changed

+39
-46
lines changed

2 files changed

+39
-46
lines changed

crates/csharp/src/lib.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,14 @@ impl WorldGenerator for CSharp {
588588
NestingLevel = level;
589589
}}
590590
}}
591+
592+
{access} class WitException<T>: WitException {{
593+
{access} T TypedValue {{ get {{ return (T)this.Value;}} }}
594+
595+
{access} WitException(T v, uint level) : base(v!, level)
596+
{{
597+
}}
598+
}}
591599
"#,
592600
)
593601
}
@@ -2889,6 +2897,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
28892897
if !self.results.is_empty() {
28902898
self.gen.gen.needs_wit_exception = true;
28912899
let cases = cases.join("\n");
2900+
// r#"}} catch (WitException<{err_ty}> e) {{
28922901
uwriteln!(
28932902
self.src,
28942903
r#"}} catch (WitException e) {{
@@ -2952,10 +2961,9 @@ impl Bindgen for FunctionBindgen<'_, '_> {
29522961
1 => {
29532962
let mut payload_is_void = false;
29542963
let mut previous = operands[0].clone();
2955-
let mut vars = Vec::with_capacity(self.results.len());
2964+
let mut vars: Vec::<(String, Option<String>)> = Vec::with_capacity(self.results.len());
29562965
if let Direction::Import = self.gen.direction {
29572966
for ty in &self.results {
2958-
vars.push(previous.clone());
29592967
let tmp = self.locals.tmp("tmp");
29602968
uwrite!(
29612969
self.src,
@@ -2964,21 +2972,29 @@ impl Bindgen for FunctionBindgen<'_, '_> {
29642972
var {tmp} = {previous}.AsOk;
29652973
"
29662974
);
2967-
previous = tmp;
29682975
let TypeDefKind::Result(result) = &self.gen.resolve.types[*ty].kind else {
29692976
unreachable!();
29702977
};
2978+
let exception_name = result.err
2979+
.map(|ty| self.gen.type_name_with_qualifier(&ty, true));
2980+
vars.push((previous.clone(), exception_name));
29712981
payload_is_void = result.ok.is_none();
2982+
previous = tmp;
29722983
}
29732984
}
29742985
uwriteln!(self.src, "return {};", if payload_is_void { "" } else { &previous });
29752986
for (level, var) in vars.iter().enumerate().rev() {
29762987
self.gen.gen.needs_wit_exception = true;
2988+
let (var_name, exception_name) = var;
2989+
let exception_name = match exception_name {
2990+
Some(type_name) => &format!("WitException<{}>",type_name),
2991+
None => "WitException",
2992+
};
29772993
uwrite!(
29782994
self.src,
29792995
"\
29802996
}} else {{
2981-
throw new WitException({var}.AsErr!, {level});
2997+
throw new {exception_name}({var_name}.AsErr!, {level});
29822998
}}
29832999
"
29843000
);

tests/runtime/results/wasm.cs

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,54 @@
1-
using ResultsWorld.wit.imports.test.results;
2-
31
namespace ResultsWorld.wit.exports.test.results
42
{
53
public class TestImpl : ITest
64
{
75
public static float StringError(float a)
86
{
9-
return ResultsWorld.wit.imports.test.results.TestInterop.StringError(a);
7+
return imports.test.results.TestInterop.StringError(a);
108
}
119

1210
public static float EnumError(float a)
1311
{
1412
try {
15-
return ResultsWorld.wit.imports.test.results.TestInterop.EnumError(a);
16-
} catch (WitException e) {
17-
switch ((ResultsWorld.wit.imports.test.results.ITest.E) e.Value) {
18-
case ResultsWorld.wit.imports.test.results.ITest.E.A:
19-
throw new WitException(ITest.E.A, 0);
20-
case ResultsWorld.wit.imports.test.results.ITest.E.B:
21-
throw new WitException(ITest.E.B, 0);
22-
case ResultsWorld.wit.imports.test.results.ITest.E.C:
23-
throw new WitException(ITest.E.C, 0);
24-
default:
25-
throw new Exception("unreachable");
26-
}
13+
return imports.test.results.TestInterop.EnumError(a);
14+
} catch (WitException<imports.test.results.ITest.E> e) {
15+
throw new WitException(e.TypedValue, 0);
2716
}
2817
}
2918

3019
public static float RecordError(float a)
3120
{
3221
try {
33-
return ResultsWorld.wit.imports.test.results.TestInterop.RecordError(a);
34-
} catch (WitException e) {
35-
var value = (ResultsWorld.wit.imports.test.results.ITest.E2) e.Value;
36-
throw new WitException(new ITest.E2(value.line, value.column), 0);
22+
return imports.test.results.TestInterop.RecordError(a);
23+
} catch (WitException<imports.test.results.ITest.E2> e) {
24+
throw new WitException(new ITest.E2(e.TypedValue.line, e.TypedValue.column), 0);
3725
}
3826
}
3927

4028
public static float VariantError(float a)
4129
{
4230
try {
43-
return ResultsWorld.wit.imports.test.results.TestInterop.VariantError(a);
44-
} catch (WitException e) {
45-
var value = (ResultsWorld.wit.imports.test.results.ITest.E3) e.Value;
46-
switch (value.Tag) {
47-
case ResultsWorld.wit.imports.test.results.ITest.E3.Tags.E1:
48-
switch (value.AsE1) {
49-
case ResultsWorld.wit.imports.test.results.ITest.E.A:
50-
throw new WitException(ITest.E3.E1(ITest.E.A), 0);
51-
case ResultsWorld.wit.imports.test.results.ITest.E.B:
52-
throw new WitException(ITest.E3.E1(ITest.E.B), 0);
53-
case ResultsWorld.wit.imports.test.results.ITest.E.C:
54-
throw new WitException(ITest.E3.E1(ITest.E.C), 0);
55-
default:
56-
throw new Exception("unreachable");
57-
}
58-
case ResultsWorld.wit.imports.test.results.ITest.E3.Tags.E2: {
59-
throw new WitException(ITest.E3.E2(new ITest.E2(value.AsE2.line, value.AsE2.column)), 0);
60-
}
61-
default:
62-
throw new Exception("unreachable");
63-
}
31+
return imports.test.results.TestInterop.VariantError(a);
32+
} catch (WitException<imports.test.results.ITest.E3> e)
33+
when (e.TypedValue.Tag == imports.test.results.ITest.E3.Tags.E1) {
34+
throw new WitException(ITest.E3.E1((ITest.E)Enum.Parse(typeof(ITest.E), e.TypedValue.AsE1.ToString())), 0);
35+
} catch (WitException<imports.test.results.ITest.E3> e)
36+
when (e.TypedValue.Tag == imports.test.results.ITest.E3.Tags.E2) {
37+
throw new WitException(ITest.E3.E2(new ITest.E2(e.TypedValue.AsE2.line, e.TypedValue.AsE2.column)), 0);
38+
}
39+
catch {
40+
throw new Exception("unreachable");
6441
}
6542
}
6643

6744
public static uint EmptyError(uint a)
6845
{
69-
return ResultsWorld.wit.imports.test.results.TestInterop.EmptyError(a);
46+
return imports.test.results.TestInterop.EmptyError(a);
7047
}
7148

7249
public static void DoubleError(uint a)
7350
{
74-
ResultsWorld.wit.imports.test.results.TestInterop.DoubleError(a);
51+
imports.test.results.TestInterop.DoubleError(a);
7552
}
7653
}
7754
}

0 commit comments

Comments
 (0)