mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-23 13:41:32 +00:00
feat(interface-types) Simplify code by implementing From<TryFromIntError>
.
This commit is contained in:
@ -5,6 +5,7 @@ use crate::{ast::InterfaceType, interpreter::Instruction};
|
|||||||
use std::{
|
use std::{
|
||||||
error::Error,
|
error::Error,
|
||||||
fmt::{self, Display, Formatter},
|
fmt::{self, Display, Formatter},
|
||||||
|
num::TryFromIntError,
|
||||||
result::Result,
|
result::Result,
|
||||||
string::{self, ToString},
|
string::{self, ToString},
|
||||||
};
|
};
|
||||||
@ -150,7 +151,7 @@ pub enum InstructionErrorKind {
|
|||||||
/// The string contains invalid UTF-8 encoding.
|
/// The string contains invalid UTF-8 encoding.
|
||||||
String(string::FromUtf8Error),
|
String(string::FromUtf8Error),
|
||||||
|
|
||||||
/// A negative value isn't allowed (like a negative pointer value).
|
/// Out of range integral type conversion attempted.
|
||||||
NegativeValue {
|
NegativeValue {
|
||||||
/// The variable name that triggered the error.
|
/// The variable name that triggered the error.
|
||||||
subject: &'static str,
|
subject: &'static str,
|
||||||
@ -236,9 +237,15 @@ impl Display for InstructionErrorKind {
|
|||||||
|
|
||||||
Self::NegativeValue { subject } => write!(
|
Self::NegativeValue { subject } => write!(
|
||||||
formatter,
|
formatter,
|
||||||
"read the value of `{}` which must be positive",
|
"attempted to convert `{}` but it appears to be a negative value",
|
||||||
subject
|
subject
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<(TryFromIntError, &'static str)> for InstructionErrorKind {
|
||||||
|
fn from((_, subject): (TryFromIntError, &'static str)) -> Self {
|
||||||
|
InstructionErrorKind::NegativeValue { subject }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -33,18 +33,14 @@ executable_instruction!(
|
|||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let pointer: usize = to_native::<i32>(&inputs[0], instruction)?.try_into().map_err(|_| {
|
let pointer: usize = to_native::<i32>(&inputs[0], instruction)?
|
||||||
InstructionError::new(
|
.try_into()
|
||||||
instruction,
|
.map_err(|e| (e, "pointer").into())
|
||||||
InstructionErrorKind::NegativeValue { subject: "pointer" },
|
.map_err(|k| InstructionError::new(instruction, k))?;
|
||||||
)
|
let length: usize = to_native::<i32>(&inputs[1], instruction)?
|
||||||
})?;
|
.try_into()
|
||||||
let length: usize = to_native::<i32>(&inputs[1], instruction)?.try_into().map_err(|_| {
|
.map_err(|e| (e, "length").into())
|
||||||
InstructionError::new(
|
.map_err(|k| InstructionError::new(instruction, k))?;
|
||||||
instruction,
|
|
||||||
InstructionErrorKind::NegativeValue { subject: "length" },
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
let memory_view = memory.view();
|
let memory_view = memory.view();
|
||||||
|
|
||||||
if length == 0 {
|
if length == 0 {
|
||||||
@ -237,7 +233,7 @@ mod tests {
|
|||||||
memory: Memory::new("Hello!".as_bytes().iter().map(|u| Cell::new(*u)).collect()),
|
memory: Memory::new("Hello!".as_bytes().iter().map(|u| Cell::new(*u)).collect()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
error: r#"`string.lift_memory` read the value of `pointer` which must be positive"#,
|
error: r#"`string.lift_memory` attempted to convert `pointer` but it appears to be a negative value"#,
|
||||||
);
|
);
|
||||||
|
|
||||||
test_executable_instruction!(
|
test_executable_instruction!(
|
||||||
@ -255,7 +251,7 @@ mod tests {
|
|||||||
memory: Memory::new("Hello!".as_bytes().iter().map(|u| Cell::new(*u)).collect()),
|
memory: Memory::new("Hello!".as_bytes().iter().map(|u| Cell::new(*u)).collect()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
error: r#"`string.lift_memory` read the value of `length` which must be positive"#,
|
error: r#"`string.lift_memory` attempted to convert `length` but it appears to be a negative value"#,
|
||||||
);
|
);
|
||||||
|
|
||||||
test_executable_instruction!(
|
test_executable_instruction!(
|
||||||
|
Reference in New Issue
Block a user