From b939b92f7dac66b6874e511787a423b1442ecf71 Mon Sep 17 00:00:00 2001 From: IgorKhomenko Date: Thu, 11 Jul 2024 17:59:08 +0300 Subject: [PATCH 1/7] updates to protobuf --- .gitignore | 4 +++- README.md | 7 +++++++ cpp/my_code.cpp | 2 ++ prepare-protobuf.sh | 2 ++ resources/proto/wallet.proto | 10 ++++++++++ 5 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 prepare-protobuf.sh create mode 100644 resources/proto/wallet.proto 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..8b595b2 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 @@ -37,3 +42,5 @@ This should print the following: The sodium is initialized! sodium randombytes: 714298213 ``` + + \ No newline at end of file diff --git a/cpp/my_code.cpp b/cpp/my_code.cpp index 00a1b81..eb77dd5 100644 --- a/cpp/my_code.cpp +++ b/cpp/my_code.cpp @@ -10,6 +10,8 @@ #include +#include "wallet.pb.h" + struct KeyPair { std::vector public_key; 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; +} From ef2ab2cba7b9502a40989d57fd23b0f042a22223 Mon Sep 17 00:00:00 2001 From: IgorKhomenko Date: Thu, 11 Jul 2024 18:00:06 +0300 Subject: [PATCH 2/7] cleanup --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 8b595b2..1fe350d 100644 --- a/README.md +++ b/README.md @@ -41,6 +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 +``` \ No newline at end of file From d2dcaee37b9b6ab63e01cae4dfb20419c77a26e7 Mon Sep 17 00:00:00 2001 From: IgorKhomenko Date: Fri, 12 Jul 2024 14:44:56 +0300 Subject: [PATCH 3/7] examples --- cpp/add.cpp | 13 +++++++++++++ cpp/add.h | 15 +++++++++++++++ cpp/add2.h | 19 +++++++++++++++++++ cpp/my_code.cpp | 19 +++++++++++++++++-- 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 cpp/add.cpp create mode 100644 cpp/add.h create mode 100644 cpp/add2.h diff --git a/cpp/add.cpp b/cpp/add.cpp new file mode 100644 index 0000000..354bfc9 --- /dev/null +++ b/cpp/add.cpp @@ -0,0 +1,13 @@ +// add.cpp +#include "add.h" + +A2DD::A2DD(int x, int y) +{ + gx = x; + gy = y; +} + +int A2DD::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..7c5280c --- /dev/null +++ b/cpp/add.h @@ -0,0 +1,15 @@ +// A2DD.h +#ifndef A2DD_H +#define A2DD_H + +class A2DD +{ + int gx; + int gy; + +public: + A2DD(int x, int y); + int getSum(); +}; + +#endif \ No newline at end of file diff --git a/cpp/add2.h b/cpp/add2.h new file mode 100644 index 0000000..27818f0 --- /dev/null +++ b/cpp/add2.h @@ -0,0 +1,19 @@ +#ifndef A2DD2_H +#define A2DD2_H + +class A2DD2 +{ + int gx; + int gy; + +public: + A2DD2(int x, int y) { + gx = x; + gy = y; + } + int getSum() { + return gx + gy; + } +}; + +#endif \ No newline at end of file diff --git a/cpp/my_code.cpp b/cpp/my_code.cpp index eb77dd5..8348bcf 100644 --- a/cpp/my_code.cpp +++ b/cpp/my_code.cpp @@ -12,6 +12,9 @@ #include "wallet.pb.h" +#include "add.h" +#include "add2.h" + struct KeyPair { std::vector public_key; @@ -19,7 +22,7 @@ struct KeyPair }; extern "C" -{ +{ void hello() { // Test C++ @@ -84,9 +87,21 @@ extern "C" std::string s = "Boost Libraries"; boost::to_upper(s); printf("[hello] Boost Upper Case test: %s\n", s.c_str()); + + // Test protobuf + // + // google::protobuf::internal::AnyMetadata max_supply; + // TableStruct_wallet_2eproto a; + + // A2DD *a = new A2DD(1, 2); + // a->getSum(); + + A2DD2 *a = new A2DD2(1, 2); + printf("SUM: %d\n", a->getSum()); } - pid_t getpid(void) { + pid_t getpid(void) + { return 1; } } \ No newline at end of file From 593812679955100100c39c42c35c101697302994 Mon Sep 17 00:00:00 2001 From: IgorKhomenko Date: Mon, 15 Jul 2024 21:24:40 +0300 Subject: [PATCH 4/7] cleanuop --- cpp/add.cpp | 13 ------------- cpp/add.h | 13 ++++++++++++- cpp/add2.h | 19 ------------------- cpp/my_code.cpp | 11 +++-------- 4 files changed, 15 insertions(+), 41 deletions(-) delete mode 100644 cpp/add.cpp delete mode 100644 cpp/add2.h diff --git a/cpp/add.cpp b/cpp/add.cpp deleted file mode 100644 index 354bfc9..0000000 --- a/cpp/add.cpp +++ /dev/null @@ -1,13 +0,0 @@ -// add.cpp -#include "add.h" - -A2DD::A2DD(int x, int y) -{ - gx = x; - gy = y; -} - -int A2DD::getSum() -{ - return gx + gy; -} \ No newline at end of file diff --git a/cpp/add.h b/cpp/add.h index 7c5280c..1fed9be 100644 --- a/cpp/add.h +++ b/cpp/add.h @@ -12,4 +12,15 @@ class A2DD int getSum(); }; -#endif \ No newline at end of file +#endif + +A2DD::A2DD(int x, int y) +{ + gx = x; + gy = y; +} + +int A2DD::getSum() +{ + return gx + gy; +} \ No newline at end of file diff --git a/cpp/add2.h b/cpp/add2.h deleted file mode 100644 index 27818f0..0000000 --- a/cpp/add2.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef A2DD2_H -#define A2DD2_H - -class A2DD2 -{ - int gx; - int gy; - -public: - A2DD2(int x, int y) { - gx = x; - gy = y; - } - int getSum() { - return gx + gy; - } -}; - -#endif \ No newline at end of file diff --git a/cpp/my_code.cpp b/cpp/my_code.cpp index 8348bcf..5518630 100644 --- a/cpp/my_code.cpp +++ b/cpp/my_code.cpp @@ -13,7 +13,6 @@ #include "wallet.pb.h" #include "add.h" -#include "add2.h" struct KeyPair { @@ -90,14 +89,10 @@ extern "C" // Test protobuf // - // google::protobuf::internal::AnyMetadata max_supply; - // TableStruct_wallet_2eproto a; + my_wallets::MaxSupply max_supply; - // A2DD *a = new A2DD(1, 2); - // a->getSum(); - - A2DD2 *a = new A2DD2(1, 2); - printf("SUM: %d\n", a->getSum()); + A2DD *a1 = new A2DD(2, 2); + printf("SUM1: %d\n", a1->getSum()); } pid_t getpid(void) From bf4f0c3ec8fe92b60b40c5a5d19d47d35348aa7f Mon Sep 17 00:00:00 2001 From: IgorKhomenko Date: Wed, 17 Jul 2024 11:19:39 +0300 Subject: [PATCH 5/7] test wrapper --- build.rs | 3 ++- cpp/add.cpp | 30 ++++++++++++++++++++++++++++++ cpp/add.h | 25 +++++++++++-------------- cpp/my_code.cpp | 8 +++++--- 4 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 cpp/add.cpp diff --git a/build.rs b/build.rs index 299c8ba..6de657e 100644 --- a/build.rs +++ b/build.rs @@ -12,7 +12,8 @@ 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/my_code.cpp"); if target.contains("wasm32") { diff --git a/cpp/add.cpp b/cpp/add.cpp new file mode 100644 index 0000000..dddb880 --- /dev/null +++ b/cpp/add.cpp @@ -0,0 +1,30 @@ +#include "add.h" + +Add::Add(int x, int y) +{ + gx = x; + gy = y; +} + +int Add::getSum() +{ + return gx + gy; +} + +extern "C" +{ + Add *Add_C_new(int x, int y) + { + return new Add(x, y); + } + + void Add_C_delete(Add *add) + { + delete add; + } + + int Add_C_getSum(Add *add) + { + return add->getSum(); + } +} \ No newline at end of file diff --git a/cpp/add.h b/cpp/add.h index 1fed9be..aa529d6 100644 --- a/cpp/add.h +++ b/cpp/add.h @@ -1,26 +1,23 @@ -// A2DD.h -#ifndef A2DD_H -#define A2DD_H +// add.h +#ifndef add_H +#define add_H -class A2DD +class Add { int gx; int gy; public: - A2DD(int x, int y); + Add(int x, int y); int getSum(); }; -#endif - -A2DD::A2DD(int x, int y) +// wrapper +extern "C" { - gx = x; - gy = y; + Add *Add_C_new(int x, int y); + void Add_C_delete(Add *add); + int Add_C_getSum(Add *add); } -int A2DD::getSum() -{ - return gx + gy; -} \ No newline at end of file +#endif \ No newline at end of file diff --git a/cpp/my_code.cpp b/cpp/my_code.cpp index 5518630..c715f6d 100644 --- a/cpp/my_code.cpp +++ b/cpp/my_code.cpp @@ -89,10 +89,12 @@ extern "C" // Test protobuf // - my_wallets::MaxSupply max_supply; + // my_wallets::MaxSupply max_supply; - A2DD *a1 = new A2DD(2, 2); - printf("SUM1: %d\n", a1->getSum()); + // Add *a1 = new Add(2, 2); + // printf("SUM1: %d\n", a1->getSum()); + Add *a2 = Add_C_new(2, 2); + printf("SUM2: %d\n", Add_C_getSum(a2)); } pid_t getpid(void) From 6a46025981451a18e4b4eafd9e8825d38475ddf7 Mon Sep 17 00:00:00 2001 From: IgorKhomenko Date: Wed, 17 Jul 2024 11:21:28 +0300 Subject: [PATCH 6/7] proper connect of C++ classes --- cpp/add.cpp | 18 ------------------ cpp/add.h | 8 -------- cpp/my_code.cpp | 6 ++---- 3 files changed, 2 insertions(+), 30 deletions(-) diff --git a/cpp/add.cpp b/cpp/add.cpp index dddb880..fa1ff22 100644 --- a/cpp/add.cpp +++ b/cpp/add.cpp @@ -9,22 +9,4 @@ Add::Add(int x, int y) int Add::getSum() { return gx + gy; -} - -extern "C" -{ - Add *Add_C_new(int x, int y) - { - return new Add(x, y); - } - - void Add_C_delete(Add *add) - { - delete add; - } - - int Add_C_getSum(Add *add) - { - return add->getSum(); - } } \ No newline at end of file diff --git a/cpp/add.h b/cpp/add.h index aa529d6..11959c8 100644 --- a/cpp/add.h +++ b/cpp/add.h @@ -12,12 +12,4 @@ class Add int getSum(); }; -// wrapper -extern "C" -{ - Add *Add_C_new(int x, int y); - void Add_C_delete(Add *add); - int Add_C_getSum(Add *add); -} - #endif \ No newline at end of file diff --git a/cpp/my_code.cpp b/cpp/my_code.cpp index c715f6d..d8055f5 100644 --- a/cpp/my_code.cpp +++ b/cpp/my_code.cpp @@ -91,10 +91,8 @@ extern "C" // // my_wallets::MaxSupply max_supply; - // Add *a1 = new Add(2, 2); - // printf("SUM1: %d\n", a1->getSum()); - Add *a2 = Add_C_new(2, 2); - printf("SUM2: %d\n", Add_C_getSum(a2)); + Add *a1 = new Add(5, 5); + printf("SUM1: %d\n", a1->getSum()); } pid_t getpid(void) From d15fd36483f8b0856b998a0cf03a5805d4781e42 Mon Sep 17 00:00:00 2001 From: IgorKhomenko Date: Wed, 17 Jul 2024 11:34:33 +0300 Subject: [PATCH 7/7] updated build.rs --- build.rs | 1 + cpp/my_code.cpp | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/build.rs b/build.rs index 6de657e..b57a61a 100644 --- a/build.rs +++ b/build.rs @@ -14,6 +14,7 @@ fn main() { let mut build = cc::Build::new(); // 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/my_code.cpp b/cpp/my_code.cpp index d8055f5..78b194c 100644 --- a/cpp/my_code.cpp +++ b/cpp/my_code.cpp @@ -87,12 +87,14 @@ extern "C" boost::to_upper(s); printf("[hello] Boost Upper Case test: %s\n", s.c_str()); - // Test protobuf + // Test external class // - // my_wallets::MaxSupply max_supply; - Add *a1 = new Add(5, 5); printf("SUM1: %d\n", a1->getSum()); + + // Test protobuf + // + my_wallets::MaxSupply max_supply; } pid_t getpid(void)