From a166ddb6855e0c18f0d4426de436e1d21281d75e Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Sat, 13 Jul 2024 00:19:14 +0200 Subject: [PATCH] fully working symmetric C++ codegen --- crates/cpp/src/lib.rs | 32 +++++----- .../meshless_resources/component_b/b.cpp | 62 +++++++------------ 2 files changed, 37 insertions(+), 57 deletions(-) diff --git a/crates/cpp/src/lib.rs b/crates/cpp/src/lib.rs index 96e902906..7147f1a55 100644 --- a/crates/cpp/src/lib.rs +++ b/crates/cpp/src/lib.rs @@ -1501,11 +1501,7 @@ impl CppInterfaceGenerator<'_> { let name = self.declare_import( &module_name, &func.name, - &[if self.gen.opts.symmetric { - WasmType::Pointer - } else { - WasmType::I32 - }], + &[WasmType::I32], &[], ); uwriteln!( @@ -1520,20 +1516,21 @@ impl CppInterfaceGenerator<'_> { let module_name = self.wasm_import_module.as_ref().map(|e| e.clone()).unwrap(); if matches!(variant, AbiVariant::GuestExport) { - let namespace = class_namespace(self, func, variant); + let mut namespace = class_namespace(self, func, variant); self.gen.c_src.qualify(&namespace); self.gen.c_src.src.push_str("Dtor(("); + let classname = namespace.pop().unwrap_or_default(); self.gen.c_src.qualify(&namespace); - uwriteln!(self.gen.c_src.src, "*){});", func.params.get(0).unwrap().0); + uwriteln!( + self.gen.c_src.src, + "{classname}*){});", + func.params.get(0).unwrap().0 + ); } else { let name = self.declare_import( &module_name, &func.name, - &[if self.gen.opts.symmetric { - WasmType::Pointer - } else { - WasmType::I32 - }], + &[WasmType::Pointer], &[], ); uwriteln!( @@ -1564,8 +1561,9 @@ impl CppInterfaceGenerator<'_> { if self.gen.opts.symmetric { uwriteln!( self.gen.c_src.src, - "{0}::ResourceDrop(({0}*)arg0);", - classname + "{}::ResourceDrop(({})arg0);", + classname, + self.gen.opts.ptr_type() ); } else { uwriteln!(self.gen.c_src.src, "(({classname}*)arg0)->handle=-1;"); @@ -2912,9 +2910,11 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> { ); uwriteln!( self.src, - "auto {var} = {tname}::Owned({tname}::ResourceRep({op})); - {var}->into_handle();" + "auto {var} = {tname}::Owned({tname}::ResourceRep({op}));" ); + if !self.gen.gen.opts.symmetric { + uwriteln!(self.src, "{var}->into_handle();"); + } results.push(format!("std::move({var})")) } }, diff --git a/crates/cpp/tests/meshless_resources/component_b/b.cpp b/crates/cpp/tests/meshless_resources/component_b/b.cpp index 983506576..501cd2ece 100644 --- a/crates/cpp/tests/meshless_resources/component_b/b.cpp +++ b/crates/cpp/tests/meshless_resources/component_b/b.cpp @@ -25,65 +25,45 @@ cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) { return ret; } -//static wit::ResourceTable r_table; -//template std::map wit::ResourceTable::resources; - -// extern "C" __attribute__((import_module("[export]foo:foo/resources"))) -// __attribute__((import_name("[resource-new]r"))) int32_t -// X5BexportX5DfooX3AfooX2FresourcesX00X5Bresource_newX5Dr(uint8_t *); -// extern "C" __attribute__((import_module("[export]foo:foo/resources"))) -// __attribute__((import_name("[resource-rep]r"))) -// uint8_t *X5BexportX5DfooX3AfooX2FresourcesX00X5Bresource_repX5Dr(int32_t); -// extern "C" __attribute__((import_module("[export]foo:foo/resources"))) -// __attribute__((import_name("[resource-drop]r"))) void -// X5BexportX5DfooX3AfooX2FresourcesX00X5Bresource_dropX5Dr(int32_t); -// extern "C" __attribute__((__export_name__("foo:foo/resources#[dtor]r"))) void -// fooX3AfooX2FresourcesX23X5BdtorX5Dr(uint8_t *arg0) { -// ((exports::foo::foo::resources::R *)arg0)->handle = -1; -// exports::foo::foo::resources::R::Dtor( -// (exports::foo::foo::resources::R *)arg0); -// } extern "C" - uint8_t* - fooX3AfooX2FresourcesX00X5BconstructorX5Dr(int32_t arg0) { + __attribute__((__export_name__("foo:foo/resources#[resource_drop]r"))) void + fooX3AfooX2FresourcesX00X5Bresource_dropX5Dr(uint8_t *arg0) { + exports::foo::foo::resources::R::ResourceDrop((uint8_t *)arg0); +} +extern "C" __attribute__((__export_name__("foo:foo/resources#[constructor]r"))) +uint8_t * +fooX3AfooX2FresourcesX00X5BconstructorX5Dr(int32_t arg0) { auto result0 = exports::foo::foo::resources::R::New((uint32_t(arg0))); return result0.release()->handle; } extern "C" - void - fooX3AfooX2FresourcesX00X5BmethodX5DrX2Eadd(uint8_t* arg0, int32_t arg1) { - exports::foo::foo::resources::R::ResourceRep(arg0) - ->Add((uint32_t(arg1))); + __attribute__((__export_name__("foo:foo/resources#[method]r.add"))) void + fooX3AfooX2FresourcesX00X5BmethodX5DrX2Eadd(uint8_t *arg0, int32_t arg1) { + (std::ref(*(exports::foo::foo::resources::R *)arg0)) + .get() + .Add((uint32_t(arg1))); } -uint8_t* exports::foo::foo::resources::R::ResourceNew(R *self) { - return (uint8_t*)self; - //wit::ResourceTable::store_resource(std::move(self)); +uint8_t *exports::foo::foo::resources::R::ResourceNew(R *self) { + return (uint8_t *)self; } exports::foo::foo::resources::R * -exports::foo::foo::resources::R::ResourceRep(uint8_t* id) { +exports::foo::foo::resources::R::ResourceRep(uint8_t *id) { return (exports::foo::foo::resources::R *)id; -// *wit::ResourceTable::lookup_resource(id); } -void exports::foo::foo::resources::R::ResourceDrop(uint8_t* id) { - //auto obj = wit::ResourceTable::remove_resource(id); - //assert(obj.has_value()); - exports::foo::foo::resources::R::Dtor((exports::foo::foo::resources::R*)id); +void exports::foo::foo::resources::R::ResourceDrop(uint8_t *id) { + exports::foo::foo::resources::R::Dtor((exports::foo::foo::resources::R *)id); } -extern "C" uint8_t* +extern "C" __attribute__((__export_name__("foo:foo/resources#create"))) +uint8_t * fooX3AfooX2FresourcesX00create() { auto result0 = exports::foo::foo::resources::Create(); return result0.release()->handle; } -extern "C" void -fooX3AfooX2FresourcesX00consume(uint8_t* arg0) { +extern "C" __attribute__((__export_name__("foo:foo/resources#consume"))) void +fooX3AfooX2FresourcesX00consume(uint8_t *arg0) { auto obj0 = exports::foo::foo::resources::R::Owned( exports::foo::foo::resources::R::ResourceRep(arg0)); - //obj0->into_handle(); exports::foo::foo::resources::Consume(std::move(obj0)); } -extern "C" void -fooX3AfooX2FresourcesX00X5Bresource_dropX5Dr(uint8_t* arg0) { - exports::foo::foo::resources::R::ResourceDrop(arg0); -} // Component Adapters