Add support for WASM transforms

This commit is contained in:
alonso.torres 2025-02-06 15:30:31 +01:00 committed by Alejandro Alonso
parent a3a757f842
commit 1bb337c3dd
18 changed files with 658 additions and 153 deletions

View file

@ -14,7 +14,20 @@ pub extern "C" fn alloc_bytes(len: usize) -> *mut u8 {
return ptr;
}
pub fn free_bytes() {
pub fn write_bytes(bytes: Vec<u8>) -> *mut u8 {
if unsafe { BUFFERU8.is_some() } {
panic!("Bytes already allocated");
}
let mut buffer = Box::new(bytes);
let ptr = buffer.as_mut_ptr();
unsafe { BUFFERU8 = Some(buffer) };
return ptr;
}
#[no_mangle]
pub extern "C" fn free_bytes() {
if unsafe { BUFFERU8.is_some() } {
let buffer = unsafe { BUFFERU8.take() }.expect("uninitialized buffer");
std::mem::drop(buffer);
@ -30,3 +43,31 @@ pub fn bytes() -> Vec<u8> {
let buffer = unsafe { BUFFERU8.take() }.expect("uninitialized buffer");
*buffer
}
pub trait SerializableResult {
type BytesType;
fn from_bytes(bytes: Self::BytesType) -> Self;
fn as_bytes(&self) -> Self::BytesType;
fn clone_to_slice(&self, slice: &mut [u8]);
}
/*
Returns an array in the heap. The first 4 bytes is always the size
of the array. Then the items are serialized one after the other
by the implementation of SerializableResult trait
*/
pub fn write_vec<T: SerializableResult>(result: Vec<T>) -> *mut u8 {
let elem_size = size_of::<T>();
let bytes_len = 4 + result.len() * elem_size;
let mut result_bytes = Vec::<u8>::with_capacity(bytes_len);
result_bytes.resize(bytes_len, 0);
result_bytes[0..4].clone_from_slice(&result.len().to_le_bytes());
for i in 0..result.len() {
let base = 4 + i * elem_size;
result[i].clone_to_slice(&mut result_bytes[base..base + elem_size]);
}
write_bytes(result_bytes)
}