From d2fc3bb66cd662dde412e75f0e4d664c6e2b4f9e Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Fri, 22 Mar 2024 14:29:09 -0700 Subject: [PATCH 1/2] feat: non-constructible resource error --- .../src/transpile_bindgen.rs | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/crates/js-component-bindgen/src/transpile_bindgen.rs b/crates/js-component-bindgen/src/transpile_bindgen.rs index feee5e25a..5c38e12ff 100644 --- a/crates/js-component-bindgen/src/transpile_bindgen.rs +++ b/crates/js-component-bindgen/src/transpile_bindgen.rs @@ -548,6 +548,20 @@ impl<'a> Instantiator<'a, '_> { self.exports(&self.component.exports); } + fn ensure_local_resource_class(&mut self, local_name: String) { + if !self.defined_resource_classes.contains(&local_name) { + uwriteln!( + self.src.js, + "\nclass {local_name} {{ + constructor () {{ + throw new Error('\"{local_name}\" resource cannot be explicitly constructed'); + }} + }}" + ); + self.defined_resource_classes.insert(local_name.to_string()); + } + } + fn resource_definitions(&mut self) { // It is theoretically possible for locally defined resources used in no functions // to still be exported @@ -558,10 +572,7 @@ impl<'a> Instantiator<'a, '_> { continue; } if let Some(local_name) = self.gen.local_names.try_get(resource) { - if !self.defined_resource_classes.contains(local_name) { - uwriteln!(self.src.js, "\nclass {local_name} {{}}"); - self.defined_resource_classes.insert(local_name.to_string()); - } + self.ensure_local_resource_class(local_name.to_string()); } } @@ -1800,10 +1811,7 @@ impl<'a> Instantiator<'a, '_> { match func.kind { FunctionKind::Freestanding => uwrite!(self.src.js, "\nfunction {local_name}"), FunctionKind::Method(_) => { - if !self.defined_resource_classes.contains(local_name) { - uwriteln!(self.src.js, "\nclass {local_name} {{}}"); - self.defined_resource_classes.insert(local_name.to_string()); - } + self.ensure_local_resource_class(local_name.to_string()); let method_name = func.item_name().to_lower_camel_case(); uwrite!( self.src.js, @@ -1816,10 +1824,7 @@ impl<'a> Instantiator<'a, '_> { ); } FunctionKind::Static(_) => { - if !self.defined_resource_classes.contains(local_name) { - uwriteln!(self.src.js, "\nclass {local_name} {{}}"); - self.defined_resource_classes.insert(local_name.to_string()); - } + self.ensure_local_resource_class(local_name.to_string()); let method_name = func.item_name().to_lower_camel_case(); uwrite!( self.src.js, From 0242c6d168e73abde7b7f759d702b437ed622f74 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Fri, 22 Mar 2024 14:30:06 -0700 Subject: [PATCH 2/2] fixup err msg --- crates/js-component-bindgen/src/transpile_bindgen.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/js-component-bindgen/src/transpile_bindgen.rs b/crates/js-component-bindgen/src/transpile_bindgen.rs index 5c38e12ff..97d946acf 100644 --- a/crates/js-component-bindgen/src/transpile_bindgen.rs +++ b/crates/js-component-bindgen/src/transpile_bindgen.rs @@ -554,7 +554,7 @@ impl<'a> Instantiator<'a, '_> { self.src.js, "\nclass {local_name} {{ constructor () {{ - throw new Error('\"{local_name}\" resource cannot be explicitly constructed'); + throw new Error('\"{local_name}\" resource does not define a constructor'); }} }}" );