Skip to content

Commit f038086

Browse files
committed
feat(cpp): handle multiple return areas
Signed-off-by: Bailey Hayes <behayes2@gmail.com>
1 parent f797096 commit f038086

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

crates/cpp/src/lib.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,14 +2505,25 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
25052505
results.push(result);
25062506
}
25072507
abi::Instruction::HandleLower {
2508-
handle: Handle::Own(_ty),
2508+
handle: Handle::Own(ty),
25092509
..
25102510
} => {
25112511
let op = &operands[0];
2512-
if matches!(self.variant, AbiVariant::GuestImport) {
2513-
results.push(format!("{op}.into_handle()"));
2514-
} else {
2512+
2513+
// Check if this is an imported or exported resource
2514+
let resource_ty = &self.gen.resolve.types[*ty];
2515+
let resource_ty = match &resource_ty.kind {
2516+
TypeDefKind::Type(Type::Id(id)) => &self.gen.resolve.types[*id],
2517+
_ => resource_ty,
2518+
};
2519+
let is_exported = self.gen.is_exported_type(resource_ty);
2520+
2521+
if is_exported {
2522+
// Exported resources use .release()->handle
25152523
results.push(format!("{op}.release()->handle"));
2524+
} else {
2525+
// Imported resources use .into_handle()
2526+
results.push(format!("{op}.into_handle()"));
25162527
}
25172528
}
25182529
abi::Instruction::HandleLower {
@@ -2724,14 +2735,12 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
27242735
variant.cases.iter().zip(blocks).zip(payloads).enumerate()
27252736
{
27262737
uwriteln!(self.src, "case {}: {{", i);
2727-
if let Some(ty) = case.ty.as_ref() {
2728-
let ty = self.gen.type_name(ty, &self.namespace, Flavor::InStruct);
2738+
if case.ty.is_some() {
27292739
let case =
27302740
format!("{elem_ns}::{}", to_c_ident(&case.name).to_pascal_case());
27312741
uwriteln!(
27322742
self.src,
2733-
"{} &{} = std::get<{case}>({}.variants).value;",
2734-
ty,
2743+
"auto& {} = std::get<{case}>({}.variants).value;",
27352744
payload,
27362745
operands[0],
27372746
);
@@ -3211,11 +3220,11 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
32113220
};
32123221
uwriteln!(
32133222
self.src,
3214-
"{static_var}{tp} ret_area[({size_string}+sizeof({tp})-1)/sizeof({tp})];"
3223+
"{static_var}{tp} ret_area{tmp}[({size_string}+sizeof({tp})-1)/sizeof({tp})];"
32153224
);
32163225
uwriteln!(
32173226
self.src,
3218-
"{} ptr{tmp} = ({0})(&ret_area);",
3227+
"{} ptr{tmp} = ({0})(&ret_area{tmp});",
32193228
self.gen.gen.opts.ptr_type(),
32203229
);
32213230

crates/test/src/cpp.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ impl LanguageMethods for Cpp {
5151
| "futures.wit"
5252
| "import_export_func.wit"
5353
| "import-func.wit"
54-
| "issue573.wit"
55-
| "issue929-no-export.wit"
56-
| "lift-lower-foreign.wit"
5754
| "lists.wit"
5855
| "multiversion"
5956
| "resource-alias.wit"
@@ -63,7 +60,6 @@ impl LanguageMethods for Cpp {
6360
| "resources-in-aggregates.wit"
6461
| "resources-with-futures.wit"
6562
| "resources-with-streams.wit"
66-
| "ret-areas.wit"
6763
| "return-resource-from-export.wit"
6864
| "same-names1.wit"
6965
| "streams.wit" => true,

0 commit comments

Comments
 (0)