Allocates the bytes from the src bytes
(
&self,
src: &[u8],
mut store: impl AsContextMut,
)
| 89 | |
| 90 | /// Allocates the bytes from the src bytes |
| 91 | pub fn alloc_bytes( |
| 92 | &self, |
| 93 | src: &[u8], |
| 94 | mut store: impl AsContextMut, |
| 95 | ) -> Result<WasmSliceWrapper<'_>, Error> { |
| 96 | let mem_base = self.memory.data_ptr(&mut store) as usize; |
| 97 | let mem_size = self.memory.size(&mut store) as usize * MEM_SEGMENT_SIZE; |
| 98 | |
| 99 | // TODO: check if we need to grow the underlying memory with `grow`, etc. |
| 100 | ensure!( |
| 101 | mem_size > src.len(), |
| 102 | "memory is {} need {} more", |
| 103 | mem_size, |
| 104 | src.len() |
| 105 | ); |
| 106 | |
| 107 | // get target memory location and then copy into the function |
| 108 | let wasm_slice = unsafe { self.alloc_size(src.len(), &mut store)? }; |
| 109 | let mem_bytes = unsafe { self.as_mut(wasm_slice.wasm_slice, &mut store) }; |
| 110 | mem_bytes.copy_from_slice(src); |
| 111 | |
| 112 | debug!( |
| 113 | "copied bytes into mem: {:x?}, mem_base: {:x?} mem_bytes: {:x?}", |
| 114 | mem_bytes, |
| 115 | mem_base, |
| 116 | mem_bytes.as_ptr(), |
| 117 | ); |
| 118 | |
| 119 | Ok(wasm_slice) |
| 120 | } |
| 121 | |
| 122 | /// Deallocate the WasmSlice |
| 123 | pub fn dealloc_bytes( |
no test coverage detected