diff --git a/Cargo.lock b/Cargo.lock index a56e2dbf..3b1b6b6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -598,7 +598,7 @@ dependencies = [ [[package]] name = "fce-wit-generator" -version = "0.1.3" +version = "0.1.4" dependencies = [ "fce-wit-parser", "fluence-sdk-wit 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -630,7 +630,7 @@ dependencies = [ [[package]] name = "fcli" -version = "0.1.4" +version = "0.1.5" dependencies = [ "anyhow", "clap", diff --git a/crates/wit-generator/Cargo.toml b/crates/wit-generator/Cargo.toml index ccf02df4..f508abfd 100644 --- a/crates/wit-generator/Cargo.toml +++ b/crates/wit-generator/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "fce-wit-generator" description = "Fluence FCE interface type helper crate" -version = "0.1.3" +version = "0.1.4" authors = ["Fluence Labs"] license = "Apache-2.0" edition = "2018" diff --git a/crates/wit-generator/src/instructions_generator/fn_instructions.rs b/crates/wit-generator/src/instructions_generator/fn_instructions.rs index 5deaf918..c8a227e8 100644 --- a/crates/wit-generator/src/instructions_generator/fn_instructions.rs +++ b/crates/wit-generator/src/instructions_generator/fn_instructions.rs @@ -58,19 +58,18 @@ impl WITGenerator for AstFunctionItem { function_type: export_idx, }); - // TODO: rewrite with try_fold let mut instructions = self .signature .input_types .iter() .enumerate() - .map(|(id, input_type)| { - input_type.generate_instructions_for_input_type(id as _, wit_resolver) - }) - .collect::>>()? - .into_iter() - .flatten() - .collect::>(); + .try_fold::<_, _, Result<_>>(Vec::new(), |mut instructions, (arg_id, input_type)| { + let mut new_instructions = + input_type.generate_instructions_for_input_type(arg_id as _, wit_resolver)?; + + instructions.append(&mut new_instructions); + Ok(instructions) + })?; let export_function_index = (wit_resolver.interfaces.exports.len() - 1) as u32; instructions.push(Instruction::CallCore { 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 fa217889..1141c60a 100644 --- a/crates/wit-generator/src/instructions_generator/foreign_mod_instructions.rs +++ b/crates/wit-generator/src/instructions_generator/foreign_mod_instructions.rs @@ -114,18 +114,18 @@ fn generate_wit_for_import<'a>( function_type: raw_import_idx, }); - let mut instructions: Vec = import + let mut instructions = import .signature .input_types .iter() - .enumerate() - .map(|(id, input_type)| { - input_type.generate_instructions_for_input_type(id as _, wit_resolver) - }) - .collect::>>()? - .into_iter() - .flatten() - .collect(); + .try_fold::<_, _, Result<_>>((0, Vec::new()), |(arg_id, mut instructions), input_type| { + let (mut new_instructions, shift) = + input_type.generate_instructions_for_input_type(arg_id as _, wit_resolver)?; + + instructions.append(&mut new_instructions); + Ok((arg_id + shift, instructions)) + })? + .1; // TODO: refactor let import_function_index = (wit_resolver.interfaces.exports.len() @@ -161,7 +161,7 @@ trait ForeignModInstructionGenerator { &self, arg_id: u32, wit_resolver: &mut WITResolver<'a>, - ) -> Result>; + ) -> Result<(Vec, u32)>; fn generate_instructions_for_output_type<'a>( &self, @@ -169,41 +169,42 @@ trait ForeignModInstructionGenerator { ) -> Result>; } +#[rustfmt::skip] impl ForeignModInstructionGenerator for ParsedType { fn generate_instructions_for_input_type<'a>( &self, index: u32, wit_resolver: &mut WITResolver<'a>, - ) -> Result> { + ) -> Result<(Vec, u32)> { let instructions = match self { - ParsedType::Boolean => vec![Instruction::ArgumentGet { index }], - ParsedType::I8 => vec![Instruction::ArgumentGet { index }, Instruction::S8FromI32], - ParsedType::I16 => vec![Instruction::ArgumentGet { index }, Instruction::S16FromI32], - ParsedType::I32 => vec![Instruction::ArgumentGet { index }], - ParsedType::I64 => vec![Instruction::ArgumentGet { index }], - ParsedType::U8 => vec![Instruction::ArgumentGet { index }, Instruction::U8FromI32], - ParsedType::U16 => vec![Instruction::ArgumentGet { index }, Instruction::U16FromI32], - ParsedType::U32 => vec![Instruction::ArgumentGet { index }, Instruction::U32FromI32], - ParsedType::U64 => vec![Instruction::ArgumentGet { index }, Instruction::U64FromI64], - ParsedType::F32 => vec![Instruction::ArgumentGet { index }], - ParsedType::F64 => vec![Instruction::ArgumentGet { index }], - ParsedType::Utf8String => vec![ + ParsedType::Boolean => (vec![Instruction::ArgumentGet { index }], 1), + ParsedType::I8 => (vec![Instruction::ArgumentGet { index }, Instruction::S8FromI32], 1), + ParsedType::I16 => (vec![Instruction::ArgumentGet { index }, Instruction::S16FromI32], 1), + ParsedType::I32 => (vec![Instruction::ArgumentGet { index }], 1), + ParsedType::I64 => (vec![Instruction::ArgumentGet { index }], 1), + ParsedType::U8 => (vec![Instruction::ArgumentGet { index }, Instruction::U8FromI32], 1), + ParsedType::U16 => (vec![Instruction::ArgumentGet { index }, Instruction::U16FromI32], 1), + ParsedType::U32 => (vec![Instruction::ArgumentGet { index }, Instruction::U32FromI32], 1), + ParsedType::U64 => (vec![Instruction::ArgumentGet { index }, Instruction::U64FromI64], 1), + ParsedType::F32 => (vec![Instruction::ArgumentGet { index }], 1), + ParsedType::F64 => (vec![Instruction::ArgumentGet { index }], 1), + ParsedType::Utf8String => (vec![ Instruction::ArgumentGet { index }, Instruction::ArgumentGet { index: index + 1 }, Instruction::StringLiftMemory, - ], - ParsedType::ByteVector => vec![ + ], 2), + ParsedType::ByteVector => (vec![ Instruction::ArgumentGet { index }, Instruction::ArgumentGet { index: index + 1 }, Instruction::ByteArrayLiftMemory, - ], + ], 2), ParsedType::Record(record_name) => { let type_index = wit_resolver.get_record_type_id(record_name)?; - vec![ + (vec![ Instruction::ArgumentGet { index }, Instruction::RecordLiftMemory { type_index }, - ] + ], 1) } }; diff --git a/tools/cli/Cargo.toml b/tools/cli/Cargo.toml index 5c85e7a6..828401bc 100644 --- a/tools/cli/Cargo.toml +++ b/tools/cli/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "fcli" description = "Fluence FCE command line tool" -version = "0.1.4" +version = "0.1.5" authors = ["Fluence Labs"] repository = "https://github.com/fluencelabs/fce/tools/cli" license = "Apache-2.0" @@ -12,7 +12,7 @@ name = "fce" path = "src/main.rs" [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" } anyhow = "1.0.31"