Translate from a `script::Value` to a `RuntimeValue`.
(ctx: &mut WastContext, v: &CoreConst)
| 5 | |
| 6 | /// Translate from a `script::Value` to a `RuntimeValue`. |
| 7 | pub fn val(ctx: &mut WastContext, v: &CoreConst) -> Result<Val> { |
| 8 | use CoreConst::*; |
| 9 | |
| 10 | Ok(match v { |
| 11 | I32 { value } => Val::I32(value.0), |
| 12 | I64 { value } => Val::I64(value.0), |
| 13 | F32 { value } => Val::F32(value.to_bits()), |
| 14 | F64 { value } => Val::F64(value.to_bits()), |
| 15 | V128(value) => Val::V128(value.to_u128().into()), |
| 16 | FuncRef { |
| 17 | value: None | Some(json_from_wast::FuncRef::Null), |
| 18 | } => Val::FuncRef(None), |
| 19 | |
| 20 | ExternRef { |
| 21 | value: None | Some(json_from_wast::ExternRef::Null), |
| 22 | } => Val::ExternRef(None), |
| 23 | ExternRef { |
| 24 | value: Some(json_from_wast::ExternRef::Host(x)), |
| 25 | } => Val::ExternRef(if let Some(rt) = ctx.async_runtime.as_ref() { |
| 26 | Some(rt.block_on(wasmtime::ExternRef::new_async(&mut ctx.core_store, x.0))?) |
| 27 | } else { |
| 28 | Some(wasmtime::ExternRef::new(&mut ctx.core_store, x.0)?) |
| 29 | }), |
| 30 | |
| 31 | AnyRef { |
| 32 | value: None | Some(json_from_wast::AnyRef::Null), |
| 33 | } => Val::AnyRef(None), |
| 34 | AnyRef { |
| 35 | value: Some(json_from_wast::AnyRef::Host(x)), |
| 36 | } => { |
| 37 | let x = if let Some(rt) = ctx.async_runtime.as_ref() { |
| 38 | rt.block_on(wasmtime::ExternRef::new_async(&mut ctx.core_store, x.0))? |
| 39 | } else { |
| 40 | wasmtime::ExternRef::new(&mut ctx.core_store, x.0)? |
| 41 | }; |
| 42 | let x = wasmtime::AnyRef::convert_extern(&mut ctx.core_store, x)?; |
| 43 | Val::AnyRef(Some(x)) |
| 44 | } |
| 45 | NullRef => Val::AnyRef(None), |
| 46 | other => bail!("couldn't convert {other:?} to a runtime value"), |
| 47 | }) |
| 48 | } |
| 49 | |
| 50 | fn extract_lane_as_i8(bytes: u128, lane: usize) -> i8 { |
| 51 | (bytes >> (lane * 8)) as i8 |