diff --git a/Cargo.toml b/Cargo.toml index 43fcf446c..e2f53611b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,4 +2,5 @@ members = [ "bindings/rust/evmc-sys", "bindings/rust/evmc-vm", + "examples/example-rust-vm" ] diff --git a/circle.yml b/circle.yml index 89474d42c..71ce393b7 100644 --- a/circle.yml +++ b/circle.yml @@ -54,6 +54,10 @@ jobs: - store_artifacts: path: ~/package destination: package + - persist_to_workspace: + root: ~/build + paths: + - test/evmc-vmtester build-cxx17: <<: *build @@ -159,6 +163,14 @@ jobs: - run: name: Test command: cargo test + - attach_workspace: + at: ~/build + - run: + name: Test with evmc-vmtester + command: | + # FIXME: there is a linking issue with Cargo workspaces, this is to workaround it + export LD_LIBRARY_PATH="$(rustc --print sysroot)/lib:$LD_LIBRARY_PATH" + ~/build/test/evmc-vmtester target/debug/libexamplerustvm.so workflows: version: 2 @@ -171,7 +183,9 @@ workflows: - build-clang3.8 - bindings-go-latest - bindings-go-min - - bindings-rust + - bindings-rust: + requires: + - build-cxx17 - test-docs - upload-docs: requires: diff --git a/examples/example-rust-vm/Cargo.toml b/examples/example-rust-vm/Cargo.toml new file mode 100644 index 000000000..2c2c6adae --- /dev/null +++ b/examples/example-rust-vm/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "example-rust-vm" +version = "0.1.0" +authors = ["Alex Beregszaszi "] +edition = "2018" + +[lib] +name = "examplerustvm" +crate-type = ["staticlib", "dylib"] + +[dependencies] +evmc-vm = { path = "../../bindings/rust/evmc-vm" } diff --git a/examples/example-rust-vm/src/lib.rs b/examples/example-rust-vm/src/lib.rs new file mode 100644 index 000000000..b6054388d --- /dev/null +++ b/examples/example-rust-vm/src/lib.rs @@ -0,0 +1,52 @@ +extern crate evmc_vm; + +use evmc_vm::evmc_sys as ffi; + +extern "C" fn execute( + instance: *mut ffi::evmc_instance, + context: *mut ffi::evmc_context, + rev: ffi::evmc_revision, + msg: *const ffi::evmc_message, + code: *const u8, + code_size: usize, +) -> ffi::evmc_result { + let result = evmc_vm::ExecutionResult::new( + ffi::evmc_status_code::EVMC_SUCCESS, + 66, + None, + ffi::evmc_address { bytes: [0u8; 20] }, + ); + result.into() +} + +extern "C" fn get_capabilities( + instance: *mut ffi::evmc_instance, +) -> ffi::evmc_capabilities_flagset { + ffi::evmc_capabilities::EVMC_CAPABILITY_EVM1 as u32 +} + +extern "C" fn destroy(instance: *mut ffi::evmc_instance) { + drop(unsafe { Box::from_raw(instance) }) +} + +#[no_mangle] +pub extern "C" fn evmc_create_examplerustvm() -> *const ffi::evmc_instance { + let ret = ffi::evmc_instance { + abi_version: ffi::EVMC_ABI_VERSION as i32, + destroy: Some(destroy), + execute: Some(execute), + get_capabilities: Some(get_capabilities), + set_option: None, + set_tracer: None, + name: { + let c_str = + std::ffi::CString::new("ExampleRustVM").expect("Failed to build EVMC name string"); + c_str.into_raw() as *const i8 + }, + version: { + let c_str = std::ffi::CString::new("1.0").expect("Failed to build EVMC version string"); + c_str.into_raw() as *const i8 + }, + }; + Box::into_raw(Box::new(ret)) +}