mirror of
https://github.com/fluencelabs/marine.git
synced 2024-12-12 06:45:32 +00:00
add example with records
This commit is contained in:
parent
471da75a9a
commit
0a32104cbc
60
Cargo.lock
generated
60
Cargo.lock
generated
@ -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",
|
||||
]
|
||||
|
@ -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",
|
||||
]
|
||||
|
14
README.md
14
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 <path-to-project/tools/cli>`
|
||||
|
||||
# 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:
|
||||
|
@ -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"
|
||||
|
@ -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<WasmType> {
|
||||
| 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],
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,6 +31,7 @@ use std::collections::HashMap;
|
||||
pub(super) struct WITInstance {
|
||||
funcs: HashMap<usize, WITFunction>,
|
||||
memories: Vec<WITMemory>,
|
||||
record_types: HashMap<u32, WITAstType>,
|
||||
}
|
||||
|
||||
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<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<'_>>
|
||||
@ -138,7 +155,7 @@ impl wasm::structures::Instance<WITExport, WITFunction, WITMemory, WITMemoryView
|
||||
}
|
||||
}
|
||||
|
||||
fn wit_type(&self, _index: u32) -> Option<&WITAstType> {
|
||||
None
|
||||
fn wit_type(&self, index: u32) -> Option<&WITAstType> {
|
||||
self.record_types.get(&index)
|
||||
}
|
||||
}
|
||||
|
11
examples/records/Cargo.toml
Normal file
11
examples/records/Cargo.toml
Normal 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"
|
11
examples/records/Config.toml
Normal file
11
examples/records/Config.toml
Normal 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
|
33
examples/records/src/main.rs
Normal file
33
examples/records/src/main.rs
Normal 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(())
|
||||
}
|
12
examples/records/wasm/effector/Cargo.toml
Normal file
12
examples/records/wasm/effector/Cargo.toml
Normal 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" }
|
55
examples/records/wasm/effector/src/main.rs
Normal file
55
examples/records/wasm/effector/src/main.rs
Normal 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
|
||||
}
|
12
examples/records/wasm/pure/Cargo.toml
Normal file
12
examples/records/wasm/pure/Cargo.toml
Normal 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" }
|
63
examples/records/wasm/pure/src/main.rs
Normal file
63
examples/records/wasm/pure/src/main.rs
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user