Skip to content

Commit f797096

Browse files
committed
fix(cpp): support worlds with types
Adds __wasm_export_ prefix to all C ABI export function names When a WIT world exports a function whose name matches the world/package name (e.g., world "foo" with export function "foo"), the generated C ABI function name foo() conflicted with the C++ namespace namespace foo. This generates functions like: ```c extern "C" __attribute__((__export_name__("foo"))) uint8_t * __wasm_export_foo() // C++ function name ``` The public C++ API is `exports::foo::Foo()`. Users never see the __wasm_export_foo() function in the bindings. Signed-off-by: Bailey Hayes <behayes2@gmail.com>
1 parent 6c6cd43 commit f797096

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

crates/cpp/src/lib.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,13 +591,14 @@ impl WorldGenerator for Cpp {
591591

592592
fn import_types(
593593
&mut self,
594-
_resolve: &Resolve,
594+
resolve: &Resolve,
595595
_world: WorldId,
596596
types: &[(&str, TypeId)],
597597
_files: &mut Files,
598598
) {
599-
for i in types.iter() {
600-
uwriteln!(self.h_src.src, "// import_type {}", i.0);
599+
let mut gen = self.interface(resolve, None, true, Some("$root".to_string()));
600+
for (name, id) in types.iter() {
601+
gen.define_type(name, *id);
601602
}
602603
}
603604

@@ -973,6 +974,8 @@ impl CppInterfaceGenerator<'_> {
973974
Some(ref module_name) => make_external_symbol(module_name, &func_name, symbol_variant),
974975
None => make_external_component(&func_name),
975976
};
977+
// Add prefix to C ABI export functions to avoid conflicts with C++ namespaces
978+
self.gen.c_src.src.push_str("__wasm_export_");
976979
if let Some(prefix) = self.gen.opts.export_prefix.as_ref() {
977980
self.gen.c_src.src.push_str(prefix);
978981
}
@@ -2633,7 +2636,13 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
26332636
.join(", "),
26342637
);
26352638
self.src.push_str(">(");
2636-
self.src.push_str(&operands.join(", "));
2639+
self.src.push_str(
2640+
&operands
2641+
.iter()
2642+
.map(|op| move_if_necessary(op))
2643+
.collect::<Vec<_>>()
2644+
.join(", "),
2645+
);
26372646
self.src.push_str(");\n");
26382647
results.push(format!("std::move({name})"));
26392648
}
@@ -2998,7 +3007,13 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
29983007
}
29993008
self.src.push_str(&func);
30003009
self.src.push_str("(");
3001-
self.src.push_str(&operands.join(", "));
3010+
self.src.push_str(
3011+
&operands
3012+
.iter()
3013+
.map(|op| move_if_necessary(op))
3014+
.collect::<Vec<_>>()
3015+
.join(", "),
3016+
);
30023017
self.src.push_str(");\n");
30033018
}
30043019
abi::Instruction::CallInterface { func, .. } => {
@@ -3015,7 +3030,13 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
30153030
}
30163031
self.src.push_str(&func_name_h);
30173032
self.push_str("(");
3018-
self.push_str(&operands.join(", "));
3033+
self.push_str(
3034+
&operands
3035+
.iter()
3036+
.map(|op| move_if_necessary(op))
3037+
.collect::<Vec<_>>()
3038+
.join(", "),
3039+
);
30193040
self.push_str(");\n");
30203041
if self.needs_dealloc {
30213042
uwriteln!(

crates/test/src/cpp.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ impl LanguageMethods for Cpp {
6666
| "ret-areas.wit"
6767
| "return-resource-from-export.wit"
6868
| "same-names1.wit"
69-
| "same-names5.wit"
70-
| "variants.wit"
71-
| "variants-unioning-types.wit"
72-
| "worlds-with-types.wit"
7369
| "streams.wit" => true,
7470
_ => false,
7571
}

0 commit comments

Comments
 (0)