From 47f0443397a5075efe7d03e0fc9028e89dd1f375 Mon Sep 17 00:00:00 2001 From: vms Date: Wed, 21 Apr 2021 14:56:21 +0300 Subject: [PATCH] pr fixes --- crates/main/src/export_allocator.rs | 33 ++++++++++++------- crates/wit/src/parsed_type/vector_utils.rs | 6 ++-- .../record_generator/field_values_builder.rs | 4 +-- .../exports/arrays/expanded.rs | 9 +++-- .../generation_tests/exports/refs/expanded.rs | 6 ++-- .../records/call_parameters/expanded.rs | 3 +- .../records/use_as_type/expanded.rs | 9 +++-- 7 files changed, 45 insertions(+), 25 deletions(-) diff --git a/crates/main/src/export_allocator.rs b/crates/main/src/export_allocator.rs index 6cd255b..f925106 100644 --- a/crates/main/src/export_allocator.rs +++ b/crates/main/src/export_allocator.rs @@ -32,21 +32,30 @@ pub unsafe fn allocate(elem_count: usize, elem_ty: usize) -> usize { allocated_mem } +macro_rules! alloc { + ($ty:ty, $elem_count:ident) => {{ + let vec = Vec::<$ty>::with_capacity($elem_count); + let offset = vec.as_ptr() as usize; + std::mem::forget(vec); + offset + }}; +} + fn allocate_impl(elem_count: usize, elem_ty: usize) -> usize { // TODO: handle OOM // Such allocation scheme is needed to deal with layout match elem_ty { - 0 => Vec::::with_capacity(elem_count).as_ptr() as usize, // for booleans - 1 => Vec::::with_capacity(elem_count).as_ptr() as usize, - 2 => Vec::::with_capacity(elem_count).as_ptr() as usize, - 3 => Vec::::with_capacity(elem_count).as_ptr() as usize, - 4 => Vec::::with_capacity(elem_count).as_ptr() as usize, - 5 => Vec::::with_capacity(elem_count).as_ptr() as usize, - 6 => Vec::::with_capacity(elem_count).as_ptr() as usize, - 7 => Vec::::with_capacity(elem_count).as_ptr() as usize, - 8 => Vec::::with_capacity(elem_count).as_ptr() as usize, - 9 => Vec::::with_capacity(elem_count).as_ptr() as usize, - 10 => Vec::::with_capacity(elem_count).as_ptr() as usize, - _ => Vec::::with_capacity(0).as_ptr() as usize, // it'll allocate 0 bytes + 0 => alloc!(u8, elem_count), // for booleans + 1 => alloc!(u8, elem_count), + 2 => alloc!(u16, elem_count), + 3 => alloc!(u32, elem_count), + 4 => alloc!(u64, elem_count), + 5 => alloc!(i8, elem_count), + 6 => alloc!(i16, elem_count), + 7 => alloc!(i32, elem_count), + 8 => alloc!(i64, elem_count), + 9 => alloc!(f32, elem_count), + 10 => alloc!(f64, elem_count), + _ => alloc!(u8, 0), // it'll allocate 0 bytes } } diff --git a/crates/wit/src/parsed_type/vector_utils.rs b/crates/wit/src/parsed_type/vector_utils.rs index 5733156..efa924a 100644 --- a/crates/wit/src/parsed_type/vector_utils.rs +++ b/crates/wit/src/parsed_type/vector_utils.rs @@ -122,7 +122,8 @@ pub(crate) fn generate_vector_deserializer( } ParsedType::Utf8Str(_) | ParsedType::Utf8String(_) => { quote! { - let mut arg: Vec = Vec::from_raw_parts(offset as _, (2 * size) as _, (2 * size) as _); + let vec_passing_size = 2; + let mut arg: Vec = Vec::from_raw_parts(offset as _, (vec_passing_size * size) as _, (vec_passing_size * size) as _); let mut arg = arg.into_iter(); let mut result = Vec::with_capacity(arg.len() / 2); @@ -145,7 +146,8 @@ pub(crate) fn generate_vector_deserializer( quote! { #inner_vector_deserializer - let mut arg: Vec = Vec::from_raw_parts(offset as _, (2 * size) as _, (2 * size) as _); + let vec_passing_size = 2; + let mut arg: Vec = Vec::from_raw_parts(offset as _, (vec_passing_size * size) as _, (vec_passing_size * size) as _); let mut result = Vec::with_capacity(arg.len()); let mut arg = arg.into_iter(); diff --git a/crates/wit/src/token_stream_generator/record_generator/field_values_builder.rs b/crates/wit/src/token_stream_generator/record_generator/field_values_builder.rs index 1bbd335..74d3d13 100644 --- a/crates/wit/src/token_stream_generator/record_generator/field_values_builder.rs +++ b/crates/wit/src/token_stream_generator/record_generator/field_values_builder.rs @@ -257,7 +257,7 @@ impl FieldValuesBuilder { }; }; - self.value_id += 2 * 4; + self.value_id += 2 * std::mem::size_of::(); result } @@ -292,7 +292,7 @@ impl FieldValuesBuilder { let #field = unsafe { #generated_der_ident(offset as _, size as _) }; }; - self.value_id += 2 * 4; + self.value_id += 2 * std::mem::size_of::(); result } diff --git a/crates/wit/tests/generation_tests/exports/arrays/expanded.rs b/crates/wit/tests/generation_tests/exports/arrays/expanded.rs index 1cff3b2..bf5303f 100644 --- a/crates/wit/tests/generation_tests/exports/arrays/expanded.rs +++ b/crates/wit/tests/generation_tests/exports/arrays/expanded.rs @@ -22,8 +22,9 @@ pub unsafe fn __fce_generated_wrapper_func_inner_arrays_1(arg_0: u32, arg_1: u32 ) -> Vec { Vec::from_raw_parts(offset as _, size as _, size as _) } + let vec_passing_size = 2; let mut arg: Vec = - Vec::from_raw_parts(offset as _, (2 * size) as _, (2 * size) as _); + Vec::from_raw_parts(offset as _, (vec_passing_size * size) as _, (vec_passing_size * size) as _); let mut result = Vec::with_capacity(arg.len()); let mut arg = arg.into_iter(); while let Some(offset) = arg.next() { @@ -36,8 +37,9 @@ pub unsafe fn __fce_generated_wrapper_func_inner_arrays_1(arg_0: u32, arg_1: u32 } result } + let vec_passing_size = 2; let mut arg: Vec = - Vec::from_raw_parts(offset as _, (2 * size) as _, (2 * size) as _); + Vec::from_raw_parts(offset as _, (vec_passing_size * size) as _, (vec_passing_size * size) as _); let mut result = Vec::with_capacity(arg.len()); let mut arg = arg.into_iter(); while let Some(offset) = arg.next() { @@ -48,7 +50,8 @@ pub unsafe fn __fce_generated_wrapper_func_inner_arrays_1(arg_0: u32, arg_1: u32 } result } - let mut arg: Vec = Vec::from_raw_parts(offset as _, (2 * size) as _, (2 * size) as _); + let vec_passing_size = 2; + let mut arg: Vec = Vec::from_raw_parts(offset as _, (vec_passing_size * size) as _, (vec_passing_size * size) as _); let mut result = Vec::with_capacity(arg.len()); let mut arg = arg.into_iter(); while let Some(offset) = arg.next() { diff --git a/crates/wit/tests/generation_tests/exports/refs/expanded.rs b/crates/wit/tests/generation_tests/exports/refs/expanded.rs index 08c5de5..09d1b92 100644 --- a/crates/wit/tests/generation_tests/exports/refs/expanded.rs +++ b/crates/wit/tests/generation_tests/exports/refs/expanded.rs @@ -9,8 +9,9 @@ pub fn test_array_refs(arg: &Vec>) -> &Vec>>> { pub unsafe fn __fce_generated_wrapper_func_test_array_refs(arg_0: u32, arg_1: u32) { unsafe fn __fce_generated_vec_deserializer_0(offset: u32, size: u32) -> Vec> { unsafe fn __fce_generated_vec_deserializer_0_String(offset: u32, size: u32) -> Vec { + let vec_passing_size = 2; let mut arg: Vec = - Vec::from_raw_parts(offset as _, (2 * size) as _, (2 * size) as _); + Vec::from_raw_parts(offset as _, (vec_passing_size * size) as _, (vec_passing_size * size) as _); let mut arg = arg.into_iter(); let mut result = Vec::with_capacity(arg.len() / 2); while let Some(offset) = arg.next() { @@ -20,7 +21,8 @@ pub unsafe fn __fce_generated_wrapper_func_test_array_refs(arg_0: u32, arg_1: u3 } result } - let mut arg: Vec = Vec::from_raw_parts(offset as _, (2 * size) as _, (2 * size) as _); + let vec_passing_size = 2; + let mut arg: Vec = Vec::from_raw_parts(offset as _, (vec_passing_size * size) as _, (vec_passing_size * size) as _); let mut result = Vec::with_capacity(arg.len()); let mut arg = arg.into_iter(); while let Some(offset) = arg.next() { diff --git a/crates/wit/tests/generation_tests/records/call_parameters/expanded.rs b/crates/wit/tests/generation_tests/records/call_parameters/expanded.rs index 214ab81..d12f3a9 100644 --- a/crates/wit/tests/generation_tests/records/call_parameters/expanded.rs +++ b/crates/wit/tests/generation_tests/records/call_parameters/expanded.rs @@ -161,8 +161,9 @@ impl CallParameters { } result } + let vec_passing_size = 2; let mut arg: Vec = - Vec::from_raw_parts(offset as _, (2 * size) as _, (2 * size) as _); + Vec::from_raw_parts(offset as _, (vec_passing_size * size) as _, (vec_passing_size * size) as _); let mut result = Vec::with_capacity(arg.len()); let mut arg = arg.into_iter(); while let Some(offset) = arg.next() { diff --git a/crates/wit/tests/generation_tests/records/use_as_type/expanded.rs b/crates/wit/tests/generation_tests/records/use_as_type/expanded.rs index dc3259f..e95c1b3 100644 --- a/crates/wit/tests/generation_tests/records/use_as_type/expanded.rs +++ b/crates/wit/tests/generation_tests/records/use_as_type/expanded.rs @@ -31,8 +31,9 @@ pub unsafe fn __fce_generated_wrapper_func_inner_arrays_2(arg_0: u32, arg_1: u32 } result } + let vec_passing_size = 2; let mut arg: Vec = - Vec::from_raw_parts(offset as _, (2 * size) as _, (2 * size) as _); + Vec::from_raw_parts(offset as _, (vec_passing_size * size) as _, (vec_passing_size * size) as _); let mut result = Vec::with_capacity(arg.len()); let mut arg = arg.into_iter(); while let Some(offset) = arg.next() { @@ -42,8 +43,9 @@ pub unsafe fn __fce_generated_wrapper_func_inner_arrays_2(arg_0: u32, arg_1: u32 } result } + let vec_passing_size = 2; let mut arg: Vec = - Vec::from_raw_parts(offset as _, (2 * size) as _, (2 * size) as _); + Vec::from_raw_parts(offset as _, (vec_passing_size * size) as _, (vec_passing_size * size) as _); let mut result = Vec::with_capacity(arg.len()); let mut arg = arg.into_iter(); while let Some(offset) = arg.next() { @@ -56,7 +58,8 @@ pub unsafe fn __fce_generated_wrapper_func_inner_arrays_2(arg_0: u32, arg_1: u32 } result } - let mut arg: Vec = Vec::from_raw_parts(offset as _, (2 * size) as _, (2 * size) as _); + let vec_passing_size = 2; + let mut arg: Vec = Vec::from_raw_parts(offset as _, (vec_passing_size * size) as _, (vec_passing_size * size) as _); let mut result = Vec::with_capacity(arg.len()); let mut arg = arg.into_iter(); while let Some(offset) = arg.next() {