(&mut self,spec: &str,expected_hash: Option<[u8; 32]>)
| 27 | } |
| 28 | |
| 29 | fn fetch_bytes(&mut self,spec: &str,expected_hash: Option<[u8; 32]>) -> Result<Vec<u8>, String> { |
| 30 | let mut len: u32 = 0; |
| 31 | let hash_ptr = expected_hash.as_ref().map(|h| h.as_ptr()).unwrap_or(core::ptr::null()); |
| 32 | let ptr = unsafe { |
| 33 | host_fetch_bytes(spec.as_ptr(), spec.len() as u32, hash_ptr, &mut len as *mut u32) |
| 34 | }; |
| 35 | if ptr.is_null() { |
| 36 | return Err(s!("no bytes cached by host for '", str spec, "'")); |
| 37 | } |
| 38 | // Host allocates via `wasm_alloc` (wasm-abi.md): copy into a guest Vec, then `wasm_free`. `Vec::from_raw_parts` would UB by freeing Box-laid memory through Vec's layout. |
| 39 | let len = len as usize; |
| 40 | let bytes: Vec<u8> = unsafe { core::slice::from_raw_parts(ptr, len) }.to_vec(); |
| 41 | unsafe { wasm_free(ptr, len as u32) }; |
| 42 | Ok(bytes) |
| 43 | } |
| 44 | |
| 45 | fn child(&self, spec: &str) -> Box<dyn Resolver> { |
| 46 | Box::new(WasmHostResolver { dir: dir_of(spec).to_string() }) |
no test coverage detected