diff --git a/.gitignore b/.gitignore index 4a96461..302f16f 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,6 @@ Cargo.lock libsodium openssl-wasm -boost \ No newline at end of file +boost +*pb.cc +*pb.h \ No newline at end of file diff --git a/README.md b/README.md index 1dfb83b..1fe350d 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,11 @@ export PATH=$WASI_SDK_PATH/bin:$PATH 4. Run `sh prepare-sodium.sh` to prepare `libsodium` for wasm env. You may need to install `zig` in advance. 5. Run `sh prepare-openssl.sh` to prepare `libssl` for wasm env. Also, in some of your C++ file you need to define `pid_t getpid(void) {return 1;}` since WASI lacks process identifiers, so we nede to define some stub. 6. Run `sh prepare-boost.sh` to prepare `boost` for wasm env. +7. Run `sh prepare-protobuf.sh` to prepare `protobuf` for wasm env. For protobuf to work we also need to use wasi-sdk with pthreads support, e.g. https://github.com/WebAssembly/wasi-sdk/releases/tag/wasi-sdk-20%2Bthreads, and then do : +``` +export WASI_SDK_PATH=~/Downloads/wasi-sdk-20.0+threads +export PATH=$WASI_SDK_PATH/bin:$PATH +``` ## Build @@ -36,4 +41,4 @@ This should print the following: [hello] Hello World from C++! (printf) The sodium is initialized! sodium randombytes: 714298213 -``` +``` \ No newline at end of file diff --git a/build.rs b/build.rs index 299c8ba..b57a61a 100644 --- a/build.rs +++ b/build.rs @@ -12,7 +12,9 @@ fn main() { let target = env::var("TARGET").unwrap(); let mut build = cc::Build::new(); - // build.cpp(true) // this will cause rust-lld: error: unable to find library -lstdc++ + // build.cpp(true); // this will cause rust-lld: error: unable to find library -lstdc++ + build.file("cpp/add.cpp"); + build.file("cpp/wallet.pb.cc"); build.file("cpp/my_code.cpp"); if target.contains("wasm32") { diff --git a/cpp/add.cpp b/cpp/add.cpp new file mode 100644 index 0000000..fa1ff22 --- /dev/null +++ b/cpp/add.cpp @@ -0,0 +1,12 @@ +#include "add.h" + +Add::Add(int x, int y) +{ + gx = x; + gy = y; +} + +int Add::getSum() +{ + return gx + gy; +} \ No newline at end of file diff --git a/cpp/add.h b/cpp/add.h new file mode 100644 index 0000000..11959c8 --- /dev/null +++ b/cpp/add.h @@ -0,0 +1,15 @@ +// add.h +#ifndef add_H +#define add_H + +class Add +{ + int gx; + int gy; + +public: + Add(int x, int y); + int getSum(); +}; + +#endif \ No newline at end of file diff --git a/cpp/my_code.cpp b/cpp/my_code.cpp index 00a1b81..78b194c 100644 --- a/cpp/my_code.cpp +++ b/cpp/my_code.cpp @@ -10,6 +10,10 @@ #include +#include "wallet.pb.h" + +#include "add.h" + struct KeyPair { std::vector public_key; @@ -17,7 +21,7 @@ struct KeyPair }; extern "C" -{ +{ void hello() { // Test C++ @@ -82,9 +86,19 @@ extern "C" std::string s = "Boost Libraries"; boost::to_upper(s); printf("[hello] Boost Upper Case test: %s\n", s.c_str()); + + // Test external class + // + Add *a1 = new Add(5, 5); + printf("SUM1: %d\n", a1->getSum()); + + // Test protobuf + // + my_wallets::MaxSupply max_supply; } - pid_t getpid(void) { + pid_t getpid(void) + { return 1; } } \ No newline at end of file diff --git a/prepare-protobuf.sh b/prepare-protobuf.sh new file mode 100644 index 0000000..0d253f5 --- /dev/null +++ b/prepare-protobuf.sh @@ -0,0 +1,2 @@ +cd resources/proto +protoc --cpp_out=../../cpp/ *.proto diff --git a/resources/proto/wallet.proto b/resources/proto/wallet.proto new file mode 100644 index 0000000..8b970a6 --- /dev/null +++ b/resources/proto/wallet.proto @@ -0,0 +1,10 @@ +syntax = "proto3"; + +package my_wallets; + +option optimize_for = SPEED; + +message MaxSupply{ + string max_supply = 1; + string circulation = 2; +}