use auto_impl::auto_impl;
use interpreter::{
instructions::{instruction_table_gas_changes_spec, InstructionTable},
Host, Instruction, InterpreterTypes,
};
use primitives::hardfork::SpecId;
use std::boxed::Box;
#[auto_impl(&mut, Box)]
pub trait InstructionProvider {
type Context;
type InterpreterTypes: InterpreterTypes;
fn instruction_table(&self) -> &InstructionTable<Self::InterpreterTypes, Self::Context>;
}
#[derive(Debug)]
pub struct EthInstructions<WIRE: InterpreterTypes, HOST: ?Sized> {
pub instruction_table: Box<InstructionTable<WIRE, HOST>>,
pub spec: SpecId,
}
impl<WIRE, HOST: Host + ?Sized> Clone for EthInstructions<WIRE, HOST>
where
WIRE: InterpreterTypes,
{
fn clone(&self) -> Self {
Self {
instruction_table: self.instruction_table.clone(),
spec: self.spec,
}
}
}
impl<WIRE, HOST> EthInstructions<WIRE, HOST>
where
WIRE: InterpreterTypes,
HOST: Host,
{
#[deprecated(since = "0.2.0", note = "use new_mainnet_with_spec instead")]
pub fn new_mainnet() -> Self {
let spec = SpecId::default();
Self::new(instruction_table_gas_changes_spec(spec), spec)
}
pub fn new_mainnet_with_spec(spec: SpecId) -> Self {
Self::new(instruction_table_gas_changes_spec(spec), spec)
}
#[inline]
pub fn new(base_table: InstructionTable<WIRE, HOST>, spec: SpecId) -> Self {
Self {
instruction_table: Box::new(base_table),
spec,
}
}
#[inline]
pub fn insert_instruction(&mut self, opcode: u8, instruction: Instruction<WIRE, HOST>) {
self.instruction_table[opcode as usize] = instruction;
}
}
impl<IT, CTX> InstructionProvider for EthInstructions<IT, CTX>
where
IT: InterpreterTypes,
CTX: Host,
{
type InterpreterTypes = IT;
type Context = CTX;
fn instruction_table(&self) -> &InstructionTable<Self::InterpreterTypes, Self::Context> {
&self.instruction_table
}
}