Skip to content

Commit

Permalink
rust: make ExecutionContext optional in EvmcVm.execute()
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Aug 9, 2019
1 parent 8634f0b commit f4f07ff
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bindings/rust/evmc-declare-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl EvmcVm for FooVM {
_revision: evmc_sys::evmc_revision,
_code: &[u8],
_message: &ExecutionMessage,
_context: &mut ExecutionContext,
_context: Option<&mut ExecutionContext>,
) -> ExecutionResult {
ExecutionResult::success(1337, None)
}
Expand Down
18 changes: 10 additions & 8 deletions bindings/rust/evmc-declare/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
//! ExampleVM {}
//! }
//!
//! fn execute(&self, revision: evmc_vm::ffi::evmc_revision, code: &[u8], message: &evmc_vm::ExecutionMessage, context: &mut evmc_vm::ExecutionContext) -> evmc_vm::ExecutionResult {
//! fn execute(&self, revision: evmc_vm::ffi::evmc_revision, code: &[u8], message: &evmc_vm::ExecutionMessage, context: Option<&mut evmc_vm::ExecutionContext>) -> evmc_vm::ExecutionResult {
//! evmc_vm::ExecutionResult::success(1337, None)
//! }
//! }
//! ```
// Set a higher recursion limit because parsing certain token trees might fail with the default of 64.
#![recursion_limit = "128"]
#![recursion_limit = "160"]

extern crate proc_macro;

Expand Down Expand Up @@ -338,8 +338,6 @@ fn build_execute_fn(names: &VMNameSet) -> proc_macro2::TokenStream {
use evmc_vm::EvmcVm;

assert!(!instance.is_null());
// TODO: context is optional in case of the "precompiles" capability
assert!(!context.is_null());
assert!(!msg.is_null());

let execution_message: ::evmc_vm::ExecutionMessage = unsafe {
Expand All @@ -361,10 +359,14 @@ fn build_execute_fn(names: &VMNameSet) -> proc_macro2::TokenStream {
};

let result = ::std::panic::catch_unwind(|| {
let mut execution_context = unsafe {
::evmc_vm::ExecutionContext::new(context.as_mut().expect("EVMC context is null"))
};
container.execute(revision, code_ref, &execution_message, &mut execution_context)
if context.is_null() {
container.execute(revision, code_ref, &execution_message, None)
} else {
let mut execution_context = unsafe {
::evmc_vm::ExecutionContext::new(context.as_mut().expect("EVMC context is null"))
};
container.execute(revision, code_ref, &execution_message, Some(&mut execution_context))
}
});

let result = if result.is_err() {
Expand Down
6 changes: 3 additions & 3 deletions bindings/rust/evmc-vm/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ mod tests {
_revision: evmc_sys::evmc_revision,
_code: &[u8],
_message: &ExecutionMessage,
_context: &mut ExecutionContext,
_context: Option<&mut ExecutionContext>,
) -> ExecutionResult {
ExecutionResult::failure()
}
Expand Down Expand Up @@ -141,7 +141,7 @@ mod tests {
evmc_sys::evmc_revision::EVMC_PETERSBURG,
&code,
&message,
&mut context
Some(&mut context)
)
.get_status_code(),
::evmc_sys::evmc_status_code::EVMC_FAILURE
Expand All @@ -157,7 +157,7 @@ mod tests {
evmc_sys::evmc_revision::EVMC_PETERSBURG,
&code,
&message,
&mut context
Some(&mut context)
)
.get_status_code(),
::evmc_sys::evmc_status_code::EVMC_FAILURE
Expand Down
2 changes: 1 addition & 1 deletion bindings/rust/evmc-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub trait EvmcVm {
revision: ffi::evmc_revision,
code: &'a [u8],
message: &'a ExecutionMessage,
context: &'a mut ExecutionContext<'a>,
context: Option<&'a mut ExecutionContext<'a>>,
) -> ExecutionResult;
}

Expand Down
7 changes: 6 additions & 1 deletion examples/example-rust-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ impl EvmcVm for ExampleRustVM {
_revision: evmc_sys::evmc_revision,
_code: &'a [u8],
message: &'a ExecutionMessage,
_context: &'a mut ExecutionContext<'a>,
_context: Option<&'a mut ExecutionContext<'a>>,
) -> ExecutionResult {
if _context.is_none() {
return ExecutionResult::failure();
}
let _context = _context.unwrap();

if message.kind() != evmc_sys::evmc_call_kind::EVMC_CALL {
return ExecutionResult::failure();
}
Expand Down

0 comments on commit f4f07ff

Please sign in to comment.