Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: non-constructible resource error #412

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions crates/js-component-bindgen/src/transpile_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 does not define a constructor');
}}
}}"
);
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
Expand All @@ -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());
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Loading