safe handles and docs

This commit is contained in:
debris 2015-12-22 16:27:19 +01:00
parent 9754cf83be
commit 61a4b59da5

View File

@ -3,7 +3,6 @@
//! Requires latest version of Ethereum EVM JIT. https://github.com/debris/evmjit //! Requires latest version of Ethereum EVM JIT. https://github.com/debris/evmjit
extern crate libc; extern crate libc;
use std::ptr;
#[repr(C)] #[repr(C)]
pub struct JitI256 { pub struct JitI256 {
@ -37,20 +36,20 @@ pub struct RuntimeDataHandle {
impl RuntimeDataHandle { impl RuntimeDataHandle {
/// Creates new handle. /// Creates new handle.
pub unsafe fn new() -> Self { pub fn new() -> Self {
RuntimeDataHandle { RuntimeDataHandle {
runtime_data: evmjit_create_runtime_data() runtime_data: unsafe { evmjit_create_runtime_data() }
} }
} }
/// Returns immutable reference to runtime data. /// Returns immutable reference to runtime data.
pub unsafe fn runtime_data(&self) -> &JitRuntimeData { pub fn runtime_data(&self) -> &JitRuntimeData {
&*self.runtime_data unsafe { &*self.runtime_data }
} }
/// Returns mutable reference to runtime data. /// Returns mutable reference to runtime data.
pub unsafe fn mut_runtime_data(&mut self) -> &mut JitRuntimeData { pub fn mut_runtime_data(&mut self) -> &mut JitRuntimeData {
&mut *self.runtime_data unsafe { &mut *self.runtime_data }
} }
} }
@ -76,22 +75,32 @@ pub enum JitReturnCode {
/// JitContext struct declaration. /// JitContext struct declaration.
pub enum JitContext {} pub enum JitContext {}
/// Safe handle for jit context.
pub struct ContextHandle { pub struct ContextHandle {
data_handle: RuntimeDataHandle, context: *mut JitContext,
env: Env, _data_handle: RuntimeDataHandle
context: *mut JitContext
} }
impl ContextHandle { impl ContextHandle {
pub unsafe fn new(mut data_handle: RuntimeDataHandle, env: Env) -> Self { /// Creates new context handle.
// TODO: context should take env pub fn new(mut data_handle: RuntimeDataHandle, mut env: Env) -> Self {
let context = evmjit_create_context(data_handle.mut_runtime_data(), &mut 0); let context = unsafe { evmjit_create_context(data_handle.mut_runtime_data(), &mut env) };
ContextHandle { ContextHandle {
data_handle: data_handle, context: context,
env: env, _data_handle: data_handle
context: context
} }
} }
/// Executes context.
pub fn exec(&mut self) -> JitReturnCode {
unsafe { evmjit_exec(self.context) }
}
}
impl Drop for ContextHandle {
fn drop(&mut self) {
unsafe { evmjit_destroy_context(self.context); }
}
} }
/// Component oriented wrapper around jit env c interface. /// Component oriented wrapper around jit env c interface.
@ -105,15 +114,23 @@ pub struct Env {
} }
impl Env { impl Env {
/// Creates new environment wrapper for given implementation
pub fn new<T>(env_impl: T) -> Self where T: JitEnv + 'static { pub fn new<T>(env_impl: T) -> Self where T: JitEnv + 'static {
Env { env_impl: Some(Box::new(env_impl)) } Env { env_impl: Some(Box::new(env_impl)) }
} }
/// Creates empty environment.
/// It can be used to for any operations.
pub fn empty() -> Self {
Env { env_impl: None }
}
} }
impl JitEnv for Env { impl JitEnv for Env {
fn sload(&mut self) { fn sload(&mut self) {
if let Some(ref mut env) = self.env_impl { match self.env_impl {
env.sload(); Some(ref mut env) => env.sload(),
None => { panic!(); }
} }
} }
} }
@ -189,21 +206,22 @@ pub extern fn env_log(_env: *mut Env,
extern "C" { extern "C" {
pub fn evmjit_create_runtime_data() -> *mut JitRuntimeData; pub fn evmjit_create_runtime_data() -> *mut JitRuntimeData;
pub fn evmjit_destroy_runtime_data(data: *mut JitRuntimeData); pub fn evmjit_destroy_runtime_data(data: *mut JitRuntimeData);
pub fn evmjit_create_context(data: *mut JitRuntimeData, env: *mut u8) -> *mut JitContext;
pub fn evmjit_destroy_context(context: *mut JitContext); pub fn evmjit_destroy_context(context: *mut JitContext);
pub fn evmjit_exec(context: *mut JitContext) -> JitReturnCode; pub fn evmjit_exec(context: *mut JitContext) -> JitReturnCode;
}
#[link(name="evmjit")]
// Env does not have to by a C type
#[allow(improper_ctypes)]
extern "C" {
pub fn evmjit_create_context(data: *mut JitRuntimeData, env: *mut Env) -> *mut JitContext;
} }
#[test] #[test]
fn it_works() { fn ffi_test() {
unsafe { unsafe {
let mut env = 0u8;
let data = evmjit_create_runtime_data(); let data = evmjit_create_runtime_data();
let context = evmjit_create_context(data, &mut env); let context = evmjit_create_context(data, &mut Env::empty());
let code = evmjit_exec(context); let code = evmjit_exec(context);
assert_eq!(code, JitReturnCode::Stop); assert_eq!(code, JitReturnCode::Stop);
@ -213,3 +231,8 @@ fn it_works() {
} }
} }
#[test]
fn handle_test() {
let mut context = ContextHandle::new(RuntimeDataHandle::new(), Env::empty());
assert_eq!(context.exec(), JitReturnCode::Stop);
}