Skip to content

Commit

Permalink
fully working symmetric C++ codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed Jul 12, 2024
1 parent dae55ed commit a166ddb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 57 deletions.
32 changes: 16 additions & 16 deletions crates/cpp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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!(
Expand Down Expand Up @@ -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;");
Expand Down Expand Up @@ -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})"))
}
},
Expand Down
62 changes: 21 additions & 41 deletions crates/cpp/tests/meshless_resources/component_b/b.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,65 +25,45 @@ cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) {
return ret;
}

//static wit::ResourceTable<exports::foo::foo::resources::R*> r_table;
//template <class R> std::map<int32_t, R> wit::ResourceTable<R>::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<exports::foo::foo::resources::R*>::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<exports::foo::foo::resources::R*>::lookup_resource(id);
}
void exports::foo::foo::resources::R::ResourceDrop(uint8_t* id) {
//auto obj = wit::ResourceTable<exports::foo::foo::resources::R*>::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

0 comments on commit a166ddb

Please sign in to comment.