Merge pull request #401 from ethereum/rust-container

rust: change ownership api in container
This commit is contained in:
Alex Beregszaszi 2019-08-11 09:51:54 +01:00 committed by GitHub
commit d8c2aa2c89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 5 deletions

View File

@ -301,8 +301,11 @@ fn build_create_fn(names: &VMNameSet) -> proc_macro2::TokenStream {
version: unsafe { ::std::ffi::CStr::from_bytes_with_nul_unchecked(#static_version_ident.as_bytes()).as_ptr() as *const i8 },
};
let container = ::evmc_vm::EvmcContainer::<#type_ident>::new(new_instance);
unsafe {
::evmc_vm::EvmcContainer::into_ffi_pointer(Box::new(::evmc_vm::EvmcContainer::<#type_ident>::new(new_instance)))
// Release ownership to EVMC.
::evmc_vm::EvmcContainer::into_ffi_pointer(container)
}
}
}
@ -319,6 +322,7 @@ fn build_destroy_fn(names: &VMNameSet) -> proc_macro2::TokenStream {
std::process::abort();
}
unsafe {
// Acquire ownership from EVMC. This will deallocate it also at the end of the scope.
::evmc_vm::EvmcContainer::<#type_ident>::from_ffi_pointer(instance);
}
}
@ -367,6 +371,7 @@ fn build_execute_fn(names: &VMNameSet) -> proc_macro2::TokenStream {
};
let container = unsafe {
// Acquire ownership from EVMC.
::evmc_vm::EvmcContainer::<#type_name_ident>::from_ffi_pointer(instance)
};
@ -385,6 +390,7 @@ fn build_execute_fn(names: &VMNameSet) -> proc_macro2::TokenStream {
};
unsafe {
// Release ownership to EVMC.
::evmc_vm::EvmcContainer::into_ffi_pointer(container);
}

View File

@ -22,11 +22,11 @@ where
T: EvmcVm + Sized,
{
/// Basic constructor.
pub fn new(_instance: ::evmc_sys::evmc_instance) -> Self {
Self {
pub fn new(_instance: ::evmc_sys::evmc_instance) -> Box<Self> {
Box::new(Self {
instance: _instance,
vm: T::init(),
}
})
}
/// Take ownership of the given pointer and return a box.
@ -147,7 +147,7 @@ mod tests {
::evmc_sys::evmc_status_code::EVMC_FAILURE
);
let ptr = unsafe { EvmcContainer::into_ffi_pointer(Box::new(container)) };
let ptr = unsafe { EvmcContainer::into_ffi_pointer(container) };
let mut context = ExecutionContext::new(&mut backing_context);
let container = unsafe { EvmcContainer::<TestVm>::from_ffi_pointer(ptr) };