Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions crates/cpp/tests/native_strings/Makefile
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
CXXFLAGS=-g -O0 -I../../helper-types
CXXFLAGS=-g -O0 -I../../helper-types -I../../cpp_host -I../../guest --std=c++17
WIT_BINDGEN=../../../../target/debug/wit-bindgen

all: libstrings.so app-strings

libstrings.so: the_world.pie.o guest.pie.o
$(CXX) $(CXXFLAGS) -shared -o $@ $^ -Wl,--version-script=guest.lds
libstrings.so:
cd guest && make $@

%.pie.o: %.cpp
$(CXX) $(CXXFLAGS) -fPIE -o $@ -c $^
app-strings: ./cpp_host/the_world_native.o main.o ./cpp_host/guest_imported_fns.o
$(CXX) $(CXXFLAGS) -o $@ $^ -g -L./guest -lstrings

app-strings: the_world_native.o main.o
$(CXX) $(CXXFLAGS) -o $@ $^ -L. -lstrings
the_world_native.o:
cd cpp_host && make $@

bindgen: wit/strings.wit
$(WIT_BINDGEN) cpp wit --wasm64 --format
$(WIT_BINDGEN) cpp wit --wasm64 --format --direct
cd rust/src ; ../../$(WIT_BINDGEN) rust ../../wit --wasm64

guest.wasm: the_world.cpp guest.cpp
guest.wasm: the_world.cpp guest_1.cpp
/opt/wasi-sdk/bin/clang++ -o $@ $^ $(CXXFLAGS)

guest_release.wasm: the_world.cpp guest.cpp
guest_release.wasm: the_world.cpp guest_1.cpp
/opt/wasi-sdk/bin/clang++ -o $@ $^ $(CXXFLAGS) -g0 -O3

clean:
-rm *.o libstrings.so app-strings
-rm *.o app-strings
cd guest && make clean
cd cpp_host && make clean

run:
LD_LIBRARY_PATH=. ./app-strings
Expand Down
25 changes: 25 additions & 0 deletions crates/cpp/tests/native_strings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Native string example

In this example, there is a guest, a native host (cpp_host) and an application.
The `guest + native host` together form the actual `guest` code, which exports some APIs.
Now the application which is `host`, will use the guest exported calls.
The application/host also imports some calls, those are part of the `guest_imported_functions.cpp`

The directory strucutre is as below:
```
├── cpp_host (Native host code)
├── guest (Guest code)
├── guest_imported_fns.cpp ( host application imported calls)
├── main.cpp (Application)
└── wit (Wit file that is used for generating the code)
```

# Call-graph for a function
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for creating this!


This is how the example works, call graph for the function `A` (communication between the guest_1 and guest_2 using the mesh/native host code)

guest_1->exports::foo::foo::strings::A(a){native host export call}->fooX3AfooX2FstringsX23a(){native host export binding(lifting)}
-> exports::foo::foo::strings::A(wit::string &&x){guest_1 export implementation}
-> foo::foo::strings::A(std::string_view x){guest import call}->fooX3AfooX2FstringsX00a() {native host import binding(lowering)}
-> foo::foo::strings::A(std::string_view x) { guest_2 import implementation}
14 changes: 14 additions & 0 deletions crates/cpp/tests/native_strings/cpp_host/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CXXFLAGS=-g -O0 -I../../../helper-types --std=c++17
WIT_BINDGEN=../../../../target/debug/wit-bindgen

all: the_world_native.o guest_imported_fns.o

the_world_native.o:
$(CXX) $(CXXFLAGS) -fPIE -o $@ -c the_world_native.cpp

guest_imported_fns.o:
$(CXX) $(CXXFLAGS) -fPIE -o $@ -c guest_imported_fns.cpp


clean:
-rm *.o
20 changes: 20 additions & 0 deletions crates/cpp/tests/native_strings/cpp_host/guest_imported_fns.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "./the_world_cpp_native.h"
#include <iostream>

using namespace std;

void foo::foo::strings::A(std::string_view x)
{
std::cout << x << std::endl;
}
wit::string foo::foo::strings::B()
{
wit::string b = wit::string::from_view(std::string_view("hello B"));
return b;
}
wit::string foo::foo::strings::C(std::string_view a, std::string_view b)
{
std::cout << a << '|' << b << std::endl;
wit::string c = wit::string::from_view(std::string_view("hello C"));
return c;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fooX3AfooX2FstringsX23c(uint8_t *, size_t, uint8_t *, size_t);
extern "C" __attribute__((import_module("cabi_post_foo:foo/strings")))
__attribute__((import_name("c"))) void
cabi_post_fooX3AfooX2FstringsX23c(uint8_t *);
extern "C" void fooX3AfooX2FstringsX00a(uint8_t *arg0, size_t arg1) {
extern "C" void fooX3AfooX2FstringsX00a(uint8_t *arg0, size_t arg1) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding trailing whitespace?

auto len0 = arg1;

foo::foo::strings::A(std::string_view((char const *)(arg0), len0));
Expand Down Expand Up @@ -44,10 +44,10 @@ extern "C" void fooX3AfooX2FstringsX00c(uint8_t *arg0, size_t arg1,
*((size_t *)(arg4 + 8)) = len3;
*((uint8_t **)(arg4 + 0)) = ptr3;
}
void exports::foo::foo::strings::A(wit::string x) {
void exports::foo::foo::strings::A(wit::string x) {
auto const &vec0 = x;
auto ptr0 = vec0.data();
auto len0 = vec0.size();
auto len0 = vec0.size();
fooX3AfooX2FstringsX23a(ptr0, len0);
}
wit::guest_owned<std::string_view> exports::foo::foo::strings::B() {
Expand Down
35 changes: 35 additions & 0 deletions crates/cpp/tests/native_strings/guest/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
CXXFLAGS=-g -O0 -I../../../helper-types --std=c++17
WIT_BINDGEN=../../../../target/debug/wit-bindgen

all: libstrings.so

libstrings.so: the_world.pie.o guest.pie.o
$(CXX) $(CXXFLAGS) -shared -o $@ $^ -Wl,--version-script=guest.lds

%.pie.o: %.cpp
$(CXX) $(CXXFLAGS) -fPIE -o $@ -c $^

bindgen: wit/strings.wit
$(WIT_BINDGEN) cpp wit --wasm64 --format
cd cpp_host
$(WIT_BINDGEN) cpp wit --wasm64 --format --direct
cd -
cd rust/src ; ../../$(WIT_BINDGEN) rust ../../wit --wasm64

guest.wasm: the_world.cpp guest.cpp
/opt/wasi-sdk/bin/clang++ -o $@ $^ $(CXXFLAGS)

guest_release.wasm: the_world.cpp guest.cpp
/opt/wasi-sdk/bin/clang++ -o $@ $^ $(CXXFLAGS) -g0 -O3

clean:
-rm *.o libstrings.so app-strings

run:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fear these targets don't make much sense for the guest directory

LD_LIBRARY_PATH=. ./app-strings

valgrind:
LD_LIBRARY_PATH=. valgrind ./app-strings

w2c2_guest.c: guest_release.wasm
w2c2 $^ $@
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include "the_world_cpp.h"

void exports::foo::foo::strings::A(wit::string &&x) {
void exports::foo::foo::strings::A(wit::string &&x)
{
::foo::foo::strings::A(x.get_view());
}

wit::string exports::foo::foo::strings::B() {
wit::string exports::foo::foo::strings::B()
{
return ::foo::foo::strings::B();
}

wit::string exports::foo::foo::strings::C(wit::string &&x, wit::string &&b) {
wit::string exports::foo::foo::strings::C(wit::string &&x, wit::string &&b)
{
return ::foo::foo::strings::C(x.get_view(), b.get_view());
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fooX3AfooX2FstringsX00b(uint8_t *);
extern "C" __attribute__((import_module("foo:foo/strings")))
__attribute__((import_name("c"))) void
fooX3AfooX2FstringsX00c(uint8_t *, size_t, uint8_t *, size_t, uint8_t *);
void foo::foo::strings::A(std::string_view x) {
void foo::foo::strings::A(std::string_view x) {
auto const &vec0 = x;
auto ptr0 = (uint8_t *)(vec0.data());
auto len0 = (size_t)(vec0.size());
Expand Down Expand Up @@ -63,9 +63,8 @@ wit::string foo::foo::strings::C(std::string_view a, std::string_view b) {
return wit::string((char const *)(*((uint8_t **)(ptr2 + 0))), len3);
}
extern "C" __attribute__((__export_name__("foo:foo/strings#a"))) void
fooX3AfooX2FstringsX23a(uint8_t *arg0, size_t arg1) {
auto len0 = arg1;

fooX3AfooX2FstringsX23a(uint8_t *arg0, size_t arg1) {
auto len0 = arg1;
exports::foo::foo::strings::A(wit::string((char const *)(arg0), len0));
}
extern "C" __attribute__((__export_name__("foo:foo/strings#b"))) uint8_t *
Expand Down
15 changes: 1 addition & 14 deletions crates/cpp/tests/native_strings/main.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@

#include "the_world_cpp_native.h"
#include "./cpp_host/the_world_cpp_native.h"
#include <iostream>

void foo::foo::strings::A(std::string_view x) {
std::cout << x << std::endl;
}
wit::string foo::foo::strings::B() {
wit::string b = wit::string::from_view(std::string_view("hello B"));
return b;
}
wit::string foo::foo::strings::C(std::string_view a, std::string_view b) {
std::cout << a << '|' << b << std::endl;
wit::string c = wit::string::from_view(std::string_view("hello C"));
return c;
}

int main() {
wit::string a = wit::string::from_view(std::string_view("hello A"));
exports::foo::foo::strings::A(a);
Expand Down
6 changes: 6 additions & 0 deletions crates/cpp/tests/native_strings/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::alloc::Layout;

use the_world::exports::foo::foo::strings::Guest;

use the_world::exports::foo::foo::strings;
#[macro_use]
mod the_world;
mod the_world_native;

struct MyWorld;

Expand All @@ -22,6 +25,9 @@ impl Guest for MyWorld {

the_world::export!(MyWorld with_types_in the_world);

__export_foo_foo_strings_cabi!(MyWorld with_types_in strings);


// the crate wit-bindgen-rt doesn't work on native
#[no_mangle]
pub unsafe extern "C" fn cabi_realloc(
Expand Down
Loading