Skip to content

Commit

Permalink
generate correct new method
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed May 4, 2024
1 parent a949b10 commit 1358302
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
24 changes: 17 additions & 7 deletions crates/cpp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ impl CppInterfaceGenerator<'_> {
}
SpecialMethod::Allocate => WasmSignature {
params: vec![],
results: vec![WasmType::Pointer],
results: vec![],
indirect_params: false,
retptr: false,
},
Expand Down Expand Up @@ -1052,9 +1052,17 @@ impl CppInterfaceGenerator<'_> {
}
}
wit_bindgen_core::wit_parser::Results::Anon(ty) => {
res.result = self.type_name(ty, from_namespace, Flavor::Result(abi_variant));
if matches!(is_drop, SpecialMethod::Allocate | SpecialMethod::ResourceRep) {
res.result.push('*');
if matches!(is_drop, SpecialMethod::Allocate) {
res.result = OWNED_CLASS_NAME.into();
} else {
res.result =
self.type_name(ty, from_namespace, Flavor::Result(abi_variant));
if matches!(
is_drop,
SpecialMethod::Allocate | SpecialMethod::ResourceRep
) {
res.result.push('*');
}
}
}
}
Expand Down Expand Up @@ -1149,7 +1157,7 @@ impl CppInterfaceGenerator<'_> {
uwrite!(
self.gen.h_src.src,
"{{\
return Owned(new {}({}));\
return {OWNED_CLASS_NAME}(new {}({}));\
}}",
cpp_sig.namespace.last().unwrap(), //join("::"),
cpp_sig
Expand Down Expand Up @@ -1183,7 +1191,9 @@ impl CppInterfaceGenerator<'_> {
if !import
&& !matches!(
is_special,
SpecialMethod::ResourceDrop | SpecialMethod::ResourceNew | SpecialMethod::ResourceRep
SpecialMethod::ResourceDrop
| SpecialMethod::ResourceNew
| SpecialMethod::ResourceRep
)
{
self.print_export_signature(func, variant)
Expand Down Expand Up @@ -1594,7 +1604,7 @@ impl CppInterfaceGenerator<'_> {
| (false, Flavor::Result(AbiVariant::GuestExport))
| (true, Flavor::Argument(AbiVariant::GuestImport))
| (true, Flavor::Result(AbiVariant::GuestImport)) => {
typename.push_str("::Owned")
typename.push_str(&format!("::{OWNED_CLASS_NAME}"))
}
(false, Flavor::Result(AbiVariant::GuestImport))
| (true, Flavor::Result(AbiVariant::GuestExport)) => (),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class R : public wit::ResourceExportBase<R> {
static Owned New(uint32_t a) { return Owned(new R(a)); }
void Add(uint32_t b) { value += b; }
static int32_t ResourceNew(R *self);
static void ResourceDrop(int32_t id);
static R* ResourceRep(int32_t id);
static void ResourceDrop(int32_t id);

uint32_t GetValue() const { return value; }
};
Expand Down

0 comments on commit 1358302

Please sign in to comment.