Skip to content

Commit

Permalink
fix: top-level resource bindgen (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Nov 22, 2024
1 parent 791d1c1 commit da41087
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
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

0 comments on commit da41087

Please sign in to comment.