Skip to content

Commit

Permalink
fix: post-return on error return bindgen (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Aug 20, 2024
1 parent a30d24d commit b17ade5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
54 changes: 28 additions & 26 deletions crates/js-component-bindgen/src/function_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,38 +1153,40 @@ impl Bindgen for FunctionBindgen<'_> {
Instruction::Return { amt, .. } => {
if *amt == 0 {
if let Some(f) = &self.post_return {
uwriteln!(self.src, "{f}()");
uwriteln!(self.src, "{f}();");
}
return;
}
let ret_assign = if self.post_return.is_some() {
"const retVal ="
} else if *amt == 1 && self.err == ErrHandling::ThrowResultErr {
let component_err = self.intrinsic(Intrinsic::ComponentError);
let op = &operands[0];
uwriteln!(self.src, "const retVal = {op};");
if let Some(f) = &self.post_return {
uwriteln!(self.src, "{f}(ret);");
}
uwriteln!(
self.src,
"if (typeof retVal === 'object' && retVal.tag === 'err') {{
throw new {component_err}(retVal.val);
}}
return retVal.val;"
);
} else {
"return"
};
if *amt == 1 {
if self.err == ErrHandling::ThrowResultErr {
let component_err = self.intrinsic(Intrinsic::ComponentError);
let op = &operands[0];
let ret_assign = if self.post_return.is_some() {
"const retVal ="
} else {
"return"
};
if *amt == 1 {
uwriteln!(self.src, "{ret_assign} {};", operands[0]);
} else {
uwriteln!(self.src, "{ret_assign} [{}];", operands.join(", "));
}
if let Some(f) = &self.post_return {
uwriteln!(
self.src,
"if (typeof {op} === 'object' && {op}.tag === 'err') {{
throw new {component_err}({op}.val);
}}
{ret_assign} {op}.val;"
"{f}(ret);
return retVal;"
);
} else {
uwriteln!(self.src, "{ret_assign} {};", operands[0]);
}
} else {
uwriteln!(self.src, "{ret_assign} [{}];", operands.join(", "));
}
if let Some(f) = &self.post_return {
uwriteln!(
self.src,
"{f}(ret);
return retVal;"
);
}
}

Expand Down
7 changes: 0 additions & 7 deletions crates/js-component-bindgen/src/transpile_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,13 +1307,6 @@ impl<'a> Instantiator<'a, '_> {
// mapping can be used to construct virtual nested namespaces
// which is used eg to support WASI interface groupings
if let Some(iface_member) = iface_member {
// if local_name.starts_with("Poll") {
// dbg!( &[
// &import_specifier,
// &iface_member.to_lower_camel_case(),
// &import_binding.as_ref().unwrap().to_string(),
// ], &local_name);
// }
self.gen.esm_bindgen.add_import_binding(
&[
import_specifier,
Expand Down

0 comments on commit b17ade5

Please sign in to comment.