Skip to content

Commit

Permalink
Fix constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelang committed Mar 13, 2019
1 parent 2770761 commit b64d7f1
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions bindings/rust/evmc-vm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub extern crate evmc_sys;
pub use evmc_sys as ffi;

use std::slice;

// TODO: Add helpers for host interface
// TODO: Add convenient helpers for evmc_result
// TODO: Add convenient helpers for evmc_execute
Expand All @@ -12,25 +14,45 @@ pub use evmc_sys as ffi;
pub struct EvmcResult(Box<ffi::evmc_result>);

impl EvmcResult {
pub fn new(
status: ffi::evmc_status_code,
gasleft: i64,
output: &[u8],
) -> Self {
pub fn new(status: ffi::evmc_status_code, gasleft: i64, output: Option<&[u8]>) -> Self {
EvmcResult(Box::new(ffi::evmc_result {
status_code: status,
gas_left: gasleft,
output_data: output.as_ptr(),
output_size: output.len(),
output_data: if let Some(buf) = output {
buf.clone().as_ptr()
} else {
std::ptr::null()
},
output_size: if let Some(buf) = output { buf.len() } else { 0 },
release: Some(release_result),
create_address: ffi::evmc_address { bytes: [0u8; 20] },
padding: [0u8; 4],
}))
}

pub fn gasleft(&self) -> i64 {
self.0.gas_left
}

pub fn create_addr(&self) -> &ffi::evmc_address {
&self.0.create_address
}

pub fn output(&self) -> Option<&[u8]> {
if !self.0.output_data.is_null() && self.0.output_size != 0 {
Some(unsafe { slice::from_raw_parts::<u8>(self.0.output_data, self.0.output_size) })
} else {
None
}
}

pub fn status(&self) -> ffi::evmc_status_code {
self.0.status_code
}
}

#[no_mangle]
pub extern "C" fn release_result(result: *const ffi::evmc_result) {
extern "C" fn release_result(result: *const ffi::evmc_result) {
unsafe {
Box::from_raw(result as *mut ffi::evmc_result);
}
Expand Down

0 comments on commit b64d7f1

Please sign in to comment.