wasm_interp: don't panic when unwrapping Value

This commit is contained in:
Brian Carroll 2022-12-06 20:19:25 +00:00
parent d49ae6701a
commit 9d912a6cc7
No known key found for this signature in database
GPG key ID: 5C7B2EC4101703C0
5 changed files with 49 additions and 39 deletions

View file

@ -695,28 +695,28 @@ pub enum Value {
}
impl Value {
pub fn unwrap_i32(&self) -> i32 {
pub fn expect_i32(&self) -> Result<i32, (ValueType, ValueType)> {
match self {
Value::I32(x) => *x,
_ => panic!("Expected I32 but found {:?}", self),
Value::I32(x) => Ok(*x),
_ => Err((ValueType::I32, ValueType::from(*self))),
}
}
pub fn unwrap_i64(&self) -> i64 {
pub fn expect_i64(&self) -> Result<i64, (ValueType, ValueType)> {
match self {
Value::I64(x) => *x,
_ => panic!("Expected I64 but found {:?}", self),
Value::I64(x) => Ok(*x),
_ => Err((ValueType::I64, ValueType::from(*self))),
}
}
pub fn unwrap_f32(&self) -> f32 {
pub fn expect_f32(&self) -> Result<f32, (ValueType, ValueType)> {
match self {
Value::F32(x) => *x,
_ => panic!("Expected F32 but found {:?}", self),
Value::F32(x) => Ok(*x),
_ => Err((ValueType::F32, ValueType::from(*self))),
}
}
pub fn unwrap_f64(&self) -> f64 {
pub fn expect_f64(&self) -> Result<f64, (ValueType, ValueType)> {
match self {
Value::F64(x) => *x,
_ => panic!("Expected F64 but found {:?}", self),
Value::F64(x) => Ok(*x),
_ => Err((ValueType::F64, ValueType::from(*self))),
}
}
}