MCPcopy Create free account
hub / github.com/bluejekyll/wasmtime-java / store_to_args

Method store_to_args

wasmtime-jni/src/wasm_value.rs:235–294  ·  view source on GitHub ↗
(
        self,
        env: &JNIEnv<'j>,
        args: &mut Vec<Val>,
        wasm_alloc: Option<&'w WasmAlloc>,
        mut store: S,
    )

Source from the content-addressed store, hash-verified

233 }
234
235 pub fn store_to_args<'w, S: AsContextMut>(
236 self,
237 env: &JNIEnv<'j>,
238 args: &mut Vec<Val>,
239 wasm_alloc: Option<&'w WasmAlloc>,
240 mut store: S,
241 ) -> Result<Option<WasmSliceWrapper<'w>>, anyhow::Error> {
242 match self {
243 // WasmVal::ByteBuffer(buffer) => {
244 // let direct_bytes: &[u8] = env.get_direct_buffer_address(buffer)?;
245
246 // // the module might not have the memory exported
247 // let wasm_alloc = wasm_alloc
248 // .ok_or_else(|| anyhow!("no memory or allocator supplied from module"))?;
249 // let wasm_slice = wasm_alloc.alloc_bytes(direct_bytes)?;
250 // wasm_slice.store_to_args(args);
251 // return Ok(Some(wasm_slice));
252 // }
253 WasmVal::ByteArray { jarray, .. } => {
254 // This is should be safe, it's copied into while borrowed the WASM context.
255 let len = env.get_array_length(jarray)?;
256 let jbytes = env.get_byte_array_elements(jarray, ReleaseMode::CopyBack)?;
257 let byte_array: &[u8] =
258 unsafe { slice::from_raw_parts(jbytes.as_ptr() as *const u8, len as usize) };
259
260 // the module might not have the memory exported
261 let wasm_alloc = wasm_alloc
262 .ok_or_else(|| anyhow!("no memory or allocator supplied from module"))?;
263 let wasm_slice = wasm_alloc.alloc_bytes(byte_array, &mut store)?;
264
265 wasm_slice.store_to_args(args);
266 return Ok(Some(wasm_slice));
267 }
268 WasmVal::String(string) => {
269 // This is should be safe, it's copied into while borrowed the WASM context.
270
271 let jstr = env.get_string(string)?;
272 let cow = Cow::from(&jstr);
273 debug!("String from Java going to WASM: {}", cow);
274
275 let cow_bytes = cow.as_bytes();
276
277 // the module might not have the memory exported
278 let wasm_alloc = wasm_alloc
279 .ok_or_else(|| anyhow!("no memory or allocator supplied from module"))?;
280 let wasm_slice = wasm_alloc.alloc_bytes(cow_bytes, store)?;
281
282 // release the array reference, CopyBack is following the JNI guidelines
283 wasm_slice.store_to_args(args);
284 return Ok(Some(wasm_slice));
285 }
286 WasmVal::Val(val @ Val::I32(_)) => val.unwrap_i32().store_to_args(args),
287 WasmVal::Val(val @ Val::I64(_)) => val.unwrap_i64().store_to_args(args),
288 WasmVal::Val(val @ Val::F32(_)) => val.unwrap_f32().store_to_args(args),
289 WasmVal::Val(val @ Val::F64(_)) => val.unwrap_f64().store_to_args(args),
290 WasmVal::Val(v) => unimplemented!("type not yet supported as an arg: {:?}", v),
291 }
292

Calls 2

alloc_bytesMethod · 0.80
as_bytesMethod · 0.80

Tested by

no test coverage detected