From a38d88a094c27f68136bc52d61e53e6ee850184e Mon Sep 17 00:00:00 2001 From: vms Date: Sun, 26 Jul 2020 21:42:39 +0300 Subject: [PATCH] pass records with only pointer without size --- crates/wit/src/parsed_type/fn_arg.rs | 7 +++---- crates/wit/src/parsed_type/fn_prolog.rs | 2 +- crates/wit/src/parsed_type/foreign_mod_arg.rs | 11 ++++++++++- .../wit/src/parsed_type/foreign_mod_epilog.rs | 5 +---- .../record_generator.rs | 7 +++++-- .../record_generator/record_deserializer.rs | 4 +--- crates/wit/src/utils.rs | 19 +++++++++++++++++++ 7 files changed, 40 insertions(+), 15 deletions(-) diff --git a/crates/wit/src/parsed_type/fn_arg.rs b/crates/wit/src/parsed_type/fn_arg.rs index 31de291..46503a8 100644 --- a/crates/wit/src/parsed_type/fn_arg.rs +++ b/crates/wit/src/parsed_type/fn_arg.rs @@ -31,13 +31,12 @@ impl FnArgGlueCodeGenerator for ParsedType { | 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/crates/wit/src/parsed_type/fn_prolog.rs b/crates/wit/src/parsed_type/fn_prolog.rs index 126b107..36d2b85 100644 --- a/crates/wit/src/parsed_type/fn_prolog.rs +++ b/crates/wit/src/parsed_type/fn_prolog.rs @@ -110,7 +110,7 @@ fn generate_type_prolog( GENERATED_RECORD_DESERIALIZER_PREFIX.to_string() + record_name ); quote! { - let #generated_arg_id = #deserializer(#ptr, #size); + let #generated_arg_id = #deserializer(#ptr); } } _ => panic!( diff --git a/crates/wit/src/parsed_type/foreign_mod_arg.rs b/crates/wit/src/parsed_type/foreign_mod_arg.rs index 850802c..27fc881 100644 --- a/crates/wit/src/parsed_type/foreign_mod_arg.rs +++ b/crates/wit/src/parsed_type/foreign_mod_arg.rs @@ -28,9 +28,18 @@ impl ForeignModArgGlueCodeGenerator for ParsedType { let arg = crate::new_ident!(format!("arg_{}", arg_start_id)); match self { - ParsedType::Utf8String | ParsedType::ByteVector | ParsedType::Record(_) => { + ParsedType::Utf8String | ParsedType::ByteVector => { quote! { #arg.as_ptr() as _, #arg.len() as _ } } + ParsedType::Record(record_name) => { + let serializer = crate::new_ident!( + crate::token_stream_generator::GENERATED_RECORD_SERIALIZER_PREFIX.to_string() + + record_name + ); + quote! { + #serializer(#arg) + } + } _ => quote! { arg }, } } diff --git a/crates/wit/src/parsed_type/foreign_mod_epilog.rs b/crates/wit/src/parsed_type/foreign_mod_epilog.rs index d8eaa73..b523f49 100644 --- a/crates/wit/src/parsed_type/foreign_mod_epilog.rs +++ b/crates/wit/src/parsed_type/foreign_mod_epilog.rs @@ -64,10 +64,7 @@ impl ForeignModEpilogGlueCodeGenerator for Option { + record_name ); quote! { - #record_deserializer( - fluence::internal::get_result_ptr() as _, - fluence::internal::get_result_size() as _, - ); + #record_deserializer(fluence::internal::get_result_ptr() as _) } } _ => panic!( diff --git a/crates/wit/src/token_stream_generator/record_generator.rs b/crates/wit/src/token_stream_generator/record_generator.rs index d846d1c..4f6e953 100644 --- a/crates/wit/src/token_stream_generator/record_generator.rs +++ b/crates/wit/src/token_stream_generator/record_generator.rs @@ -99,9 +99,12 @@ fn generate_deserializer_fn(record: &fce_ast_types::AstRecordItem) -> proc_macro return_type, } = record.generate_deserializer(&record.name); + let record_size = + crate::utils::get_record_size(record.fields.iter().map(|ast_field| &ast_field.ty)); + quote::quote! { - unsafe fn #deserializer_fn_name(offset: i32, size: i32) -> #return_type { - let raw_record: Vec = Vec::from_raw_parts(offset as _, size as _, size as _); + unsafe fn #deserializer_fn_name(offset: i32) -> #return_type { + let raw_record: Vec = Vec::from_raw_parts(offset as _, #record_size, #record_size); #deserializer diff --git a/crates/wit/src/token_stream_generator/record_generator/record_deserializer.rs b/crates/wit/src/token_stream_generator/record_generator/record_deserializer.rs index fb81df4..20d449d 100644 --- a/crates/wit/src/token_stream_generator/record_generator/record_deserializer.rs +++ b/crates/wit/src/token_stream_generator/record_generator/record_deserializer.rs @@ -118,13 +118,11 @@ impl RecordDeserializerGlueCodeGenerator for fce_ast_types::AstRecordItem { } ParsedType::Record(record_name) => { let ptr_id = value_id; - let size_id = value_id + 1; - value_id += 1; let record_deserializer = new_ident!(GENERATED_RECORD_DESERIALIZER_PREFIX.to_string() + record_name); quote! { - let #field = #record_deserializer(raw_record[#ptr_id] as _, raw_record[#size_id] as _); + let #field = #record_deserializer(raw_record[#ptr_id] as _); } } }; diff --git a/crates/wit/src/utils.rs b/crates/wit/src/utils.rs index c7bd48b..2311d95 100644 --- a/crates/wit/src/utils.rs +++ b/crates/wit/src/utils.rs @@ -39,3 +39,22 @@ macro_rules! prepare_global_data { + &$name.replace(".", "_"); }; } + +pub fn get_record_size<'a>( + fields: impl Iterator, +) -> usize { + use crate::parsed_type::ParsedType; + + let mut size = 0; + + for field in fields { + let params_count = match field { + ParsedType::ByteVector | ParsedType::Utf8String => 2, + _ => 1, + }; + + size += std::mem::size_of::() * params_count; + } + + size +}