From 1358302f8049d7fc5160d4b5891965d5003e0f74 Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Sat, 4 May 2024 22:58:48 +0200 Subject: [PATCH] generate correct new method --- crates/cpp/src/lib.rs | 24 +++++++++++++------ .../guest/exports-foo-foo-resources-R.h | 2 +- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/crates/cpp/src/lib.rs b/crates/cpp/src/lib.rs index e4e58dbc4..1d4f1b41a 100644 --- a/crates/cpp/src/lib.rs +++ b/crates/cpp/src/lib.rs @@ -917,7 +917,7 @@ impl CppInterfaceGenerator<'_> { } SpecialMethod::Allocate => WasmSignature { params: vec![], - results: vec![WasmType::Pointer], + results: vec![], indirect_params: false, retptr: false, }, @@ -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('*'); + } } } } @@ -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 @@ -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) @@ -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)) => (), diff --git a/crates/cpp/tests/native_resources/guest/exports-foo-foo-resources-R.h b/crates/cpp/tests/native_resources/guest/exports-foo-foo-resources-R.h index 438782772..ce8d45fd0 100644 --- a/crates/cpp/tests/native_resources/guest/exports-foo-foo-resources-R.h +++ b/crates/cpp/tests/native_resources/guest/exports-foo-foo-resources-R.h @@ -16,8 +16,8 @@ class R : public wit::ResourceExportBase { 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; } };