/// A prepared and sufficiently large memory Region is expected at ptr that points to pre-allocated memory.
///
/// Returns number of bytes written on success.
A prepared and sufficiently large memory Region is expected at ptr that points to pre-allocated memory.
Returns number of bytes written on success.
A prepared and sufficiently large memory Region is expected at ptr that points to pre - allocated memory . Returns number of bytes written on success .
pub fn write_region(ctx: &Ctx, ptr: u32, data: &[u8]) -> VmResult<()> {
    let mut region = get_region(ctx, ptr);
    let region_capacity = region.capacity as usize;
    if data.len() > region_capacity {
        return Err(VmError::region_too_small(region_capacity, data.len()));
    }
    let memory = ctx.memory(0);
    match WasmPtr::<u8, Array>::new(region.offset).deref(memory, 0, region.capacity) {
        Some(cells) => {
            // In case you want to do some premature optimization, this shows how to cast a `&'mut [Cell<u8>]` to `&mut [u8]`:
            // https://github.com/wasmerio/wasmer/blob/0.13.1/lib/wasi/src/syscalls/mod.rs#L79-L81
            for i in 0..data.len() {
                cells[i].set(data[i])
            }
            region.length = data.len() as u32;
            set_region(ctx, ptr, region);
            Ok(())
        },
        None => panic!(
            "Error dereferencing region {:?} in wasm memory of size {}. This typically happens when the given pointer does not point to a Region struct.",
            region,
            memory.size().bytes().0
        ),
    }
}
pub fn write_region ( ctx : & Ctx , ptr : u32 , data : & [ u8 ] ) -> VmResult < ( ) > { let mut region = get_region ( ctx , ptr ) ; let region_capacity = region . capacity as usize ; if data . len ( ) > region_capacity { return Err ( VmError :: region_too_small ( region_capacity , data . len ( ) ) ) ; } let memory = ctx . memory ( 0 ) ; match WasmPtr :: < u8 , Array > :: new ( region . offset ) . deref ( memory , 0 , region . capacity ) { Some ( cells ) => { // In case you want to do some premature optimization, this shows how to cast a `&'mut [Cell<u8>]` to `&mut [u8]`: // https://github.com/wasmerio/wasmer/blob/0.13.1/lib/wasi/src/syscalls/mod.rs#L79-L81 for i in 0 .. data . len ( ) { cells [ i ] . set ( data [ i ] ) } region . length = data . len ( ) as u32 ; set_region ( ctx , ptr , region ) ; Ok ( ( ) ) } , None => panic ! ( " " region memory size ( ) bytes ( ) 0 ) , } }
A prepared and sufficiently large memory Region is expected at ptr that points to pre-allocated memory.
A prepared and sufficiently large memory Region is expected at ptr that points to pre - allocated memory .
ctx
&Ctx
ptr
u32
data
&[u8]