add example with records

This commit is contained in:
vms 2020-07-28 01:19:15 +03:00
parent 471da75a9a
commit 0a32104cbc
13 changed files with 271 additions and 41 deletions

60
Cargo.lock generated
View File

@ -313,6 +313,13 @@ dependencies = [
"generic-array 0.14.3", "generic-array 0.14.3",
] ]
[[package]]
name = "effector"
version = "0.1.0"
dependencies = [
"fluence",
]
[[package]] [[package]]
name = "either" name = "either"
version = "1.5.3" version = "1.5.3"
@ -403,7 +410,7 @@ dependencies = [
"parity-wasm", "parity-wasm",
"pwasm-utils", "pwasm-utils",
"serde", "serde",
"wasmer-interface-types 0.17.0 (git+http://github.com/fluencelabs/interface-types?branch=struct_support)", "wasmer-interface-types",
"wasmer-runtime", "wasmer-runtime",
"wasmer-runtime-core", "wasmer-runtime-core",
"wasmer-wasi", "wasmer-wasi",
@ -415,7 +422,7 @@ name = "fce-wit-interfaces"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"multimap", "multimap",
"wasmer-interface-types 0.17.0 (git+https://github.com/fluencelabs/interface-types?branch=byte_array)", "wasmer-interface-types",
] ]
[[package]] [[package]]
@ -434,7 +441,7 @@ dependencies = [
[[package]] [[package]]
name = "fluence" name = "fluence"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/fluencelabs/rust-sdk#e589d52c7247462f194800f50595029d224a5e43" source = "git+https://github.com/fluencelabs/rust-sdk#88afe8feaa6119ebeb2032281c26f6b74f2f8fd1"
dependencies = [ dependencies = [
"fluence-sdk-macro", "fluence-sdk-macro",
"fluence-sdk-main", "fluence-sdk-main",
@ -459,7 +466,7 @@ dependencies = [
[[package]] [[package]]
name = "fluence-sdk-macro" name = "fluence-sdk-macro"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/fluencelabs/rust-sdk#e589d52c7247462f194800f50595029d224a5e43" source = "git+https://github.com/fluencelabs/rust-sdk#88afe8feaa6119ebeb2032281c26f6b74f2f8fd1"
dependencies = [ dependencies = [
"fluence-sdk-wit", "fluence-sdk-wit",
] ]
@ -467,7 +474,7 @@ dependencies = [
[[package]] [[package]]
name = "fluence-sdk-main" name = "fluence-sdk-main"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/fluencelabs/rust-sdk#e589d52c7247462f194800f50595029d224a5e43" source = "git+https://github.com/fluencelabs/rust-sdk#88afe8feaa6119ebeb2032281c26f6b74f2f8fd1"
dependencies = [ dependencies = [
"log", "log",
] ]
@ -475,7 +482,7 @@ dependencies = [
[[package]] [[package]]
name = "fluence-sdk-wit" name = "fluence-sdk-wit"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/fluencelabs/rust-sdk#e589d52c7247462f194800f50595029d224a5e43" source = "git+https://github.com/fluencelabs/rust-sdk#88afe8feaa6119ebeb2032281c26f6b74f2f8fd1"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -862,6 +869,13 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "pure"
version = "0.1.0"
dependencies = [
"fluence",
]
[[package]] [[package]]
name = "pwasm-utils" name = "pwasm-utils"
version = "0.12.0" version = "0.12.0"
@ -965,6 +979,16 @@ dependencies = [
"num_cpus", "num_cpus",
] ]
[[package]]
name = "records"
version = "0.1.0"
dependencies = [
"anyhow",
"env_logger",
"fluence-faas",
"log",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.1.57" version = "0.1.57"
@ -1078,9 +1102,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.56" version = "1.0.57"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3433e879a558dde8b5e8feb2a04899cf34fdde1fafb894687e52105fc1162ac3" checksum = "164eacbdb13512ec2745fb09d51fd5b22b0d65ed294a1dcf7285a360c80a675c"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",
@ -1113,9 +1137,9 @@ checksum = "502d53007c02d7605a05df1c1a73ee436952781653da5d0bf57ad608f66932c1"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.35" version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb7f4c519df8c117855e19dd8cc851e89eb746fe7a73f0157e0d95fdec5369b0" checksum = "4cdb98bcb1f9d81d07b536179c269ea15999b5d14ea958196413869445bb5250"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1390,17 +1414,7 @@ dependencies = [
[[package]] [[package]]
name = "wasmer-interface-types" name = "wasmer-interface-types"
version = "0.17.0" version = "0.17.0"
source = "git+https://github.com/fluencelabs/interface-types?branch=byte_array#4521e79a50e39dfa155d07e74ffc7d12203fa198" source = "git+https://github.com/fluencelabs/interface-types?branch=struct_support#eb3dbcb8353f658040621e179251b182695f04f4"
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"
dependencies = [ dependencies = [
"nom", "nom",
"safe-transmute", "safe-transmute",
@ -1541,7 +1555,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"walrus", "walrus",
"wasmer-interface-types 0.17.0 (git+http://github.com/fluencelabs/interface-types?branch=struct_support)", "wasmer-interface-types",
"wit-parser", "wit-parser",
] ]
@ -1552,6 +1566,6 @@ dependencies = [
"anyhow", "anyhow",
"fce-wit-interfaces", "fce-wit-interfaces",
"walrus", "walrus",
"wasmer-interface-types 0.17.0 (git+http://github.com/fluencelabs/interface-types?branch=struct_support)", "wasmer-interface-types",
"wasmer-runtime-core", "wasmer-runtime-core",
] ]

View File

@ -4,11 +4,14 @@ members = [
"crates/wit-generator", "crates/wit-generator",
"crates/wit-parser", "crates/wit-parser",
"engine", "engine",
"examples/greeting",
"examples/greeting/wasm/",
"examples/ipfs_node", "examples/ipfs_node",
"examples/ipfs_node/wasm/ipfs_node", "examples/ipfs_node/wasm/ipfs_node",
"examples/ipfs_node/wasm/ipfs_rpc", "examples/ipfs_node/wasm/ipfs_rpc",
"examples/greeting", "examples/records",
"examples/greeting/wasm/", "examples/records/wasm/effector",
"examples/records/wasm/pure",
"fluence-faas", "fluence-faas",
"tools/cli", "tools/cli",
] ]

View File

@ -2,22 +2,22 @@
FCE is intended to run various Wasm binaries. At now, it is in the heavily developing phase. FCE is intended to run various Wasm binaries. At now, it is in the heavily developing phase.
# Installation ## Installation
- Clone this project - Clone this project
- `cargo install --path <path-to-project/tools/cli>` - `cargo install --path <path-to-project/tools/cli>`
# Usage ## Usage
- `fce build` in Rust project - `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. - 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 - Splitting app to small FCE modules are easier to support, reuse and distribute
- Each module for its own task (npm like) - Each module for its own task (npm like)
## Module project structure ### Module project structure
- Init simple rust project `cargo init --bin` - Init simple rust project `cargo init --bin`
@ -28,7 +28,7 @@ name = "wasm_application"
path = "src/main.rs" path = "src/main.rs"
[dependencies] [dependencies]
// logger - if you'll use logging # logger - if you'll use logging
fluence = { git = "https://github.com/fluencelabs/rust-sdk", features = ["logger"] } 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 simple Rust project
- Create `Config.toml` to describe existed wasm modules and give accesses to host binaries and local storage if needed: - Create `Config.toml` to describe existed wasm modules and give accesses to host binaries and local storage if needed:

View File

@ -9,5 +9,5 @@ name = "fce_wit_interfaces"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [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" multimap = "0.8.1"

View File

@ -247,7 +247,7 @@ impl ForeignModInstructionGenerator for ParsedType {
vec![ vec![
Instruction::RecordLowerMemory {type_index}, 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<WasmType> {
| ParsedType::I32 | ParsedType::I32
| ParsedType::U8 | ParsedType::U8
| ParsedType::U16 | ParsedType::U16
| ParsedType::U32 => vec![WasmType::I32], | ParsedType::U32
| ParsedType::Record(_) => vec![WasmType::I32],
ParsedType::I64 | ParsedType::U64 => vec![WasmType::I64], ParsedType::I64 | ParsedType::U64 => vec![WasmType::I64],
ParsedType::F32 => vec![WasmType::F32], ParsedType::F32 => vec![WasmType::F32],
ParsedType::F64 => vec![WasmType::F64], ParsedType::F64 => vec![WasmType::F64],
ParsedType::Utf8String | ParsedType::ByteVector | ParsedType::Record(_) => { ParsedType::Utf8String | ParsedType::ByteVector => vec![WasmType::I32, WasmType::I32],
vec![WasmType::I32, WasmType::I32]
}
} }
} }

View File

@ -31,6 +31,7 @@ use std::collections::HashMap;
pub(super) struct WITInstance { pub(super) struct WITInstance {
funcs: HashMap<usize, WITFunction>, funcs: HashMap<usize, WITFunction>,
memories: Vec<WITMemory>, memories: Vec<WITMemory>,
record_types: HashMap<u32, WITAstType>,
} }
impl WITInstance { impl WITInstance {
@ -46,7 +47,13 @@ impl WITInstance {
exports.extend(imports); exports.extend(imports);
let funcs = exports; 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( fn extract_raw_exports(
@ -113,6 +120,16 @@ impl WITInstance {
memories memories
} }
fn extract_record_types(wit: &FCEWITInterfaces<'_>) -> HashMap<u32, WITAstType> {
wit.types()
.enumerate()
.filter_map(|(id, ty)| match ty {
WITAstType::Record(_) => Some((id as u32, ty.clone())),
_ => None,
})
.collect::<HashMap<_, _>>()
}
} }
impl wasm::structures::Instance<WITExport, WITFunction, WITMemory, WITMemoryView<'_>> impl wasm::structures::Instance<WITExport, WITFunction, WITMemory, WITMemoryView<'_>>
@ -138,7 +155,7 @@ impl wasm::structures::Instance<WITExport, WITFunction, WITMemory, WITMemoryView
} }
} }
fn wit_type(&self, _index: u32) -> Option<&WITAstType> { fn wit_type(&self, index: u32) -> Option<&WITAstType> {
None self.record_types.get(&index)
} }
} }

View File

@ -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"

View File

@ -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

View File

@ -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(())
}

View File

@ -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" }

View File

@ -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<u8>,
}
#[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
}

View File

@ -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" }

View File

@ -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<u8>,
}
#[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;
}