use super::ExtBytecode;
use crate::{
primitives::ExecReturnValue,
vm::{
evm::{memory::Memory, stack::Stack},
ExecResult, Ext,
},
Config, DispatchError, Error,
};
use alloc::vec::Vec;
use pallet_revive_uapi::ReturnFlags;
#[derive(Debug, PartialEq)]
pub enum Halt {
Stop,
Return(Vec<u8>),
Revert(Vec<u8>),
Err(DispatchError),
}
impl<T: Config> From<Error<T>> for Halt {
fn from(err: Error<T>) -> Self {
Halt::Err(err.into())
}
}
impl From<Halt> for ExecResult {
fn from(halt: Halt) -> Self {
match halt {
Halt::Stop => Ok(ExecReturnValue::default()),
Halt::Return(data) => Ok(ExecReturnValue { flags: ReturnFlags::empty(), data }),
Halt::Revert(data) => Ok(ExecReturnValue { flags: ReturnFlags::REVERT, data }),
Halt::Err(err) => Err(err.into()),
}
}
}
#[derive(Debug)]
pub struct Interpreter<'a, E: Ext> {
pub ext: &'a mut E,
pub bytecode: ExtBytecode,
pub input: Vec<u8>, pub stack: Stack<E::T>,
pub memory: Memory<E::T>,
}
impl<'a, E: Ext> Interpreter<'a, E> {
pub fn new(bytecode: ExtBytecode, input: Vec<u8>, ext: &'a mut E) -> Self {
Self { ext, bytecode, input, stack: Stack::new(), memory: Memory::new() }
}
}