| 53 | } |
| 54 | |
| 55 | unsafe fn create_function( |
| 56 | store: &mut wasm_store_t, |
| 57 | ty: &wasm_functype_t, |
| 58 | func: impl Fn(*const wasm_val_vec_t, *mut wasm_val_vec_t) -> Option<Box<wasm_trap_t>> |
| 59 | + Send |
| 60 | + Sync |
| 61 | + 'static, |
| 62 | ) -> Box<wasm_func_t> { |
| 63 | let ty = ty.ty().ty(store.store.context().engine()); |
| 64 | let func = Func::new( |
| 65 | store.store.context_mut(), |
| 66 | ty, |
| 67 | move |_caller, params, results| { |
| 68 | let params: wasm_val_vec_t = params |
| 69 | .iter() |
| 70 | .cloned() |
| 71 | .map(|p| wasm_val_t::from_val(p)) |
| 72 | .collect::<Vec<_>>() |
| 73 | .into(); |
| 74 | let mut out_results: wasm_val_vec_t = vec![wasm_val_t::default(); results.len()].into(); |
| 75 | let out = func(¶ms, &mut out_results); |
| 76 | if let Some(trap) = out { |
| 77 | return Err(trap.error); |
| 78 | } |
| 79 | |
| 80 | let out_results = out_results.as_slice(); |
| 81 | for i in 0..results.len() { |
| 82 | results[i] = out_results[i].val(); |
| 83 | } |
| 84 | Ok(()) |
| 85 | }, |
| 86 | ); |
| 87 | Box::new(wasm_func_t { |
| 88 | ext: wasm_extern_t { |
| 89 | store: store.store.clone(), |
| 90 | which: func.into(), |
| 91 | }, |
| 92 | }) |
| 93 | } |
| 94 | |
| 95 | #[unsafe(no_mangle)] |
| 96 | pub unsafe extern "C" fn wasm_func_new( |