Skip to content

Commit

Permalink
mesh example
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed May 29, 2024
1 parent 98efeb8 commit 25f5e4c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion crates/cpp/tests/native_mesh/component_a/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CXXFLAGS=-g -O0 -I../../../helper-types
all: component_a

component_a: a.cpp main.cpp
$(CXX) -shared $(CXXFLAGS) -o $@ $^
$(CXX) $(CXXFLAGS) -o $@ $^ libmesh.so libcomponent_b.so

bindgen:
../../../../../target/debug/wit-bindgen cpp ../wit -w a --wasm64 --format
1 change: 1 addition & 0 deletions crates/cpp/tests/native_mesh/component_a/libmesh.so
2 changes: 1 addition & 1 deletion crates/cpp/tests/native_mesh/mesh/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CXXFLAGS=-fPIC -g -O0 -I../../../helper-types

all: libmesh.so

libmesh.so: mesh_native.cpp
libmesh.so: mesh_native.cpp impl.cpp
$(CXX) -shared $(CXXFLAGS) -o $@ $^ libcomponent_b.so

bindgen:
Expand Down
22 changes: 22 additions & 0 deletions crates/cpp/tests/native_mesh/mesh/impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#include "mesh_cpp_native.h"

mesh::foo::foo::resources::R::R(uint32_t a)
: impl(exports::foo::foo::resources::R(a)) {}

mesh::foo::foo::resources::R::R(exports::foo::foo::resources::R && a)
: impl(std::move(a)) {}

void mesh::foo::foo::resources::R::Add(uint32_t b) {
impl.Add(b);
}

mesh::foo::foo::resources::R::Owned
mesh::foo::foo::resources::Create() {
return mesh::foo::foo::resources::R::Owned(new mesh::foo::foo::resources::R
(exports::foo::foo::resources::Create()));
}

void mesh::foo::foo::resources::Consume(mesh::foo::foo::resources::R::Owned obj) {
exports::foo::foo::resources::Consume(obj->into_inner());
}
6 changes: 5 additions & 1 deletion crates/cpp/tests/native_mesh/mesh/mesh_cpp_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ namespace foo {
namespace foo {
namespace resources {
class R : public wit::ResourceImportBase<R> {

exports::foo::foo::resources::R impl;
public:
static void Dtor(R *self) { delete self; }
R(uint32_t a);
R(exports::foo::foo::resources::R && a);
static Owned New(uint32_t a) { return Owned(new R(a)); }
void Add(uint32_t b);
exports::foo::foo::resources::R into_inner() {
return std::move(impl);
}
};

R::Owned Create();
Expand Down
20 changes: 5 additions & 15 deletions crates/cpp/tests/native_mesh/mesh/mesh_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,11 @@
#include "mesh_cpp_native.h"
template <class R> std::map<int32_t, R> wit::ResourceTable<R>::resources;
#include <assert.h>
extern "C" __attribute__((import_module("foo:foo/resources")))
__attribute__((import_name("[dtor]r"))) void
fooX3AfooX2FresourcesX23X5BdtorX5Dr(uint8_t *);
extern "C" __attribute__((import_module("foo:foo/resources")))
__attribute__((import_name("[constructor]r")))
int32_t fooX3AfooX2FresourcesX23X5BconstructorX5Dr(int32_t);
extern "C" __attribute__((import_module("foo:foo/resources")))
__attribute__((import_name("[method]r.add"))) void
fooX3AfooX2FresourcesX23X5BmethodX5DrX2Eadd(uint8_t *, int32_t);
extern "C" __attribute__((import_module("foo:foo/resources")))
__attribute__((import_name("create"))) int32_t
fooX3AfooX2FresourcesX23create();
extern "C" __attribute__((import_module("foo:foo/resources")))
__attribute__((import_name("consume"))) void
fooX3AfooX2FresourcesX23consume(int32_t);
extern "C" void fooX3AfooX2FresourcesX23X5BdtorX5Dr(uint8_t *);
extern "C" int32_t fooX3AfooX2FresourcesX23X5BconstructorX5Dr(int32_t);
extern "C" void fooX3AfooX2FresourcesX23X5BmethodX5DrX2Eadd(uint8_t *, int32_t);
extern "C" int32_t fooX3AfooX2FresourcesX23create();
extern "C" void fooX3AfooX2FresourcesX23consume(int32_t);
extern "C" void fooX3AfooX2FresourcesX00X5Bresource_dropX5Dr(int32_t arg0) {
auto ptr = mesh::foo::foo::resources::R::remove_resource(arg0);
assert(ptr.has_value());
Expand Down

0 comments on commit 25f5e4c

Please sign in to comment.