diff --git a/marine-examples/README.md b/marine-examples/README.md index 4103a09..dc03ea1 100644 --- a/marine-examples/README.md +++ b/marine-examples/README.md @@ -10,6 +10,7 @@ Figure 1: Module And Service Creation With The Marine Toolchain ```mermaid stateDiagram + state "Rust Marine Code" as Code state "Build wasm32-wasi Module" as Build state "Test Wasm App With Cargo" as Test @@ -26,9 +27,10 @@ Figure 1: Module And Service Creation With The Marine Toolchain Build --> Test Build --> Deploy Service --> Deploy + ``` -Wasm modules can also be accessed locally through the Marine REPL and tested with `cargo test`. +Wasm modules can also be accessed locally through the Marine REPL and tested with `cargo test`. Moreover, most of the examples listed below have a packaging script providing modules in the archived format, i.e., tar.gz, for easy import into [fluence cli](https://github.com/fluencelabs/fluence-cli) projects. ## Greeting Example diff --git a/marine-examples/call_parameters/module-exports/call_parameters.tar.gz b/marine-examples/call_parameters/module-exports/call_parameters.tar.gz new file mode 100644 index 0000000..70dc2a6 Binary files /dev/null and b/marine-examples/call_parameters/module-exports/call_parameters.tar.gz differ diff --git a/marine-examples/call_parameters/package_modules.sh b/marine-examples/call_parameters/package_modules.sh new file mode 100755 index 0000000..831b0a3 --- /dev/null +++ b/marine-examples/call_parameters/package_modules.sh @@ -0,0 +1,18 @@ + +mkdir -p module-exports/modules/call_parameters +sh ./build.sh + +cd module-exports +cp ../target/wasm32-wasi/release/call_parameters.wasm modules/call_parameters/ +file="modules/call_parameters/module.yaml" +cat > $file <<- EOF +version: 0 +name: call_parameters +loggerEnabled: true +EOF + +tar -czf call_parameters.tar.gz modules +rm -r modules + +cd ../ +cargo clean diff --git a/marine-examples/greeting/module-exports/greeting.tar.gz b/marine-examples/greeting/module-exports/greeting.tar.gz new file mode 100644 index 0000000..331045a Binary files /dev/null and b/marine-examples/greeting/module-exports/greeting.tar.gz differ diff --git a/marine-examples/greeting/package_modules.sh b/marine-examples/greeting/package_modules.sh new file mode 100755 index 0000000..6f8958b --- /dev/null +++ b/marine-examples/greeting/package_modules.sh @@ -0,0 +1,19 @@ +mkdir -p module-exports/modules/greeting +sh ./build.sh + +cd module-exports +cp ../target/wasm32-wasi/release/greeting.wasm modules/greeting/ + +file="modules/greeting/module.yaml" +cat > $file <<- EOF +version: 0 +name: local_storage +loggerEnabled: true +EOF + + +tar -czvf greeting.tar.gz modules +rm -r modules +cd ../ + +cargo clean diff --git a/marine-examples/records/Config.toml b/marine-examples/records/Config.toml index d86e4c1..c8ed3a2 100644 --- a/marine-examples/records/Config.toml +++ b/marine-examples/records/Config.toml @@ -1,11 +1,11 @@ modules_dir = "artifacts/" -[[module]] -name = "records_effector" -max_heap_size = "10 KiB" -logger_enabled = true - [[module]] name = "records_pure" max_heap_size = "10 KiB" logger_enabled = true + +[[module]] +name = "records_facade" +max_heap_size = "10 KiB" +logger_enabled = true diff --git a/marine-examples/records/build.sh b/marine-examples/records/build.sh index 40a6e22..b1c7744 100755 --- a/marine-examples/records/build.sh +++ b/marine-examples/records/build.sh @@ -2,17 +2,15 @@ set -o errexit -o nounset -o pipefail # This script builds all subprojects and puts all created Wasm modules in one dir -cd effector -cargo update --aggressive +cd pure marine build --release -cd ../pure -cargo update --aggressive +cd ../facade marine build --release cd .. mkdir -p artifacts rm -f artifacts/*.wasm -cp effector/target/wasm32-wasi/release/records_effector.wasm artifacts/ cp pure/target/wasm32-wasi/release/records_pure.wasm artifacts/ +cp facade/target/wasm32-wasi/release/records_facade.wasm artifacts/ diff --git a/marine-examples/records/effector/Cargo.toml b/marine-examples/records/facade/Cargo.toml similarity index 79% rename from marine-examples/records/effector/Cargo.toml rename to marine-examples/records/facade/Cargo.toml index d52c060..92698f1 100644 --- a/marine-examples/records/effector/Cargo.toml +++ b/marine-examples/records/facade/Cargo.toml @@ -1,12 +1,12 @@ [package] -name = "record-effector" +name = "record-facade" version = "0.1.0" authors = ["Fluence Labs"] edition = "2018" publish = false [[bin]] -name = "records_effector" +name = "records_facade" path = "src/main.rs" [dependencies] diff --git a/marine-examples/records/effector/src/main.rs b/marine-examples/records/facade/src/main.rs similarity index 57% rename from marine-examples/records/effector/src/main.rs rename to marine-examples/records/facade/src/main.rs index 3809741..274f1cd 100644 --- a/marine-examples/records/effector/src/main.rs +++ b/marine-examples/records/facade/src/main.rs @@ -14,6 +14,8 @@ * limitations under the License. */ +#![allow(improper_ctypes)] + use marine_rs_sdk::marine; use marine_rs_sdk::module_manifest; @@ -24,20 +26,28 @@ module_manifest!(); pub fn main() {} #[marine] -pub fn mutate_struct(mut test_record: test_record::TestRecord) -> TestRecord { - test_record.field_0 = true; - test_record.field_1 = 1; - test_record.field_2 = 2; - test_record.field_3 = 3; - test_record.field_4 = 4; - test_record.field_5 = 5; - test_record.field_6 = 6; - test_record.field_7 = 7; - test_record.field_8 = 8; - test_record.field_9 = 9f32; - test_record.field_10 = 10f64; - test_record.field_11 = "field_11".to_string(); - test_record.field_12 = vec![0x13, 0x37]; +pub fn invoke() -> TestRecord { + let test_record = TestRecord { + field_0: false, + field_1: 0, + field_2: 0, + field_3: 0, + field_4: 0, + field_5: 0, + field_6: 0, + field_7: 0, + field_8: 0, + field_9: 0f32, + field_10: 0f64, + field_11: String::new(), + field_12: Vec::new(), + }; - test_record + mutate_struct(test_record) +} + +#[marine] +#[link(wasm_import_module = "records_pure")] +extern "C" { + pub fn mutate_struct(test_record: TestRecord) -> TestRecord; } diff --git a/marine-examples/records/module-exports/records.tar.gz b/marine-examples/records/module-exports/records.tar.gz new file mode 100644 index 0000000..e7c2e2a Binary files /dev/null and b/marine-examples/records/module-exports/records.tar.gz differ diff --git a/marine-examples/records/package_modules.sh b/marine-examples/records/package_modules.sh new file mode 100755 index 0000000..2b56d95 --- /dev/null +++ b/marine-examples/records/package_modules.sh @@ -0,0 +1,45 @@ + +mkdir -p module-exports/modules/facade +mkdir -p module-exports/modules/pure + +sh ./build.sh + +cd module-exports +cp ../facade/target/wasm32-wasi/release/records_facade.wasm modules/facade/ +cp ../pure/target/wasm32-wasi/release/records_pure.wasm modules/pure/ + +file="service.yaml" +cat > $file <<- EOF +version: 0 +name: records +modules: + pure: + get: modules/facade + effector: + get: modules/pure +EOF + + +file="modules/facade/module.yaml" +cat > $file <<- EOF +version: 0 +name: facade +loggerEnabled: true +EOF + +file="modules/pure/module.yaml" +cat > $file <<- EOF +version: 0 +name: pure +loggerEnabled: true +EOF + +tar -czf records.tar.gz modules service.yaml +rm -r modules +rm service.yaml + +cd ../facade +cargo clean +pwd +cd ../pure +cargo clean diff --git a/marine-examples/records/pure/src/main.rs b/marine-examples/records/pure/src/main.rs index 65bd6b2..3809741 100644 --- a/marine-examples/records/pure/src/main.rs +++ b/marine-examples/records/pure/src/main.rs @@ -14,8 +14,6 @@ * limitations under the License. */ -#![allow(improper_ctypes)] - use marine_rs_sdk::marine; use marine_rs_sdk::module_manifest; @@ -26,28 +24,20 @@ module_manifest!(); pub fn main() {} #[marine] -pub fn invoke() -> TestRecord { - let test_record = TestRecord { - field_0: false, - field_1: 0, - field_2: 0, - field_3: 0, - field_4: 0, - field_5: 0, - field_6: 0, - field_7: 0, - field_8: 0, - field_9: 0f32, - field_10: 0f64, - field_11: String::new(), - field_12: Vec::new(), - }; +pub fn mutate_struct(mut test_record: test_record::TestRecord) -> TestRecord { + test_record.field_0 = true; + test_record.field_1 = 1; + test_record.field_2 = 2; + test_record.field_3 = 3; + test_record.field_4 = 4; + test_record.field_5 = 5; + test_record.field_6 = 6; + test_record.field_7 = 7; + test_record.field_8 = 8; + test_record.field_9 = 9f32; + test_record.field_10 = 10f64; + test_record.field_11 = "field_11".to_string(); + test_record.field_12 = vec![0x13, 0x37]; - mutate_struct(test_record) -} - -#[marine] -#[link(wasm_import_module = "records_effector")] -extern "C" { - pub fn mutate_struct(test_record: TestRecord) -> TestRecord; + test_record } diff --git a/marine-examples/sqlite/module-exports/sqlite.tar.gz b/marine-examples/sqlite/module-exports/sqlite.tar.gz new file mode 100644 index 0000000..bad910b Binary files /dev/null and b/marine-examples/sqlite/module-exports/sqlite.tar.gz differ diff --git a/marine-examples/sqlite/package_modules.sh b/marine-examples/sqlite/package_modules.sh new file mode 100755 index 0000000..0e8d5fb --- /dev/null +++ b/marine-examples/sqlite/package_modules.sh @@ -0,0 +1,19 @@ +mkdir -p module-exports/modules/sqlite +sh ./build.sh + +cd module-exports +cp ../artifacts/sqlite3.wasm modules/sqlite/ + +file="modules/sqlite/module.yaml" +cat > $file <<- EOF +version: 0 +name: sqlite +loggerEnabled: true +EOF + + +tar -czvf sqlite.tar.gz modules +rm -r modules +cd ../ + +cargo clean diff --git a/marine-examples/url-downloader/module-exports/url_downloader.tar.gz b/marine-examples/url-downloader/module-exports/url_downloader.tar.gz new file mode 100644 index 0000000..0271af3 Binary files /dev/null and b/marine-examples/url-downloader/module-exports/url_downloader.tar.gz differ diff --git a/marine-examples/url-downloader/package_modules.sh b/marine-examples/url-downloader/package_modules.sh new file mode 100755 index 0000000..0c76a13 --- /dev/null +++ b/marine-examples/url-downloader/package_modules.sh @@ -0,0 +1,62 @@ + +mkdir -p module-exports/modules/curl_adapter +mkdir -p module-exports/modules/facade +mkdir -p module-exports/modules/local_storage +sh ./build.sh + +cd module-exports +cp ../curl_adapter/target/wasm32-wasi/release/curl_adapter.wasm modules/curl_adapter/ +cp ../facade/target/wasm32-wasi/release/facade.wasm modules/facade/ +cp ../local_storage/target/wasm32-wasi/release/local_storage.wasm modules/local_storage/ + +file="service.yaml" +cat > $file <<- EOF +version: 0 +name: urlDownloader +modules: + facade: + get: modules/facade + localStorage: + get: modules/local_storage + curlAdapter: + get: modules/curl_adapter +EOF + + +file="modules/curl_adapter/module.yaml" +cat > $file <<- EOF +version: 0 +name: curl_adapter +loggerEnabled: true +mountedBinaries: + curl: /usr/bin/curl +EOF + +file="modules/local_storage/module.yaml" +cat > $file <<- EOF +version: 0 +name: local_storage +loggerEnabled: true +volumes: + sites: ./tmp +EOF + +file="modules/facade/module.yaml" +cat > $file <<- EOF +version: 0 +name: facade +loggerEnabled: true +EOF + + +tar -czf url_downloader.tar.gz modules service.yaml +rm -r modules +rm service.yaml + +cd ../facade +cargo clean +pwd +cd ../curl_adapter +cargo clean +cd ../local_storage +cargo clean