From 0a32104cbc7d156d7cf771169be3e3978af17fec Mon Sep 17 00:00:00 2001 From: vms Date: Tue, 28 Jul 2020 01:19:15 +0300 Subject: [PATCH] add example with records --- Cargo.lock | 60 +++++++++++------- Cargo.toml | 7 ++- README.md | 14 ++--- crates/fce-wit-interfaces/Cargo.toml | 2 +- .../foreign_mod_instructions.rs | 9 ++- engine/src/module/wit_instance.rs | 23 ++++++- examples/records/Cargo.toml | 11 ++++ examples/records/Config.toml | 11 ++++ examples/records/src/main.rs | 33 ++++++++++ examples/records/wasm/effector/Cargo.toml | 12 ++++ examples/records/wasm/effector/src/main.rs | 55 ++++++++++++++++ examples/records/wasm/pure/Cargo.toml | 12 ++++ examples/records/wasm/pure/src/main.rs | 63 +++++++++++++++++++ 13 files changed, 271 insertions(+), 41 deletions(-) create mode 100644 examples/records/Cargo.toml create mode 100644 examples/records/Config.toml create mode 100644 examples/records/src/main.rs create mode 100644 examples/records/wasm/effector/Cargo.toml create mode 100644 examples/records/wasm/effector/src/main.rs create mode 100644 examples/records/wasm/pure/Cargo.toml create mode 100644 examples/records/wasm/pure/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 1ef0fb3a..84838785 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -313,6 +313,13 @@ dependencies = [ "generic-array 0.14.3", ] +[[package]] +name = "effector" +version = "0.1.0" +dependencies = [ + "fluence", +] + [[package]] name = "either" version = "1.5.3" @@ -403,7 +410,7 @@ dependencies = [ "parity-wasm", "pwasm-utils", "serde", - "wasmer-interface-types 0.17.0 (git+http://github.com/fluencelabs/interface-types?branch=struct_support)", + "wasmer-interface-types", "wasmer-runtime", "wasmer-runtime-core", "wasmer-wasi", @@ -415,7 +422,7 @@ name = "fce-wit-interfaces" version = "0.1.0" dependencies = [ "multimap", - "wasmer-interface-types 0.17.0 (git+https://github.com/fluencelabs/interface-types?branch=byte_array)", + "wasmer-interface-types", ] [[package]] @@ -434,7 +441,7 @@ dependencies = [ [[package]] name = "fluence" version = "0.2.0" -source = "git+https://github.com/fluencelabs/rust-sdk#e589d52c7247462f194800f50595029d224a5e43" +source = "git+https://github.com/fluencelabs/rust-sdk#88afe8feaa6119ebeb2032281c26f6b74f2f8fd1" dependencies = [ "fluence-sdk-macro", "fluence-sdk-main", @@ -459,7 +466,7 @@ dependencies = [ [[package]] name = "fluence-sdk-macro" version = "0.2.0" -source = "git+https://github.com/fluencelabs/rust-sdk#e589d52c7247462f194800f50595029d224a5e43" +source = "git+https://github.com/fluencelabs/rust-sdk#88afe8feaa6119ebeb2032281c26f6b74f2f8fd1" dependencies = [ "fluence-sdk-wit", ] @@ -467,7 +474,7 @@ dependencies = [ [[package]] name = "fluence-sdk-main" version = "0.2.0" -source = "git+https://github.com/fluencelabs/rust-sdk#e589d52c7247462f194800f50595029d224a5e43" +source = "git+https://github.com/fluencelabs/rust-sdk#88afe8feaa6119ebeb2032281c26f6b74f2f8fd1" dependencies = [ "log", ] @@ -475,7 +482,7 @@ dependencies = [ [[package]] name = "fluence-sdk-wit" version = "0.2.0" -source = "git+https://github.com/fluencelabs/rust-sdk#e589d52c7247462f194800f50595029d224a5e43" +source = "git+https://github.com/fluencelabs/rust-sdk#88afe8feaa6119ebeb2032281c26f6b74f2f8fd1" dependencies = [ "proc-macro2", "quote", @@ -862,6 +869,13 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "pure" +version = "0.1.0" +dependencies = [ + "fluence", +] + [[package]] name = "pwasm-utils" version = "0.12.0" @@ -965,6 +979,16 @@ dependencies = [ "num_cpus", ] +[[package]] +name = "records" +version = "0.1.0" +dependencies = [ + "anyhow", + "env_logger", + "fluence-faas", + "log", +] + [[package]] name = "redox_syscall" version = "0.1.57" @@ -1078,9 +1102,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.56" +version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3433e879a558dde8b5e8feb2a04899cf34fdde1fafb894687e52105fc1162ac3" +checksum = "164eacbdb13512ec2745fb09d51fd5b22b0d65ed294a1dcf7285a360c80a675c" dependencies = [ "itoa", "ryu", @@ -1113,9 +1137,9 @@ checksum = "502d53007c02d7605a05df1c1a73ee436952781653da5d0bf57ad608f66932c1" [[package]] name = "syn" -version = "1.0.35" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb7f4c519df8c117855e19dd8cc851e89eb746fe7a73f0157e0d95fdec5369b0" +checksum = "4cdb98bcb1f9d81d07b536179c269ea15999b5d14ea958196413869445bb5250" dependencies = [ "proc-macro2", "quote", @@ -1390,17 +1414,7 @@ dependencies = [ [[package]] name = "wasmer-interface-types" version = "0.17.0" -source = "git+https://github.com/fluencelabs/interface-types?branch=byte_array#4521e79a50e39dfa155d07e74ffc7d12203fa198" -dependencies = [ - "nom", - "serde", - "wast", -] - -[[package]] -name = "wasmer-interface-types" -version = "0.17.0" -source = "git+http://github.com/fluencelabs/interface-types?branch=struct_support#053056c1eb5063b8aab480022301e4b5cd5baa81" +source = "git+https://github.com/fluencelabs/interface-types?branch=struct_support#eb3dbcb8353f658040621e179251b182695f04f4" dependencies = [ "nom", "safe-transmute", @@ -1541,7 +1555,7 @@ dependencies = [ "serde", "serde_json", "walrus", - "wasmer-interface-types 0.17.0 (git+http://github.com/fluencelabs/interface-types?branch=struct_support)", + "wasmer-interface-types", "wit-parser", ] @@ -1552,6 +1566,6 @@ dependencies = [ "anyhow", "fce-wit-interfaces", "walrus", - "wasmer-interface-types 0.17.0 (git+http://github.com/fluencelabs/interface-types?branch=struct_support)", + "wasmer-interface-types", "wasmer-runtime-core", ] diff --git a/Cargo.toml b/Cargo.toml index 0dd0ff29..560fbdd5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,11 +4,14 @@ members = [ "crates/wit-generator", "crates/wit-parser", "engine", + "examples/greeting", + "examples/greeting/wasm/", "examples/ipfs_node", "examples/ipfs_node/wasm/ipfs_node", "examples/ipfs_node/wasm/ipfs_rpc", - "examples/greeting", - "examples/greeting/wasm/", + "examples/records", + "examples/records/wasm/effector", + "examples/records/wasm/pure", "fluence-faas", "tools/cli", ] diff --git a/README.md b/README.md index 58fbcb8c..0e1e9d33 100644 --- a/README.md +++ b/README.md @@ -2,22 +2,22 @@ FCE is intended to run various Wasm binaries. At now, it is in the heavily developing phase. -# Installation +## Installation - Clone this project - `cargo install --path ` -# Usage +## Usage - `fce build` in Rust project -# HOW TO: Create App with FCE Modules +## HOW TO: Create App with FCE Modules -## Recommendations: +### Recommendations: - Modules architecture should be upwards from `effectors` (modules that persist data and WASI modules) that will work with local binaries, local storage and syscalls to `pure modules` that perform business logic. - Splitting app to small FCE modules are easier to support, reuse and distribute - Each module for its own task (npm like) -## Module project structure +### Module project structure - Init simple rust project `cargo init --bin` @@ -28,7 +28,7 @@ name = "wasm_application" path = "src/main.rs" [dependencies] -// logger - if you'll use logging +# logger - if you'll use logging fluence = { git = "https://github.com/fluencelabs/rust-sdk", features = ["logger"] } ``` @@ -64,7 +64,7 @@ extern "C" { } ``` -## Combine modules to Application +### Combine modules to Application - Create simple Rust project - Create `Config.toml` to describe existed wasm modules and give accesses to host binaries and local storage if needed: diff --git a/crates/fce-wit-interfaces/Cargo.toml b/crates/fce-wit-interfaces/Cargo.toml index 4b029b2b..1b0e0911 100644 --- a/crates/fce-wit-interfaces/Cargo.toml +++ b/crates/fce-wit-interfaces/Cargo.toml @@ -9,5 +9,5 @@ name = "fce_wit_interfaces" path = "src/lib.rs" [dependencies] -wasmer-wit = { package = "wasmer-interface-types", git = "https://github.com/fluencelabs/interface-types", branch = "byte_array"} +wasmer-wit = { package = "wasmer-interface-types", git = "https://github.com/fluencelabs/interface-types", branch = "struct_support"} multimap = "0.8.1" diff --git a/crates/wit-generator/src/instructions_generator/foreign_mod_instructions.rs b/crates/wit-generator/src/instructions_generator/foreign_mod_instructions.rs index f3242faf..0c753eaf 100644 --- a/crates/wit-generator/src/instructions_generator/foreign_mod_instructions.rs +++ b/crates/wit-generator/src/instructions_generator/foreign_mod_instructions.rs @@ -247,7 +247,7 @@ impl ForeignModInstructionGenerator for ParsedType { vec![ Instruction::RecordLowerMemory {type_index}, - Instruction::CallCore { function_index: SET_RESULT_SIZE_FUNC.id }, + Instruction::CallCore { function_index: SET_RESULT_PTR_FUNC.id }, ] }, }; @@ -266,13 +266,12 @@ pub fn to_raw_input_types(ty: &ParsedType) -> Vec { | ParsedType::I32 | ParsedType::U8 | ParsedType::U16 - | ParsedType::U32 => vec![WasmType::I32], + | ParsedType::U32 + | ParsedType::Record(_) => vec![WasmType::I32], ParsedType::I64 | ParsedType::U64 => vec![WasmType::I64], ParsedType::F32 => vec![WasmType::F32], ParsedType::F64 => vec![WasmType::F64], - ParsedType::Utf8String | ParsedType::ByteVector | ParsedType::Record(_) => { - vec![WasmType::I32, WasmType::I32] - } + ParsedType::Utf8String | ParsedType::ByteVector => vec![WasmType::I32, WasmType::I32], } } diff --git a/engine/src/module/wit_instance.rs b/engine/src/module/wit_instance.rs index 372e2e4a..2329c4cd 100644 --- a/engine/src/module/wit_instance.rs +++ b/engine/src/module/wit_instance.rs @@ -31,6 +31,7 @@ use std::collections::HashMap; pub(super) struct WITInstance { funcs: HashMap, memories: Vec, + record_types: HashMap, } impl WITInstance { @@ -46,7 +47,13 @@ impl WITInstance { exports.extend(imports); let funcs = exports; - Ok(Self { funcs, memories }) + let record_types = Self::extract_record_types(wit); + + Ok(Self { + funcs, + memories, + record_types, + }) } fn extract_raw_exports( @@ -113,6 +120,16 @@ impl WITInstance { memories } + + fn extract_record_types(wit: &FCEWITInterfaces<'_>) -> HashMap { + wit.types() + .enumerate() + .filter_map(|(id, ty)| match ty { + WITAstType::Record(_) => Some((id as u32, ty.clone())), + _ => None, + }) + .collect::>() + } } impl wasm::structures::Instance> @@ -138,7 +155,7 @@ impl wasm::structures::Instance Option<&WITAstType> { - None + fn wit_type(&self, index: u32) -> Option<&WITAstType> { + self.record_types.get(&index) } } diff --git a/examples/records/Cargo.toml b/examples/records/Cargo.toml new file mode 100644 index 00000000..66815b54 --- /dev/null +++ b/examples/records/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "records" +version = "0.1.0" +authors = ["Fluence Labs"] +edition = "2018" + +[dependencies] +fluence-faas = { path = "../../fluence-faas" } +anyhow = "1.0.31" +log = "0.4.11" +env_logger = "0.7.1" diff --git a/examples/records/Config.toml b/examples/records/Config.toml new file mode 100644 index 00000000..c76ac303 --- /dev/null +++ b/examples/records/Config.toml @@ -0,0 +1,11 @@ +core_modules_dir = "wasm/artifacts/wasm_modules" + +[[core_module]] + name = "effector.wasm" + mem_pages_count = 1 + logger_enabled = true + +[[core_module]] + name = "pure.wasm" + mem_pages_count = 1 + logger_enabled = true diff --git a/examples/records/src/main.rs b/examples/records/src/main.rs new file mode 100644 index 00000000..d4949c31 --- /dev/null +++ b/examples/records/src/main.rs @@ -0,0 +1,33 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use fluence_faas::FluenceFaaS; + +use std::path::PathBuf; + +const RECORDS_MODULES_CONFIG_PATH: &str = "Config.toml"; + +fn main() -> Result<(), anyhow::Error> { + env_logger::init(); + + let mut records_test = FluenceFaaS::new(PathBuf::from(RECORDS_MODULES_CONFIG_PATH))?; + println!("ipfs node interface is\n{}", records_test.get_interface()); + + let result = records_test.call_module("pure.wasm", "invoke", &[])?; + println!("execution result {:?}", result); + + Ok(()) +} diff --git a/examples/records/wasm/effector/Cargo.toml b/examples/records/wasm/effector/Cargo.toml new file mode 100644 index 00000000..49b7de64 --- /dev/null +++ b/examples/records/wasm/effector/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "effector" +version = "0.1.0" +authors = ["Fluence Labs"] +edition = "2018" + +[[bin]] +name = "effector" +path = "src/main.rs" + +[dependencies] +fluence = { git = "https://github.com/fluencelabs/rust-sdk" } diff --git a/examples/records/wasm/effector/src/main.rs b/examples/records/wasm/effector/src/main.rs new file mode 100644 index 00000000..66bb3acf --- /dev/null +++ b/examples/records/wasm/effector/src/main.rs @@ -0,0 +1,55 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use fluence::fce; + +pub fn main() {} + +#[fce] +pub struct TestRecord { + pub field_0: bool, + pub field_1: i8, + pub field_2: i16, + pub field_3: i32, + pub field_4: i64, + pub field_5: u8, + pub field_6: u16, + pub field_7: u32, + pub field_8: u64, + pub field_9: f32, + pub field_10: f64, + pub field_11: String, + pub field_12: Vec, +} + +#[fce] +pub fn mutate_struct(mut 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]; + + test_record +} diff --git a/examples/records/wasm/pure/Cargo.toml b/examples/records/wasm/pure/Cargo.toml new file mode 100644 index 00000000..82a086d0 --- /dev/null +++ b/examples/records/wasm/pure/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "pure" +version = "0.1.0" +authors = ["Fluence Labs"] +edition = "2018" + +[[bin]] +name = "pure" +path = "src/main.rs" + +[dependencies] +fluence = { git = "https://github.com/fluencelabs/rust-sdk" } diff --git a/examples/records/wasm/pure/src/main.rs b/examples/records/wasm/pure/src/main.rs new file mode 100644 index 00000000..cbfa18eb --- /dev/null +++ b/examples/records/wasm/pure/src/main.rs @@ -0,0 +1,63 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use fluence::fce; + +pub fn main() {} + +#[fce] +pub struct TestRecord { + pub field_0: bool, + pub field_1: i8, + pub field_2: i16, + pub field_3: i32, + pub field_4: i64, + pub field_5: u8, + pub field_6: u16, + pub field_7: u32, + pub field_8: u64, + pub field_9: f32, + pub field_10: f64, + pub field_11: String, + pub field_12: Vec, +} + +#[fce] +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(), + }; + + mutate_struct(test_record) +} + +#[fce] +#[link(wasm_import_module = "effector.wasm")] +extern "C" { + pub fn mutate_struct(test_record: TestRecord) -> TestRecord; +}