1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use evmc_sys as ffi;
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::str;
extern crate num;
use num::FromPrimitive;
#[link(name = "evmc-loader")]
extern "C" {
fn evmc_load_and_create(
filename: *const c_char,
evmc_loader_error_code: *mut i32,
) -> *mut ffi::evmc_vm;
fn evmc_last_error_msg() -> *const c_char;
}
enum_from_primitive! {
#[derive(Debug)]
pub enum EvmcLoaderErrorCode {
EvmcLoaderSucces = 0,
EvmcLoaderCannotOpen = 1,
EvmcLoaderSymbolNotFound = 2,
EvmcLoaderInvalidArgument = 3,
EvmcLoaderInstanceCreationFailure = 4,
EvmcLoaderAbiVersionMismatch = 5,
EvmcLoaderInvalidOptionName = 6,
EvmcLoaderInvalidOptionValue = 7,
}
}
fn error(err: EvmcLoaderErrorCode) -> Result<EvmcLoaderErrorCode, &'static str> {
match err {
EvmcLoaderErrorCode::EvmcLoaderSucces => Ok(EvmcLoaderErrorCode::EvmcLoaderSucces),
_ => unsafe { Err(CStr::from_ptr(evmc_last_error_msg()).to_str().unwrap()) },
}
}
pub fn load_and_create(
fname: &str,
) -> (*mut ffi::evmc_vm, Result<EvmcLoaderErrorCode, &'static str>) {
let c_str = CString::new(fname).unwrap();
unsafe {
let mut error_code: i32 = 0;
let instance = evmc_load_and_create(c_str.as_ptr() as *const c_char, &mut error_code);
return (
instance,
error(EvmcLoaderErrorCode::from_i32(error_code).unwrap()),
);
}
}