Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several refactor work #253

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
483 changes: 0 additions & 483 deletions interpreter/src/call_create.rs

This file was deleted.

26 changes: 19 additions & 7 deletions interpreter/src/etable.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
use crate::{
call_create::CallCreateTrap, eval::*, ExitResult, GasState, Machine, Opcode, RuntimeBackend,
eval::*, trap::CallCreateTrap, ExitResult, GasState, Machine, Opcode, RuntimeBackend,
RuntimeEnvironment, RuntimeState, TrapConstruct,
};
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};

pub trait EtableSet<S, H, Tr> {
pub trait EtableSet {
type State;
type Handle;
type Trap;

fn eval(
&self,
machine: &mut Machine<S>,
handle: &mut H,
machine: &mut Machine<Self::State>,
handle: &mut Self::Handle,
opcode: Opcode,
position: usize,
) -> Control<Tr>;
) -> Control<Self::Trap>;
}

impl<S, H, Tr, F> EtableSet<S, H, Tr> for Etable<S, H, Tr, F>
impl<S, H, Tr, F> EtableSet for Etable<S, H, Tr, F>
where
F: Fn(&mut Machine<S>, &mut H, Opcode, usize) -> Control<Tr>,
{
type State = S;
type Handle = H;
type Trap = Tr;

fn eval(
&self,
machine: &mut Machine<S>,
Expand All @@ -30,11 +38,15 @@ where
}
}

impl<S, H, Tr, F1, F2> EtableSet<S, H, Tr> for (Etable<S, H, Tr, F1>, Etable<S, H, Tr, F2>)
impl<S, H, Tr, F1, F2> EtableSet for (Etable<S, H, Tr, F1>, Etable<S, H, Tr, F2>)
where
F1: Fn(&mut Machine<S>, &mut H, Opcode, usize) -> Control<Tr>,
F2: Fn(&mut Machine<S>, &mut H, Opcode, usize) -> Control<Tr>,
{
type State = S;
type Handle = H;
type Trap = Tr;

fn eval(
&self,
machine: &mut Machine<S>,
Expand Down
2 changes: 1 addition & 1 deletion interpreter/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod misc;
mod system;

use crate::{
call_create::CallCreateTrap, Control, ExitException, ExitSucceed, GasState, Machine, Opcode,
trap::CallCreateTrap, Control, ExitException, ExitSucceed, GasState, Machine, Opcode,
RuntimeBackend, RuntimeEnvironment, RuntimeState, TrapConstruct,
};
use core::ops::{BitAnd, BitOr, BitXor};
Expand Down
17 changes: 11 additions & 6 deletions interpreter/src/interpreter/etable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::interpreter::{Interpreter, StepInterpreter};
use crate::{
Capture, Control, EtableSet, ExitError, ExitException, ExitFatal, ExitResult, ExitSucceed,
Interpreter, Machine, Opcode, Stack, StepInterpreter, Valids,
Machine, Opcode, Stack, Valids,
};
use alloc::vec::Vec;
use core::marker::PhantomData;
Expand Down Expand Up @@ -30,7 +31,7 @@ impl<'etable, S, H, Tr, ES> DerefMut for EtableInterpreter<'etable, S, H, Tr, ES

impl<'etable, S, H, Tr, ES> EtableInterpreter<'etable, S, H, Tr, ES>
where
ES: EtableSet<S, H, Tr>,
ES: EtableSet<State = S, Handle = H, Trap = Tr>,
{
/// Return a reference of the program counter.
pub const fn position(&self) -> usize {
Expand Down Expand Up @@ -86,10 +87,14 @@ where
}
}

impl<'etable, S, H, Tr, ES> Interpreter<S, H, Tr> for EtableInterpreter<'etable, S, H, Tr, ES>
impl<'etable, S, H, Tr, ES> Interpreter for EtableInterpreter<'etable, S, H, Tr, ES>
where
ES: EtableSet<S, H, Tr>,
ES: EtableSet<State = S, Handle = H, Trap = Tr>,
{
type State = S;
type Handle = H;
type Trap = Tr;

fn machine(&self) -> &Machine<S> {
&self.machine
}
Expand Down Expand Up @@ -120,9 +125,9 @@ where
}
}

impl<'etable, S, H, Tr, ES> StepInterpreter<S, H, Tr> for EtableInterpreter<'etable, S, H, Tr, ES>
impl<'etable, S, H, Tr, ES> StepInterpreter for EtableInterpreter<'etable, S, H, Tr, ES>
where
ES: EtableSet<S, H, Tr>,
ES: EtableSet<State = S, Handle = H, Trap = Tr>,
{
#[inline]
fn step(&mut self, handle: &mut H) -> Result<(), Capture<ExitResult, Tr>> {
Expand Down
23 changes: 16 additions & 7 deletions interpreter/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ pub use self::etable::EtableInterpreter;
use crate::{Capture, ExitResult, Machine};
use alloc::vec::Vec;

pub trait Interpreter<S, H, Tr> {
fn machine(&self) -> &Machine<S>;
fn machine_mut(&mut self) -> &mut Machine<S>;
pub type StateFor<I> = <I as Interpreter>::State;
pub type TrapFor<I> = <I as Interpreter>::Trap;
pub type HandleFor<I> = <I as Interpreter>::Handle;
pub type DeconstructFor<I> = (StateFor<I>, Vec<u8>);

fn deconstruct(self) -> (S, Vec<u8>);
fn run(&mut self, handle: &mut H) -> Capture<ExitResult, Tr>;
pub trait Interpreter {
type State;
type Handle;
type Trap;

fn machine(&self) -> &Machine<Self::State>;
fn machine_mut(&mut self) -> &mut Machine<Self::State>;

fn deconstruct(self) -> (Self::State, Vec<u8>);
fn run(&mut self, handle: &mut Self::Handle) -> Capture<ExitResult, Self::Trap>;
fn advance(&mut self);
}

pub trait StepInterpreter<S, H, Tr>: Interpreter<S, H, Tr> {
fn step(&mut self, handle: &mut H) -> Result<(), Capture<ExitResult, Tr>>;
pub trait StepInterpreter: Interpreter {
fn step(&mut self, handle: &mut Self::Handle) -> Result<(), Capture<ExitResult, Self::Trap>>;
}
6 changes: 2 additions & 4 deletions interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@

extern crate alloc;

pub mod call_create;
mod error;
mod etable;
pub mod eval;
mod interpreter;
pub mod interpreter;
mod memory;
mod opcode;
mod runtime;
mod stack;
mod trap;
pub mod trap;
pub mod utils;
mod valids;

pub use crate::error::{Capture, ExitError, ExitException, ExitFatal, ExitResult, ExitSucceed};
pub use crate::etable::{Control, Efn, Etable, EtableSet};
pub use crate::interpreter::{EtableInterpreter, Interpreter, StepInterpreter};
pub use crate::memory::Memory;
pub use crate::opcode::Opcode;
pub use crate::runtime::{
Expand Down
Loading