From 53452d4da62337b91735ab0d51ea6437ae94d7e8 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 19 Aug 2024 20:03:18 -0700 Subject: [PATCH 1/3] fix: post-return on error return bindgen --- .../src/function_bindgen.rs | 53 ++++++++++--------- .../src/transpile_bindgen.rs | 7 --- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/crates/js-component-bindgen/src/function_bindgen.rs b/crates/js-component-bindgen/src/function_bindgen.rs index 7bd5bef29..4620c8410 100644 --- a/crates/js-component-bindgen/src/function_bindgen.rs +++ b/crates/js-component-bindgen/src/function_bindgen.rs @@ -1153,38 +1153,41 @@ 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 { - "return" - }; - if *amt == 1 { - if self.err == ErrHandling::ThrowResultErr { - let component_err = self.intrinsic(Intrinsic::ComponentError); - let op = &operands[0]; - uwriteln!( - self.src, - "if (typeof {op} === 'object' && {op}.tag === 'err') {{ - throw new {component_err}({op}.val); - }} - {ret_assign} {op}.val;" - ); - } else { - uwriteln!(self.src, "{ret_assign} {};", operands[0]); + 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}.val;"); + if let Some(f) = &self.post_return { + uwriteln!(self.src, "{f}(ret);"); } - } else { - uwriteln!(self.src, "{ret_assign} [{}];", operands.join(", ")); - } - if let Some(f) = &self.post_return { uwriteln!( self.src, - "{f}(ret); + "if (typeof {op} === 'object' && {op}.tag === 'err') {{ + throw new {component_err}(retVal); + }} return retVal;" ); + } else { + 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, + "{f}(ret); + return retVal;" + ); + } } } diff --git a/crates/js-component-bindgen/src/transpile_bindgen.rs b/crates/js-component-bindgen/src/transpile_bindgen.rs index 2171bf86e..2e01fca9e 100644 --- a/crates/js-component-bindgen/src/transpile_bindgen.rs +++ b/crates/js-component-bindgen/src/transpile_bindgen.rs @@ -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, From dde7fd8ba8aad5708b75c493cbf2bd676d34a1f9 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 19 Aug 2024 20:05:13 -0700 Subject: [PATCH 2/3] fmt --- crates/js-component-bindgen/src/function_bindgen.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/js-component-bindgen/src/function_bindgen.rs b/crates/js-component-bindgen/src/function_bindgen.rs index 4620c8410..c13ddb5b1 100644 --- a/crates/js-component-bindgen/src/function_bindgen.rs +++ b/crates/js-component-bindgen/src/function_bindgen.rs @@ -1155,8 +1155,7 @@ impl Bindgen for FunctionBindgen<'_> { if let Some(f) = &self.post_return { uwriteln!(self.src, "{f}();"); } - } - else if *amt == 1 && self.err == ErrHandling::ThrowResultErr { + } 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}.val;"); From 87c9680e0ca6808fdca7f0e796a5c41711cb548a Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 19 Aug 2024 20:06:31 -0700 Subject: [PATCH 3/3] fixup --- crates/js-component-bindgen/src/function_bindgen.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/js-component-bindgen/src/function_bindgen.rs b/crates/js-component-bindgen/src/function_bindgen.rs index c13ddb5b1..738682c83 100644 --- a/crates/js-component-bindgen/src/function_bindgen.rs +++ b/crates/js-component-bindgen/src/function_bindgen.rs @@ -1158,16 +1158,16 @@ impl Bindgen for FunctionBindgen<'_> { } 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}.val;"); + uwriteln!(self.src, "const retVal = {op};"); if let Some(f) = &self.post_return { uwriteln!(self.src, "{f}(ret);"); } uwriteln!( self.src, - "if (typeof {op} === 'object' && {op}.tag === 'err') {{ - throw new {component_err}(retVal); + "if (typeof retVal === 'object' && retVal.tag === 'err') {{ + throw new {component_err}(retVal.val); }} - return retVal;" + return retVal.val;" ); } else { let ret_assign = if self.post_return.is_some() {