From 17115b696a55defa7a579c4d635e788ab909b911 Mon Sep 17 00:00:00 2001 From: vms Date: Thu, 22 Apr 2021 17:45:06 +0300 Subject: [PATCH] pr fixes --- crates/main/src/export_allocator.rs | 49 +++++++++----------- crates/main/src/lib.rs | 20 ++++---- crates/main/src/result.rs | 15 ++---- crates/wit/src/ast_types.rs | 11 +++-- crates/wit/src/parsed_type/vector_ser_der.rs | 2 +- 5 files changed, 46 insertions(+), 51 deletions(-) diff --git a/crates/main/src/export_allocator.rs b/crates/main/src/export_allocator.rs index 9bbb6dc..3facb54 100644 --- a/crates/main/src/export_allocator.rs +++ b/crates/main/src/export_allocator.rs @@ -14,9 +14,6 @@ * limitations under the License. */ -#[cfg(feature = "debug")] -use super::log; - /// Allocates memory area of specified size and type and returns its address. /// The allocated memory region is intended to be use as a Vec. #[no_mangle] @@ -27,8 +24,7 @@ pub unsafe fn allocate(elem_count: usize, elem_ty: usize) -> usize { } let allocated_mem = allocate_impl(elem_count, elem_ty); - #[cfg(feature = "debug")] - log(format!( + crate::debug_log!(format!( "sdk.allocate: {} {} -> {}\n", elem_count, elem_ty, allocated_mem )); @@ -36,30 +32,29 @@ pub unsafe fn allocate(elem_count: usize, elem_ty: usize) -> usize { allocated_mem } -macro_rules! alloc { - ($ty:ty, $elem_count:expr) => {{ - 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 Vec layout match elem_ty { - 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), + 0 => allocate_vec::(elem_count), // for booleans + 1 => allocate_vec::(elem_count), + 2 => allocate_vec::(elem_count), + 3 => allocate_vec::(elem_count), + 4 => allocate_vec::(elem_count), + 5 => allocate_vec::(elem_count), + 6 => allocate_vec::(elem_count), + 7 => allocate_vec::(elem_count), + 8 => allocate_vec::(elem_count), + 9 => allocate_vec::(elem_count), + 10 => allocate_vec::(elem_count), _ => 0, } } + +fn allocate_vec(count: usize) -> usize { + // TODO: handle OOM + // This allocation scheme with vectors is needed to deal with internal Vec layout + let vec = Vec::::with_capacity(count); + let offset = vec.as_ptr() as usize; + std::mem::forget(vec); + + offset +} diff --git a/crates/main/src/lib.rs b/crates/main/src/lib.rs index 5535eaf..31f4a01 100644 --- a/crates/main/src/lib.rs +++ b/crates/main/src/lib.rs @@ -57,12 +57,16 @@ pub use result::add_object_to_release; pub use module_manifest::MANIFEST_SECTION_NAME; pub use sdk_version_embedder::VERSION_SECTION_NAME; -// logs will be printed only if debug feature is enabled -#[cfg(feature = "debug")] -#[allow(unused_variables)] -pub(crate) fn log>(msg: S) { - let level = log::Level::Info as i32; - let target = 0i32; - let msg = msg.as_ref(); - logger::log_utf8_string(level, target, msg.as_ptr() as i32, msg.len() as i32); +// these logs will be printed only if debug feature is enabled +#[macro_export] +macro_rules! debug_log { + ($msg_generator:expr) => { + #[cfg(feature = "debug")] + { + let level = log::Level::Info as i32; + let target = 0i32; + let msg = $msg_generator; + logger::log_utf8_string(level, target, msg.as_ptr() as i32, msg.len() as i32); + } + }; } diff --git a/crates/main/src/result.rs b/crates/main/src/result.rs index 3e61cad..b207b79 100644 --- a/crates/main/src/result.rs +++ b/crates/main/src/result.rs @@ -18,9 +18,6 @@ //! by two global variables that contain pointer and size. Will be refactored after multi-value //! support in Wasmer. -#[cfg(feature = "debug")] -use super::log; - use std::sync::atomic::AtomicUsize; use std::cell::RefCell; use std::any::Any; @@ -32,8 +29,7 @@ thread_local!(static OBJECTS_TO_RELEASE: RefCell>> = RefCell::n #[no_mangle] pub unsafe fn get_result_ptr() -> usize { - #[cfg(feature = "debug")] - log(format!( + crate::debug_log!(format!( "sdk.get_result_ptr, returns {}\n", *RESULT_PTR.get_mut() )); @@ -43,8 +39,7 @@ pub unsafe fn get_result_ptr() -> usize { #[no_mangle] pub unsafe fn get_result_size() -> usize { - #[cfg(feature = "debug")] - log(format!( + crate::debug_log!(format!( "sdk.get_result_size, returns {}\n", *RESULT_SIZE.get_mut() )); @@ -54,16 +49,14 @@ pub unsafe fn get_result_size() -> usize { #[no_mangle] pub unsafe fn set_result_ptr(ptr: usize) { - #[cfg(feature = "debug")] - log(format!("sdk.set_result_ptr: {}\n", ptr)); + crate::debug_log!(format!("sdk.set_result_ptr: {}\n", ptr)); *RESULT_PTR.get_mut() = ptr; } #[no_mangle] pub unsafe fn set_result_size(size: usize) { - #[cfg(feature = "debug")] - log(format!("sdk.set_result_size: {}\n", size)); + crate::debug_log!(format!("sdk.set_result_size: {}\n", size)); *RESULT_SIZE.get_mut() = size; } diff --git a/crates/wit/src/ast_types.rs b/crates/wit/src/ast_types.rs index c47c1fe..b5657ae 100644 --- a/crates/wit/src/ast_types.rs +++ b/crates/wit/src/ast_types.rs @@ -27,8 +27,8 @@ pub(crate) struct AstFnSignature { pub visibility: syn::Visibility, pub name: String, pub arguments: Vec, - // fce supports only one return value now, - // waiting for adding multi-value support in Wasmer. + // only one or zero return values are supported now, + // waiting for adding multi-value support in Wasmer pub output_type: Option, } @@ -39,19 +39,22 @@ pub(crate) struct AstRecordItem { pub original: syn::ItemStruct, } -#[allow(dead_code)] // at the moment tuple and unit structs aren't supported #[derive(Debug, Clone, PartialEq)] pub(crate) enum AstRecordFields { Named(Vec), + // named and unnamed variants have the same inner field types because of it's easy to handle it, // for additional info look at https://github.com/dtolnay/syn/issues/698 + #[allow(dead_code)] // at the moment tuple and unit structs aren't supported Unnamed(Vec), + + #[allow(dead_code)] // at the moment tuple and unit structs aren't supported Unit, } #[derive(Debug, Clone, PartialEq)] pub(crate) struct AstRecordField { - // fields of tuple structs haven't got name + /// Name of the field. Can be `None` for tuples. pub name: Option, pub ty: ParsedType, } diff --git a/crates/wit/src/parsed_type/vector_ser_der.rs b/crates/wit/src/parsed_type/vector_ser_der.rs index 9d0de05..4035a30 100644 --- a/crates/wit/src/parsed_type/vector_ser_der.rs +++ b/crates/wit/src/parsed_type/vector_ser_der.rs @@ -34,7 +34,7 @@ pub(crate) fn generate_vector_ser( fluence::internal::add_object_to_release(Box::new(converted_bool_vector)); (converted_bool_vector.as_ptr() as _, converted_bool_vector.len() as _) } - }, + } ParsedType::I8(_) | ParsedType::U8(_) | ParsedType::I16(_)