vm factory

This commit is contained in:
debris 2015-12-29 12:37:38 +01:00
parent 3dd26582a2
commit e3f59d82ef
4 changed files with 33 additions and 7 deletions

View File

@ -12,5 +12,5 @@ pub enum ReturnCode {
} }
pub trait Evm { pub trait Evm {
fn exec(data: RuntimeData, env: &mut Env) -> ReturnCode; fn exec(&self, data: RuntimeData, env: &mut Env) -> ReturnCode;
} }

View File

@ -189,7 +189,7 @@ impl From<evmjit::ReturnCode> for evm::ReturnCode {
pub struct JitEvm; pub struct JitEvm;
impl evm::Evm for JitEvm { impl evm::Evm for JitEvm {
fn exec(data: evm::RuntimeData, env: &mut evm::Env) -> evm::ReturnCode { fn exec(&self, data: evm::RuntimeData, env: &mut evm::Env) -> evm::ReturnCode {
// Dirty hack. This is unsafe, but we interact with ffi, so it's justified. // Dirty hack. This is unsafe, but we interact with ffi, so it's justified.
let env_adapter: EnvAdapter<'static> = unsafe { ::std::mem::transmute(EnvAdapter::new(env)) }; let env_adapter: EnvAdapter<'static> = unsafe { ::std::mem::transmute(EnvAdapter::new(env)) };
let mut env_handle = evmjit::EnvHandle::new(env_adapter); let mut env_handle = evmjit::EnvHandle::new(env_adapter);
@ -205,6 +205,7 @@ mod tests {
use util::uint::*; use util::uint::*;
use evm::*; use evm::*;
use evm::jit::{FromJit, IntoJit}; use evm::jit::{FromJit, IntoJit};
use super::*;
#[test] #[test]
fn test_to_and_from_u256() { fn test_to_and_from_u256() {
@ -254,7 +255,8 @@ mod tests {
data.origin = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap(); data.origin = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
data.call_value = U256::from_str("0de0b6b3a7640000").unwrap(); data.call_value = U256::from_str("0de0b6b3a7640000").unwrap();
let mut env = Env::new(); let mut env = Env::new();
assert_eq!(JitEvm::exec(data, &mut env), ReturnCode::Stop); let evm = JitEvm;
assert_eq!(evm.exec(data, &mut env), ReturnCode::Stop);
} }
} }

View File

@ -3,12 +3,10 @@
pub mod env; pub mod env;
pub mod runtime_data; pub mod runtime_data;
pub mod evm; pub mod evm;
pub mod vmfactory;
#[cfg(feature = "jit" )] #[cfg(feature = "jit" )]
pub mod jit; mod jit;
pub use self::evm::{Evm, ReturnCode}; pub use self::evm::{Evm, ReturnCode};
pub use self::env::Env; pub use self::env::Env;
pub use self::runtime_data::RuntimeData; pub use self::runtime_data::RuntimeData;
#[cfg(feature = "jit" )]
pub use self::jit::JitEvm;

26
src/evm/vmfactory.rs Normal file
View File

@ -0,0 +1,26 @@
//! Vm factory.
use evm::Evm;
/// Vm factory. Creates appropriate Evm.
/// TODO: SmartVm
pub struct VmFactory;
impl VmFactory {
/// Returns jit vm
#[cfg(feature = "jit")]
pub fn create() -> Box<Evm> {
Box::new(super::jit::JitEvm)
}
/// Returns native rust evm
#[cfg(not(feature = "jit"))]
pub fn create() -> Box<Evm> {
unimplemented!();
}
}
#[test]
fn test_create_vm() {
let _vm = VmFactory::create();
}