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

fix: top-level resource bindgen #526

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions crates/js-component-bindgen/src/function_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ impl Bindgen for FunctionBindgen<'_> {
uwriteln!(
self.src,
"for (const rsc of {cur_resource_borrows}) {{
rsc[{symbol_resource_handle}] = null;
rsc[{symbol_resource_handle}] = undefined;
}}
{cur_resource_borrows} = [];"
);
Expand All @@ -1141,7 +1141,7 @@ impl Bindgen for FunctionBindgen<'_> {
"for (const {{ rsc, drop }} of {cur_resource_borrows}) {{
if (rsc[{symbol_resource_handle}]) {{
drop(rsc[{symbol_resource_handle}]);
delete rsc[{symbol_resource_handle}];
rsc[{symbol_resource_handle}] = undefined;
}}
}}
{cur_resource_borrows} = [];"
Expand Down Expand Up @@ -1263,7 +1263,7 @@ impl Bindgen for FunctionBindgen<'_> {
finalizationRegistry{tid}.unregister({rsc});
{rsc_table_remove}(handleTable{tid}, {handle});
{rsc}[{symbol_dispose}] = {empty_func};
{rsc}[{symbol_resource_handle}] = null;
{rsc}[{symbol_resource_handle}] = undefined;
{dtor}(handleTable{tid}[({handle} << 1) + 1] & ~{rsc_flag});
}}}});"
);
Expand Down Expand Up @@ -1396,7 +1396,7 @@ impl Bindgen for FunctionBindgen<'_> {
}}
finalizationRegistry{tid}.unregister({op});
{op}[{symbol_dispose}] = {empty_func};
{op}[{symbol_resource_handle}] = null;",
{op}[{symbol_resource_handle}] = undefined;",
);
} else {
// When expecting a borrow, the JS resource provided will always be an own
Expand Down
38 changes: 20 additions & 18 deletions crates/js-component-bindgen/src/transpile_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,24 +1095,26 @@ impl<'a> Instantiator<'a, '_> {
WorldItem::Type(_) => unreachable!(),
};

let resource_prefix = if iface_name.is_some() {
None
} else {
match func.kind {
FunctionKind::Method(_) => Some("[method]"),
FunctionKind::Static(_) => Some("[static]"),
FunctionKind::Constructor(_) => Some("[constructor]"),
FunctionKind::Freestanding => None,
}
};

// nested interfaces only currently possible through mapping
let (import_specifier, maybe_iface_member) = map_import(
&self.gen.opts.map,
if resource_prefix.is_some() {
import_name.strip_prefix(resource_prefix.unwrap()).unwrap()
} else {
if iface_name.is_some() {
import_name
} else {
match func.kind {
FunctionKind::Method(_) => {
let stripped = import_name.strip_prefix("[method]").unwrap();
&stripped[0..stripped.find(".").unwrap()]
}
FunctionKind::Static(_) => {
let stripped = import_name.strip_prefix("[static]").unwrap();
&stripped[0..stripped.find(".").unwrap()]
}
FunctionKind::Constructor(_) => {
import_name.strip_prefix("[constructor]").unwrap()
}
FunctionKind::Freestanding => import_name,
}
},
);

Expand Down Expand Up @@ -1341,14 +1343,14 @@ impl<'a> Instantiator<'a, '_> {
local_name,
);
}
} else if let Some(import_binding) = import_binding {
self.gen
.esm_bindgen
.add_import_binding(&[import_specifier, import_binding], local_name);
} else if let Some(iface_member) = iface_member {
self.gen
.esm_bindgen
.add_import_binding(&[import_specifier, iface_member.into()], local_name);
} else if let Some(import_binding) = import_binding {
self.gen
.esm_bindgen
.add_import_binding(&[import_specifier, import_binding], local_name);
} else {
self.gen
.esm_bindgen
Expand Down
Loading