mirror of
https://github.com/fluencelabs/marine-rs-sdk-test
synced 2024-12-04 23:30:18 +00:00
intermediate
This commit is contained in:
parent
b2002b0279
commit
12f06f806f
@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
use super::log;
|
||||
|
||||
use std::alloc::alloc as global_alloc;
|
||||
@ -25,12 +26,32 @@ use std::alloc::Layout;
|
||||
pub unsafe fn allocate(size: usize) -> usize {
|
||||
let layout = match Layout::from_size_align(size, std::mem::align_of::<u8>()) {
|
||||
Ok(layout) => layout,
|
||||
// in this case a err may occur only in a case of too long allocated size,
|
||||
// in this case a err may occur only in a case of too long allocating size,
|
||||
// so just return 0
|
||||
Err(_) => return 0,
|
||||
};
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
log(format!("sdk.allocate: {:?}\n", size));
|
||||
|
||||
global_alloc(layout) as _
|
||||
}
|
||||
|
||||
/// Allocates memory area of specified size and returns its address.
|
||||
/// Returns 0 if supplied size is too long.
|
||||
#[no_mangle]
|
||||
pub unsafe fn allocate_vec(element_count: usize, align: usize) -> usize {
|
||||
let layout = match Layout::from_size_align(size, align) {
|
||||
Ok(layout) => layout,
|
||||
// in this case a err may occur only in a case of too long allocating size, or incompatible
|
||||
// so just return 0
|
||||
Err(_) => return 0,
|
||||
};
|
||||
|
||||
let layout = layout.repeat(element_count)
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
log(format!("sdk.allocate: {:?}\n", size));
|
||||
|
||||
global_alloc(layout) as _
|
||||
}
|
||||
|
@ -18,6 +18,7 @@
|
||||
//! 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;
|
||||
@ -31,6 +32,7 @@ thread_local!(static OBJECTS_TO_RELEASE: RefCell<Vec<Box<dyn Any>>> = RefCell::n
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe fn get_result_ptr() -> usize {
|
||||
#[cfg(feature = "debug")]
|
||||
log(format!(
|
||||
"sdk.get_result_ptr, returns {}\n",
|
||||
*RESULT_PTR.get_mut()
|
||||
@ -41,6 +43,7 @@ pub unsafe fn get_result_ptr() -> usize {
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe fn get_result_size() -> usize {
|
||||
#[cfg(feature = "debug")]
|
||||
log(format!(
|
||||
"sdk.get_result_size, returns {}\n",
|
||||
*RESULT_SIZE.get_mut()
|
||||
@ -51,6 +54,7 @@ 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));
|
||||
|
||||
*RESULT_PTR.get_mut() = ptr;
|
||||
@ -58,6 +62,7 @@ pub unsafe fn set_result_ptr(ptr: usize) {
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe fn set_result_size(size: usize) {
|
||||
#[cfg(feature = "debug")]
|
||||
log(format!("sdk.set_result_size: {}\n", size));
|
||||
|
||||
*RESULT_SIZE.get_mut() = size;
|
||||
|
@ -24,8 +24,6 @@ pub(crate) fn generate_vector_serializer(
|
||||
_vec_passing_style: PassingStyle,
|
||||
arg_name: &str,
|
||||
) -> proc_macro2::TokenStream {
|
||||
let t = 0f32;
|
||||
let t = t.to_be_bytes();
|
||||
let values_serializer = match value_ty {
|
||||
ParsedType::Boolean(_) => {
|
||||
quote! {
|
||||
@ -39,39 +37,13 @@ pub(crate) fn generate_vector_serializer(
|
||||
| ParsedType::I32(_)
|
||||
| ParsedType::U32(_)
|
||||
| ParsedType::I64(_)
|
||||
| ParsedType::U64(_) => {
|
||||
| ParsedType::U64(_)
|
||||
| ParsedType::F32(_)
|
||||
| ParsedType::F64(_) => {
|
||||
quote! {
|
||||
(arg.as_ptr() as _, arg.len() as _)
|
||||
}
|
||||
}
|
||||
ParsedType::F32(_) => {
|
||||
quote! {
|
||||
let mut result: Vec<u32> = Vec::with_capacity(arg.len());
|
||||
for value in arg {
|
||||
result.push(value.to_bits());
|
||||
}
|
||||
|
||||
let result_ptr = result.as_ptr();
|
||||
let result_len = 4 * result.len();
|
||||
fluence::internal::add_object_to_release(Box::new(result));
|
||||
|
||||
(result_ptr as _, result_len as _)
|
||||
}
|
||||
}
|
||||
ParsedType::F64(_) => {
|
||||
quote! {
|
||||
let mut result: Vec<u64> = Vec::with_capacity(arg.len());
|
||||
for value in arg {
|
||||
result.push(value.to_bits());
|
||||
}
|
||||
|
||||
let result_ptr = result.as_ptr();
|
||||
let result_len = 8 * result.len();
|
||||
fluence::internal::add_object_to_release(Box::new(result));
|
||||
|
||||
(result_ptr as _, result_len as _)
|
||||
}
|
||||
}
|
||||
ParsedType::Utf8Str(_) | ParsedType::Utf8String(_) => {
|
||||
quote! {
|
||||
let mut result: Vec<u32> = Vec::with_capacity(arg.len());
|
||||
@ -82,32 +54,31 @@ pub(crate) fn generate_vector_serializer(
|
||||
}
|
||||
|
||||
let result_ptr = result.as_ptr();
|
||||
let result_len = 4 * result.len();
|
||||
let result_len = result.len();
|
||||
fluence::internal::add_object_to_release(Box::new(result));
|
||||
|
||||
(result_ptr as _, result_len as _)
|
||||
}
|
||||
}
|
||||
ParsedType::Vector(ty, passing_style) => {
|
||||
let serializer_name = format!("{}_{}", arg_name, ty);
|
||||
let serializer_name = crate::utils::prepare_ident(serializer_name);
|
||||
let serializer_ident = crate::new_ident!(serializer_name);
|
||||
let ser_name = format!("{}_{}", arg_name, ty);
|
||||
let ser_name = crate::utils::prepare_ident(ser_name);
|
||||
let ser_ident = crate::new_ident!(ser_name);
|
||||
|
||||
let inner_vector_serializer =
|
||||
generate_vector_serializer(&*ty, *passing_style, &serializer_name);
|
||||
let inner_vector_ser = generate_vector_serializer(&*ty, *passing_style, &ser_name);
|
||||
|
||||
quote! {
|
||||
#inner_vector_serializer
|
||||
#inner_vector_ser
|
||||
|
||||
let mut result: Vec<u32> = Vec::with_capacity(2 * arg.len());
|
||||
for value in arg {
|
||||
let (ptr, size) = #serializer_ident(&value);
|
||||
let (ptr, size) = #ser_ident(&value);
|
||||
result.push(ptr as _);
|
||||
result.push(size as _);
|
||||
}
|
||||
|
||||
let result_ptr = result.as_ptr();
|
||||
let result_len = 4 * result.len();
|
||||
let result_len = result.len();
|
||||
fluence::internal::add_object_to_release(Box::new(result));
|
||||
|
||||
(result_ptr as _, result_len as _)
|
||||
@ -123,7 +94,7 @@ pub(crate) fn generate_vector_serializer(
|
||||
}
|
||||
|
||||
let result_ptr = result.as_ptr();
|
||||
let result_len = 4 * result.len();
|
||||
let result_len = result.len();
|
||||
fluence::internal::add_object_to_release(Box::new(result));
|
||||
|
||||
(result_ptr as _, result_len as _)
|
||||
@ -154,14 +125,7 @@ pub(crate) fn generate_vector_deserializer(
|
||||
}
|
||||
ParsedType::F32(_) => {
|
||||
quote! {
|
||||
let mut arg: Vec<u64> = Vec::from_raw_parts(offset as _, size as _, size as _);
|
||||
let mut result = Vec::with_capacity(arg.len());
|
||||
|
||||
for value in arg {
|
||||
result.push(f32::from_bits(value as _));
|
||||
}
|
||||
|
||||
result
|
||||
Vec::from_raw_parts(offset as _, size as _, size as _)
|
||||
}
|
||||
}
|
||||
ParsedType::F64(_) => {
|
||||
|
Loading…
Reference in New Issue
Block a user