getting rid of unwraps

This commit is contained in:
vms 2021-04-19 16:31:16 +03:00
parent 532043a884
commit e9cdf5ecb2
6 changed files with 44 additions and 38 deletions

View File

@ -41,6 +41,7 @@ impl InstructionError {
impl Error for InstructionError {}
/// Allows you to shorten the expression creates a new InstructionError.
#[macro_export]
macro_rules! instr_error {
($instruction:expr, $error_kind:expr) => {
@ -178,6 +179,9 @@ pub enum InstructionErrorKind {
/// Errors related to Serialization/deserialization of record.
SerdeError(String),
/// Errors related to lifting incorrect UTF8 string from a Wasm module.
CorruptedUTF8String(std::string::FromUtf8Error),
}
impl Error for InstructionErrorKind {}
@ -288,6 +292,11 @@ impl Display for InstructionErrorKind {
formatter,
"serde error: {}", err,
),
Self::CorruptedUTF8String(err) => write!(
formatter,
"corrupted utf8 string: {}", err
)
}
}
}

View File

@ -22,36 +22,34 @@ where
}
match value_type {
IType::Boolean => read_bool_array(instance, instruction.clone(), offset, elements_count),
IType::S8 => read_s8_array(instance, instruction.clone(), offset, elements_count),
IType::S16 => read_s16_array(instance, instruction.clone(), offset, elements_count),
IType::S32 => read_s32_array(instance, instruction.clone(), offset, elements_count),
IType::S64 => read_s64_array(instance, instruction.clone(), offset, elements_count),
IType::I32 => read_i32_array(instance, instruction.clone(), offset, elements_count),
IType::I64 => read_i64_array(instance, instruction.clone(), offset, elements_count),
IType::U8 => read_u8_array(instance, instruction.clone(), offset, elements_count),
IType::U16 => read_u16_array(instance, instruction.clone(), offset, elements_count),
IType::U32 => read_u32_array(instance, instruction.clone(), offset, elements_count),
IType::U64 => read_u64_array(instance, instruction.clone(), offset, elements_count),
IType::F32 => read_f32_array(instance, instruction.clone(), offset, elements_count),
IType::F64 => read_f64_array(instance, instruction.clone(), offset, elements_count),
IType::String => read_string_array(instance, instruction.clone(), offset, elements_count),
IType::Boolean => read_bool_array(instance, instruction, offset, elements_count),
IType::S8 => read_s8_array(instance, instruction, offset, elements_count),
IType::S16 => read_s16_array(instance, instruction, offset, elements_count),
IType::S32 => read_s32_array(instance, instruction, offset, elements_count),
IType::S64 => read_s64_array(instance, instruction, offset, elements_count),
IType::I32 => read_i32_array(instance, instruction, offset, elements_count),
IType::I64 => read_i64_array(instance, instruction, offset, elements_count),
IType::U8 => read_u8_array(instance, instruction, offset, elements_count),
IType::U16 => read_u16_array(instance, instruction, offset, elements_count),
IType::U32 => read_u32_array(instance, instruction, offset, elements_count),
IType::U64 => read_u64_array(instance, instruction, offset, elements_count),
IType::F32 => read_f32_array(instance, instruction, offset, elements_count),
IType::F64 => read_f64_array(instance, instruction, offset, elements_count),
IType::String => read_string_array(instance, instruction, offset, elements_count),
IType::Record(record_type_id) => read_record_array(
instance,
instruction.clone(),
instruction,
*record_type_id,
offset,
elements_count,
),
IType::ByteArray => read_array_array(
instance,
instruction.clone(),
instruction,
&IType::ByteArray,
offset,
elements_count,
),
IType::Array(ty) => {
read_array_array(instance, instruction.clone(), &ty, offset, elements_count)
}
IType::Array(ty) => read_array_array(instance, instruction, &ty, offset, elements_count),
}
}

View File

@ -354,8 +354,12 @@ where
*string_size as _,
)?;
// TODO: check
let string = String::from_utf8(string_mem).unwrap();
let string = String::from_utf8(string_mem).map_err(|e| {
InstructionError::new(
instruction.clone(),
InstructionErrorKind::CorruptedUTF8String(e),
)
})?;
result.push(IValue::String(string));
}

View File

@ -104,8 +104,9 @@ where
string_size as _,
)?;
// TODO: check
let string = String::from_utf8(string_mem).unwrap();
let string = String::from_utf8(string_mem).map_err(|e| {
InstructionError::new(instruction, InstructionErrorKind::CorruptedUTF8String(e))
})?;
Ok(string)
}
@ -126,12 +127,7 @@ where
let offset = reader.read_u32();
let elements_count = reader.read_u32();
let array = read_from_instance_mem(
instance,
instruction.clone(),
offset as _,
elements_count as _,
)?;
let array = read_from_instance_mem(instance, instruction, offset as _, elements_count as _)?;
let byte_array = IValue::ByteArray(array);
Ok(byte_array)
@ -159,7 +155,7 @@ where
ty,
array_offset as _,
elements_count as _,
instruction.clone(),
instruction,
)
}
@ -182,11 +178,9 @@ where
let record_type = instance.wit_record_by_id(record_type_id).ok_or_else(|| {
InstructionError::new(
instruction.clone(),
InstructionErrorKind::RecordTypeByNameIsMissing {
record_type_id,
},
InstructionErrorKind::RecordTypeByNameIsMissing { record_type_id },
)
})?;
record_lift_memory_impl(instance, record_type, offset as _, instruction.clone())
record_lift_memory_impl(instance, record_type, offset as _, instruction)
}

View File

@ -40,7 +40,7 @@
#![deny(
dead_code,
broken_intra_doc_links,
rustdoc::broken_intra_doc_links,
missing_docs,
nonstandard_style,
unreachable_patterns,
@ -52,7 +52,6 @@
// #![forbid(unsafe_code)]
#![doc(html_favicon_url = "https://wasmer.io/static/icons/favicon.ico")]
#![doc(html_logo_url = "https://github.com/wasmerio.png")]
#![recursion_limit = "512"]
pub mod ast;
#[macro_use]

View File

@ -206,6 +206,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
V: de::Visitor<'de>,
{
match self.iterator.peek() {
Some(IValue::Boolean(_)) => self.deserialize_bool(visitor),
Some(IValue::S8(_)) => self.deserialize_i8(visitor),
Some(IValue::S16(_)) => self.deserialize_i16(visitor),
Some(IValue::S32(_)) => self.deserialize_i32(visitor),
@ -217,6 +218,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
Some(IValue::F32(_)) => self.deserialize_f32(visitor),
Some(IValue::F64(_)) => self.deserialize_f64(visitor),
Some(IValue::String(_)) => self.deserialize_string(visitor),
Some(IValue::ByteArray(_)) => self.deserialize_bytes(visitor),
Some(IValue::Array(_)) => self.deserialize_bytes(visitor),
Some(IValue::I32(_)) => self.deserialize_i32(visitor),
Some(IValue::I64(_)) => self.deserialize_i64(visitor),
@ -225,11 +227,11 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
}
}
fn deserialize_bool<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
unimplemented!("`bool` is not supported by WIT for the moment.")
visitor.visit_bool(self.next_u8()? == 1)
}
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>