Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
65 changes: 48 additions & 17 deletions crates/cpp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,13 +591,14 @@ impl WorldGenerator for Cpp {

fn import_types(
&mut self,
_resolve: &Resolve,
resolve: &Resolve,
_world: WorldId,
types: &[(&str, TypeId)],
_files: &mut Files,
) {
for i in types.iter() {
uwriteln!(self.h_src.src, "// import_type {}", i.0);
let mut gen = self.interface(resolve, None, true, Some("$root".to_string()));
for (name, id) in types.iter() {
gen.define_type(name, *id);
}
}

Expand Down Expand Up @@ -973,6 +974,8 @@ impl CppInterfaceGenerator<'_> {
Some(ref module_name) => make_external_symbol(module_name, &func_name, symbol_variant),
None => make_external_component(&func_name),
};
// Add prefix to C ABI export functions to avoid conflicts with C++ namespaces
self.gen.c_src.src.push_str("__wasm_export_");
if let Some(prefix) = self.gen.opts.export_prefix.as_ref() {
self.gen.c_src.src.push_str(prefix);
}
Expand Down Expand Up @@ -1636,7 +1639,8 @@ impl CppInterfaceGenerator<'_> {
result: &str,
variant: AbiVariant,
) -> (String, String) {
let extern_name = make_external_symbol(module_name, name, variant);
let mut extern_name = String::from("__wasm_import_");
extern_name.push_str(&make_external_symbol(module_name, name, variant));
let import = format!("extern \"C\" __attribute__((import_module(\"{module_name}\")))\n __attribute__((import_name(\"{name}\")))\n {result} {extern_name}({args});\n")
;
(extern_name, import)
Expand Down Expand Up @@ -2502,14 +2506,25 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
results.push(result);
}
abi::Instruction::HandleLower {
handle: Handle::Own(_ty),
handle: Handle::Own(ty),
..
} => {
let op = &operands[0];
if matches!(self.variant, AbiVariant::GuestImport) {
results.push(format!("{op}.into_handle()"));
} else {

// Check if this is an imported or exported resource
let resource_ty = &self.gen.resolve.types[*ty];
let resource_ty = match &resource_ty.kind {
TypeDefKind::Type(Type::Id(id)) => &self.gen.resolve.types[*id],
_ => resource_ty,
};
let is_exported = self.gen.is_exported_type(resource_ty);

if is_exported {
// Exported resources use .release()->handle
results.push(format!("{op}.release()->handle"));
} else {
// Imported resources use .into_handle()
results.push(format!("{op}.into_handle()"));
}
}
abi::Instruction::HandleLower {
Expand Down Expand Up @@ -2633,7 +2648,13 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
.join(", "),
);
self.src.push_str(">(");
self.src.push_str(&operands.join(", "));
self.src.push_str(
&operands
.iter()
.map(|op| move_if_necessary(op))
.collect::<Vec<_>>()
.join(", "),
);
self.src.push_str(");\n");
results.push(format!("std::move({name})"));
}
Expand Down Expand Up @@ -2715,14 +2736,12 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
variant.cases.iter().zip(blocks).zip(payloads).enumerate()
{
uwriteln!(self.src, "case {}: {{", i);
if let Some(ty) = case.ty.as_ref() {
let ty = self.gen.type_name(ty, &self.namespace, Flavor::InStruct);
if case.ty.is_some() {
let case =
format!("{elem_ns}::{}", to_c_ident(&case.name).to_pascal_case());
uwriteln!(
self.src,
"{} &{} = std::get<{case}>({}.variants).value;",
ty,
"auto& {} = std::get<{case}>({}.variants).value;",
payload,
operands[0],
);
Expand Down Expand Up @@ -2998,7 +3017,13 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
}
self.src.push_str(&func);
self.src.push_str("(");
self.src.push_str(&operands.join(", "));
self.src.push_str(
&operands
.iter()
.map(|op| move_if_necessary(op))
.collect::<Vec<_>>()
.join(", "),
);
self.src.push_str(");\n");
}
abi::Instruction::CallInterface { func, .. } => {
Expand All @@ -3015,7 +3040,13 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
}
self.src.push_str(&func_name_h);
self.push_str("(");
self.push_str(&operands.join(", "));
self.push_str(
&operands
.iter()
.map(|op| move_if_necessary(op))
.collect::<Vec<_>>()
.join(", "),
);
self.push_str(");\n");
if self.needs_dealloc {
uwriteln!(
Expand Down Expand Up @@ -3190,11 +3221,11 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
};
uwriteln!(
self.src,
"{static_var}{tp} ret_area[({size_string}+sizeof({tp})-1)/sizeof({tp})];"
"{static_var}{tp} ret_area{tmp}[({size_string}+sizeof({tp})-1)/sizeof({tp})];"
);
uwriteln!(
self.src,
"{} ptr{tmp} = ({0})(&ret_area);",
"{} ptr{tmp} = ({0})(&ret_area{tmp});",
self.gen.gen.opts.ptr_type(),
);

Expand Down
11 changes: 0 additions & 11 deletions crates/test/src/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ impl LanguageMethods for Cpp {
"async-trait-function.wit"
| "error-context.wit"
| "futures.wit"
| "import_export_func.wit"
| "import-func.wit"
| "issue573.wit"
| "issue929-no-export.wit"
| "lift-lower-foreign.wit"
| "lists.wit"
| "multiversion"
| "resource-alias.wit"
Expand All @@ -63,13 +58,7 @@ impl LanguageMethods for Cpp {
| "resources-in-aggregates.wit"
| "resources-with-futures.wit"
| "resources-with-streams.wit"
| "ret-areas.wit"
| "return-resource-from-export.wit"
| "same-names1.wit"
| "same-names5.wit"
| "variants.wit"
| "variants-unioning-types.wit"
| "worlds-with-types.wit"
| "streams.wit" => true,
_ => false,
}
Expand Down
Loading