feat(vm): normal exec errors also shouldn't allocate

This commit is contained in:
Alain Zscheile 2022-10-22 22:04:13 +02:00
parent 668fc0d6fa
commit e83e9f8394

View file

@ -22,15 +22,15 @@ fn is_call2jump(m: &[u8], pos: usize) -> bool {
pub type StackEntValue = u64;
#[derive(Clone, Debug)]
pub enum Error {
pub enum Error<'m> {
InstrpOutOfBounds,
UnparsableInstruction(Vec<u8>),
UnparsableInstruction(&'m [u8]),
OutOfFuel,
NotEnoughStacked,
DivisionByZero,
}
impl fmt::Display for Error {
impl fmt::Display for Error<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InstrpOutOfBounds => write!(f, "instruction pointer out-of-bounds"),
@ -129,12 +129,12 @@ pub struct Process<'m> {
pub instrp: usize,
}
impl Process<'_> {
fn stpop(&mut self) -> Result<StackEntValue, Error> {
impl<'m> Process<'m> {
fn stpop(&mut self) -> Result<StackEntValue, Error<'m>> {
self.stack.pop().ok_or(Error::NotEnoughStacked)
}
pub fn run(&mut self, mut fuel: Option<&mut u64>) -> Result<Option<u64>, Error> {
pub fn run(&mut self, mut fuel: Option<&mut u64>) -> Result<Option<u64>, Error<'m>> {
loop {
let previptr = self.instrp;
tracing::trace!("previptr = {}", previptr);
@ -146,7 +146,7 @@ impl Process<'_> {
}
let (nxtidelta, nxti) = match next_instr(self.m, self.instrp) {
None => return Err(Error::InstrpOutOfBounds),
Some(Err(code)) => return Err(Error::UnparsableInstruction(code.to_vec())),
Some(Err(code)) => return Err(Error::UnparsableInstruction(code)),
Some(Ok(x)) => x,
};
self.instrp += nxtidelta;