Skip to content

Commit 74e4e52

Browse files
committed
issues fixes
1 parent bbc184e commit 74e4e52

File tree

2 files changed

+25
-69
lines changed

2 files changed

+25
-69
lines changed

crates/pvm_bindings/src/callbacks.rs

Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -45,70 +45,28 @@ pub unsafe fn log_message(logger: Option<&PVMLoggerCallback>, level: PVMLogLevel
4545
}
4646
}
4747

48-
/// Callback type for handling external calls with 3 parameters
49-
#[repr(C)]
50-
pub struct ExternalCallCallback3 {
51-
pub cb: unsafe extern "C" fn(arg_1: u32, arg_2: u32, arg_3: u32) -> u32,
52-
pub arg: *mut c_void,
53-
}
54-
55-
impl Copy for ExternalCallCallback3 {}
56-
impl Clone for ExternalCallCallback3 {
57-
fn clone(&self) -> Self {
58-
ExternalCallCallback3 {
59-
cb: self.cb.clone(),
60-
arg: self.arg.clone(),
48+
macro_rules! define_external_call_callback {
49+
($name:ident, $($arg_name:ident: $arg_type:ty),*) => {
50+
/// Callback type for handling external calls
51+
#[repr(C)]
52+
pub struct $name {
53+
pub cb: unsafe extern "C" fn($($arg_name: $arg_type),*) -> u32,
54+
pub arg: *mut c_void,
6155
}
62-
}
63-
}
64-
65-
/// Callback type for handling external calls with no parameters
66-
#[repr(C)]
67-
pub struct ExternalCallCallback0 {
68-
pub cb: unsafe extern "C" fn() -> u32,
69-
pub arg: *mut c_void,
70-
}
71-
72-
impl Copy for ExternalCallCallback0 {}
73-
impl Clone for ExternalCallCallback0 {
74-
fn clone(&self) -> Self {
75-
ExternalCallCallback0 {
76-
cb: self.cb.clone(),
77-
arg: self.arg.clone(),
78-
}
79-
}
80-
}
81-
82-
/// Callback type for handling external calls with 1 parameter
83-
#[repr(C)]
84-
pub struct ExternalCallCallback1 {
85-
pub cb: unsafe extern "C" fn(arg_1: u32) -> u32,
86-
pub arg: *mut c_void,
87-
}
8856

89-
impl Copy for ExternalCallCallback1 {}
90-
impl Clone for ExternalCallCallback1 {
91-
fn clone(&self) -> Self {
92-
ExternalCallCallback1 {
93-
cb: self.cb.clone(),
94-
arg: self.arg.clone(),
57+
impl Copy for $name {}
58+
impl Clone for $name {
59+
fn clone(&self) -> Self {
60+
$name {
61+
cb: self.cb.clone(),
62+
arg: self.arg.clone(),
63+
}
64+
}
9565
}
96-
}
66+
};
9767
}
9868

99-
/// Callback type for handling external calls with 2 parameters
100-
#[repr(C)]
101-
pub struct ExternalCallCallback2 {
102-
pub cb: unsafe extern "C" fn(arg_1: u32, arg_2: u32) -> u32,
103-
pub arg: *mut c_void,
104-
}
105-
106-
impl Copy for ExternalCallCallback2 {}
107-
impl Clone for ExternalCallCallback2 {
108-
fn clone(&self) -> Self {
109-
ExternalCallCallback2 {
110-
cb: self.cb.clone(),
111-
arg: self.arg.clone(),
112-
}
113-
}
114-
}
69+
define_external_call_callback!(ExternalCallCallback0,);
70+
define_external_call_callback!(ExternalCallCallback1, arg_1: u32);
71+
define_external_call_callback!(ExternalCallCallback2, arg_1: u32, arg_2: u32);
72+
define_external_call_callback!(ExternalCallCallback3, arg_1: u32, arg_2: u32, arg_3: u32);

crates/pvm_bindings/src/engine.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use crate::log_message;
2-
use crate::PVMConfig;
3-
use crate::PVMLogLevel;
4-
use crate::PVMLoggerCallback;
2+
use crate::{PVMBackend, PVMConfig, PVMLogLevel, PVMLoggerCallback, PVMSandbox};
53
use polkavm::{BackendKind, Config, Engine, SandboxKind};
64
use std::ptr;
75

@@ -37,17 +35,17 @@ pub unsafe extern "C" fn pvm_engine_new(
3735

3836
// Set the backend
3937
if let Some(backend) = match config.backend {
40-
1 => Some(BackendKind::Compiler),
41-
2 => Some(BackendKind::Interpreter),
38+
PVMBackend::Compiler => Some(BackendKind::Compiler),
39+
PVMBackend::Interpreter => Some(BackendKind::Interpreter),
4240
_ => None,
4341
} {
4442
config_rust.set_backend(Some(backend));
4543
}
4644

4745
// Set the sandbox
4846
if let Some(sandbox) = match config.sandbox {
49-
1 => Some(SandboxKind::Linux),
50-
2 => Some(SandboxKind::Generic),
47+
PVMSandbox::Linux => Some(SandboxKind::Linux),
48+
PVMSandbox::Generic => Some(SandboxKind::Generic),
5149
_ => None,
5250
} {
5351
config_rust.set_sandbox(Some(sandbox));

0 commit comments

Comments
 (0)