optimize with try_fold; fix bug in wit-generator

This commit is contained in:
vms 2020-09-09 00:20:39 +03:00
parent 709dc36550
commit f33ef7c84b
5 changed files with 41 additions and 41 deletions

4
Cargo.lock generated
View File

@ -598,7 +598,7 @@ dependencies = [
[[package]] [[package]]
name = "fce-wit-generator" name = "fce-wit-generator"
version = "0.1.3" version = "0.1.4"
dependencies = [ dependencies = [
"fce-wit-parser", "fce-wit-parser",
"fluence-sdk-wit 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "fluence-sdk-wit 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -630,7 +630,7 @@ dependencies = [
[[package]] [[package]]
name = "fcli" name = "fcli"
version = "0.1.4" version = "0.1.5"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",

View File

@ -1,7 +1,7 @@
[package] [package]
name = "fce-wit-generator" name = "fce-wit-generator"
description = "Fluence FCE interface type helper crate" description = "Fluence FCE interface type helper crate"
version = "0.1.3" version = "0.1.4"
authors = ["Fluence Labs"] authors = ["Fluence Labs"]
license = "Apache-2.0" license = "Apache-2.0"
edition = "2018" edition = "2018"

View File

@ -58,19 +58,18 @@ impl WITGenerator for AstFunctionItem {
function_type: export_idx, function_type: export_idx,
}); });
// TODO: rewrite with try_fold
let mut instructions = self let mut instructions = self
.signature .signature
.input_types .input_types
.iter() .iter()
.enumerate() .enumerate()
.map(|(id, input_type)| { .try_fold::<_, _, Result<_>>(Vec::new(), |mut instructions, (arg_id, input_type)| {
input_type.generate_instructions_for_input_type(id as _, wit_resolver) let mut new_instructions =
}) input_type.generate_instructions_for_input_type(arg_id as _, wit_resolver)?;
.collect::<Result<Vec<_>>>()?
.into_iter() instructions.append(&mut new_instructions);
.flatten() Ok(instructions)
.collect::<Vec<Instruction>>(); })?;
let export_function_index = (wit_resolver.interfaces.exports.len() - 1) as u32; let export_function_index = (wit_resolver.interfaces.exports.len() - 1) as u32;
instructions.push(Instruction::CallCore { instructions.push(Instruction::CallCore {

View File

@ -114,18 +114,18 @@ fn generate_wit_for_import<'a>(
function_type: raw_import_idx, function_type: raw_import_idx,
}); });
let mut instructions: Vec<Instruction> = import let mut instructions = import
.signature .signature
.input_types .input_types
.iter() .iter()
.enumerate() .try_fold::<_, _, Result<_>>((0, Vec::new()), |(arg_id, mut instructions), input_type| {
.map(|(id, input_type)| { let (mut new_instructions, shift) =
input_type.generate_instructions_for_input_type(id as _, wit_resolver) input_type.generate_instructions_for_input_type(arg_id as _, wit_resolver)?;
})
.collect::<Result<Vec<_>>>()? instructions.append(&mut new_instructions);
.into_iter() Ok((arg_id + shift, instructions))
.flatten() })?
.collect(); .1;
// TODO: refactor // TODO: refactor
let import_function_index = (wit_resolver.interfaces.exports.len() let import_function_index = (wit_resolver.interfaces.exports.len()
@ -161,7 +161,7 @@ trait ForeignModInstructionGenerator {
&self, &self,
arg_id: u32, arg_id: u32,
wit_resolver: &mut WITResolver<'a>, wit_resolver: &mut WITResolver<'a>,
) -> Result<Vec<Instruction>>; ) -> Result<(Vec<Instruction>, u32)>;
fn generate_instructions_for_output_type<'a>( fn generate_instructions_for_output_type<'a>(
&self, &self,
@ -169,41 +169,42 @@ trait ForeignModInstructionGenerator {
) -> Result<Vec<Instruction>>; ) -> Result<Vec<Instruction>>;
} }
#[rustfmt::skip]
impl ForeignModInstructionGenerator for ParsedType { impl ForeignModInstructionGenerator for ParsedType {
fn generate_instructions_for_input_type<'a>( fn generate_instructions_for_input_type<'a>(
&self, &self,
index: u32, index: u32,
wit_resolver: &mut WITResolver<'a>, wit_resolver: &mut WITResolver<'a>,
) -> Result<Vec<Instruction>> { ) -> Result<(Vec<Instruction>, u32)> {
let instructions = match self { let instructions = match self {
ParsedType::Boolean => vec![Instruction::ArgumentGet { index }], ParsedType::Boolean => (vec![Instruction::ArgumentGet { index }], 1),
ParsedType::I8 => vec![Instruction::ArgumentGet { index }, Instruction::S8FromI32], ParsedType::I8 => (vec![Instruction::ArgumentGet { index }, Instruction::S8FromI32], 1),
ParsedType::I16 => vec![Instruction::ArgumentGet { index }, Instruction::S16FromI32], ParsedType::I16 => (vec![Instruction::ArgumentGet { index }, Instruction::S16FromI32], 1),
ParsedType::I32 => vec![Instruction::ArgumentGet { index }], ParsedType::I32 => (vec![Instruction::ArgumentGet { index }], 1),
ParsedType::I64 => vec![Instruction::ArgumentGet { index }], ParsedType::I64 => (vec![Instruction::ArgumentGet { index }], 1),
ParsedType::U8 => vec![Instruction::ArgumentGet { index }, Instruction::U8FromI32], ParsedType::U8 => (vec![Instruction::ArgumentGet { index }, Instruction::U8FromI32], 1),
ParsedType::U16 => vec![Instruction::ArgumentGet { index }, Instruction::U16FromI32], ParsedType::U16 => (vec![Instruction::ArgumentGet { index }, Instruction::U16FromI32], 1),
ParsedType::U32 => vec![Instruction::ArgumentGet { index }, Instruction::U32FromI32], ParsedType::U32 => (vec![Instruction::ArgumentGet { index }, Instruction::U32FromI32], 1),
ParsedType::U64 => vec![Instruction::ArgumentGet { index }, Instruction::U64FromI64], ParsedType::U64 => (vec![Instruction::ArgumentGet { index }, Instruction::U64FromI64], 1),
ParsedType::F32 => vec![Instruction::ArgumentGet { index }], ParsedType::F32 => (vec![Instruction::ArgumentGet { index }], 1),
ParsedType::F64 => vec![Instruction::ArgumentGet { index }], ParsedType::F64 => (vec![Instruction::ArgumentGet { index }], 1),
ParsedType::Utf8String => vec![ ParsedType::Utf8String => (vec![
Instruction::ArgumentGet { index }, Instruction::ArgumentGet { index },
Instruction::ArgumentGet { index: index + 1 }, Instruction::ArgumentGet { index: index + 1 },
Instruction::StringLiftMemory, Instruction::StringLiftMemory,
], ], 2),
ParsedType::ByteVector => vec![ ParsedType::ByteVector => (vec![
Instruction::ArgumentGet { index }, Instruction::ArgumentGet { index },
Instruction::ArgumentGet { index: index + 1 }, Instruction::ArgumentGet { index: index + 1 },
Instruction::ByteArrayLiftMemory, Instruction::ByteArrayLiftMemory,
], ], 2),
ParsedType::Record(record_name) => { ParsedType::Record(record_name) => {
let type_index = wit_resolver.get_record_type_id(record_name)?; let type_index = wit_resolver.get_record_type_id(record_name)?;
vec![ (vec![
Instruction::ArgumentGet { index }, Instruction::ArgumentGet { index },
Instruction::RecordLiftMemory { type_index }, Instruction::RecordLiftMemory { type_index },
] ], 1)
} }
}; };

View File

@ -1,7 +1,7 @@
[package] [package]
name = "fcli" name = "fcli"
description = "Fluence FCE command line tool" description = "Fluence FCE command line tool"
version = "0.1.4" version = "0.1.5"
authors = ["Fluence Labs"] authors = ["Fluence Labs"]
repository = "https://github.com/fluencelabs/fce/tools/cli" repository = "https://github.com/fluencelabs/fce/tools/cli"
license = "Apache-2.0" license = "Apache-2.0"
@ -12,7 +12,7 @@ name = "fce"
path = "src/main.rs" path = "src/main.rs"
[dependencies] [dependencies]
fce-wit-generator = { path = "../../crates/wit-generator", version = "0.1.3"} fce-wit-generator = { path = "../../crates/wit-generator", version = "0.1.4"}
fce-wit-parser = { path = "../../crates/wit-parser", version = "0.1.3" } fce-wit-parser = { path = "../../crates/wit-parser", version = "0.1.3" }
anyhow = "1.0.31" anyhow = "1.0.31"