Skip to content

Commit 5362b21

Browse files
author
Guy Bedford
authored
gen-guest-c: switch result boolean to indicate success (#452)
1 parent d47b6c5 commit 5362b21

3 files changed

Lines changed: 16 additions & 19 deletions

File tree

crates/gen-guest-c/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ struct CSig {
6262
enum Scalar {
6363
Void,
6464
OptionBool(Type),
65-
Result(bool),
65+
ResultBool(bool),
6666
Type(Type),
6767
}
6868

@@ -410,7 +410,7 @@ impl Return {
410410
if let Some(err) = r.err {
411411
self.retptrs.push(err);
412412
}
413-
self.scalar = Some(Scalar::Result(has_ok_type));
413+
self.scalar = Some(Scalar::ResultBool(has_ok_type));
414414
return;
415415
}
416416

@@ -864,7 +864,7 @@ impl InterfaceGenerator<'_> {
864864
match &ret.scalar {
865865
None | Some(Scalar::Void) => self.src.h_fns("void"),
866866
Some(Scalar::OptionBool(_id)) => self.src.h_fns("bool"),
867-
Some(Scalar::Result(has_ok_type)) => {
867+
Some(Scalar::ResultBool(has_ok_type)) => {
868868
result_rets = true;
869869
result_rets_has_ok_type = *has_ok_type;
870870
self.src.h_fns("bool");
@@ -2168,7 +2168,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
21682168
);
21692169
results.push(option_ret);
21702170
}
2171-
Some(Scalar::Result(has_ok_type)) => {
2171+
Some(Scalar::ResultBool(has_ok_type)) => {
21722172
let result_ty = self
21732173
.gen
21742174
.type_string(func.results.iter_types().next().unwrap());
@@ -2205,7 +2205,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
22052205
};
22062206
assert!(ret_iter.next().is_none());
22072207
uwrite!(self.src, "");
2208-
uwriteln!(self.src, "{ret}.is_err = {}({args});", self.sig.name);
2208+
uwriteln!(self.src, "{ret}.is_err = !{}({args});", self.sig.name);
22092209

22102210
if let Some(err_name) = err_name {
22112211
uwriteln!(
@@ -2253,7 +2253,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
22532253
self.src.push_str(&variant);
22542254
self.src.push_str(".is_some;\n");
22552255
}
2256-
Some(Scalar::Result(has_ok_type)) => {
2256+
Some(Scalar::ResultBool(has_ok_type)) => {
22572257
assert_eq!(operands.len(), 1);
22582258
let variant = &operands[0];
22592259
assert!(self.sig.retptrs.len() <= 2);
@@ -2265,7 +2265,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
22652265
}
22662266
uwriteln!(
22672267
self.src,
2268-
" return 0;
2268+
" return 1;
22692269
}} else {{"
22702270
);
22712271
if self.sig.retptrs.len() == 2 {
@@ -2275,7 +2275,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
22752275
}
22762276
uwriteln!(
22772277
self.src,
2278-
" return 1;
2278+
" return 0;
22792279
}}"
22802280
);
22812281
}

tests/runtime/flavorful/wasm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ void flavorful_test_imports() {
6363

6464
{
6565
imports_my_errno_t errno;
66-
assert(imports_errno_result(&errno));
66+
assert(!imports_errno_result(&errno));
6767
assert(errno == IMPORTS_MY_ERRNO_B);
6868
}
6969

7070
{
7171
imports_my_errno_t errno;
72-
assert(imports_errno_result(&errno) == 0);
72+
assert(imports_errno_result(&errno));
7373
}
7474

7575
{
@@ -185,7 +185,7 @@ bool flavorful_f_list_in_variant3(flavorful_list_in_variant3_t *a, flavorful_str
185185

186186
bool flavorful_errno_result(flavorful_my_errno_t *err) {
187187
*err = FLAVORFUL_MY_ERRNO_B;
188-
return true;
188+
return false;
189189
}
190190

191191
void flavorful_list_typedefs(flavorful_list_typedef_t *a, flavorful_list_typedef3_t *c, flavorful_list_typedef2_t *ret0, flavorful_list_typedef3_t *ret1) {

tests/runtime/variants/wasm.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,16 @@ void variants_test_imports() {
2424

2525
a.is_err = false;
2626
a.val.ok = 2;
27-
bool is_err = imports_roundtrip_result(&a, &b_ok, &b_err);
28-
assert(!is_err);
27+
assert(imports_roundtrip_result(&a, &b_ok, &b_err));
2928
assert(b_ok == 2.0);
3029

3130
a.val.ok = 4;
32-
is_err = imports_roundtrip_result(&a, &b_ok, &b_err);
33-
assert(!is_err);
31+
assert(imports_roundtrip_result(&a, &b_ok, &b_err));
3432
assert(b_ok == 4);
3533

3634
a.is_err = true;
3735
a.val.err = 5.3;
38-
is_err = imports_roundtrip_result(&a, &b_ok, &b_err);
39-
assert(is_err);
36+
assert(!imports_roundtrip_result(&a, &b_ok, &b_err));
4037
assert(b_err == 5);
4138
}
4239

@@ -152,10 +149,10 @@ bool variants_roundtrip_option(variants_option_float32_t *a, uint8_t *ret0) {
152149
bool variants_roundtrip_result(variants_result_u32_float32_t *a, double *ok, uint8_t *err) {
153150
if (a->is_err) {
154151
*err = a->val.err;
155-
return true;
152+
return false;
156153
} else {
157154
*ok = a->val.ok;
158-
return false;
155+
return true;
159156
}
160157
}
161158

0 commit comments

Comments
 (0)