openethereum/src/evm/factory.rs

124 lines
2.2 KiB
Rust
Raw Normal View History

2015-12-30 12:46:10 +01:00
//! Evm factory.
2016-01-14 18:29:18 +01:00
use std::fmt;
2015-12-29 12:37:38 +01:00
use evm::Evm;
2016-01-14 18:29:18 +01:00
#[derive(Clone)]
2016-01-14 13:36:45 +01:00
pub enum VMType {
Jit,
Interpreter
}
2016-01-16 17:08:57 +01:00
2016-01-14 18:29:18 +01:00
impl fmt::Display for VMType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", match *self {
VMType::Jit => "JIT",
VMType::Interpreter => "INT"
})
}
}
impl VMType {
2016-01-16 17:08:57 +01:00
/// Return all possible VMs (JIT, Interpreter)
2016-01-14 18:29:18 +01:00
#[cfg(feature="jit")]
pub fn all() -> Vec<VMType> {
vec![VMType::Jit, VMType::Interpreter]
}
2016-01-16 17:08:57 +01:00
/// Return all possible VMs (Interpreter)
2016-01-14 18:29:18 +01:00
#[cfg(not(feature="jit"))]
pub fn all() -> Vec<VMType> {
vec![VMType::Interpreter]
}
}
2016-01-14 13:36:45 +01:00
2016-01-11 02:42:02 +01:00
/// Evm factory. Creates appropriate Evm.
2016-01-14 13:36:45 +01:00
pub struct Factory {
evm : VMType
}
2015-12-29 12:37:38 +01:00
2016-01-11 19:01:42 +01:00
impl Factory {
2016-01-16 17:08:57 +01:00
/// Create fresh instance of VM
2016-01-14 13:36:45 +01:00
pub fn create(&self) -> Box<Evm> {
match self.evm {
VMType::Jit => {
Factory::jit()
},
VMType::Interpreter => {
Box::new(super::interpreter::Interpreter)
}
}
}
2016-01-16 17:08:57 +01:00
/// Create new instance of specific `VMType` factory
2016-01-14 13:36:45 +01:00
pub fn new(evm: VMType) -> Factory {
Factory {
evm: evm
}
}
2015-12-29 12:37:38 +01:00
#[cfg(feature = "jit")]
2016-01-14 13:36:45 +01:00
fn jit() -> Box<Evm> {
2015-12-29 12:37:38 +01:00
Box::new(super::jit::JitEvm)
}
#[cfg(not(feature = "jit"))]
2016-01-14 13:36:45 +01:00
fn jit() -> Box<Evm> {
unimplemented!()
}
/// Returns jitvm factory
#[cfg(feature = "jit")]
pub fn default() -> Factory {
Factory {
evm: VMType::Jit
}
}
/// Returns native rust evm factory
#[cfg(not(feature = "jit"))]
pub fn default() -> Factory {
Factory {
evm: VMType::Interpreter
}
2015-12-29 12:37:38 +01:00
}
}
#[test]
fn test_create_vm() {
2016-01-14 13:36:45 +01:00
let _vm = Factory::default().create();
2015-12-29 12:37:38 +01:00
}
2016-01-14 16:17:44 +01:00
2016-01-16 17:08:57 +01:00
/// Create tests by injecting different VM factories
2016-01-14 16:17:44 +01:00
#[macro_export]
macro_rules! evm_test(
($name_test: ident: $name_jit: ident, $name_int: ident) => {
#[test]
#[cfg(feature = "jit")]
fn $name_jit() {
2016-01-14 17:42:17 +01:00
$name_test(Factory::new(VMType::Jit));
2016-01-14 16:17:44 +01:00
}
#[test]
fn $name_int() {
2016-01-14 17:42:17 +01:00
$name_test(Factory::new(VMType::Interpreter));
2016-01-14 16:17:44 +01:00
}
}
);
2016-01-16 17:08:57 +01:00
/// Create ignored tests by injecting different VM factories
#[macro_export]
macro_rules! evm_test_ignore(
($name_test: ident: $name_jit: ident, $name_int: ident) => {
#[test]
#[ignore]
#[cfg(feature = "jit")]
fn $name_jit() {
$name_test(Factory::new(VMType::Jit));
}
#[test]
#[ignore]
fn $name_int() {
$name_test(Factory::new(VMType::Interpreter));
}
}
);