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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
pub mod allocation;
pub extern crate aingle_middleware_bytes;
pub use aingle_wasmer_common::*;
use crate::allocation::consume_bytes;
use crate::allocation::write_bytes;
#[macro_export]
macro_rules! memory_externs {
() => {
extern "C" {
fn __import_data(guest_allocation_ptr: $crate::GuestPtr);
}
};
}
memory_externs!();
#[macro_export]
macro_rules! host_externs {
( $( $func_name:ident ),* ) => {
extern "C" {
$( pub fn $func_name(guest_allocation_ptr: $crate::GuestPtr) -> $crate::Len; )*
}
};
}
pub fn host_args<O>(ptr: GuestPtr) -> Result<O, GuestPtr>
where
O: serde::de::DeserializeOwned + std::fmt::Debug,
{
let bytes = consume_bytes(ptr);
match aingle_middleware_bytes::decode(&bytes) {
Ok(v) => Ok(v),
Err(e) => {
tracing::error!(input_type = std::any::type_name::<O>(), bytes = ?bytes, "{}", e);
Err(return_err_ptr(WasmError::Deserialize(bytes)))
}
}
}
pub fn host_call<I, O>(
f: unsafe extern "C" fn(GuestPtr) -> Len,
input: I,
) -> Result<O, crate::WasmError>
where
I: serde::Serialize + std::fmt::Debug,
O: serde::de::DeserializeOwned + std::fmt::Debug,
{
let input_guest_ptr =
crate::allocation::write_bytes(&aingle_middleware_bytes::encode(&input)?);
let result_len: Len = unsafe { f(input_guest_ptr) };
crate::allocation::__deallocate(input_guest_ptr);
let output_guest_ptr: GuestPtr = crate::allocation::__allocate(result_len);
unsafe { __import_data(output_guest_ptr) };
let bytes: Vec<u8> = crate::allocation::consume_bytes(output_guest_ptr);
match aingle_middleware_bytes::decode::<Vec<u8>, Result<O, WasmError>>(&bytes) {
Ok(output) => Ok(output?),
Err(e) => {
tracing::error!(output_type = std::any::type_name::<O>(), bytes = ?bytes, "{}", e);
Err(WasmError::Deserialize(bytes))
}
}
}
pub fn return_ptr<R>(return_value: R) -> GuestPtr
where
R: Serialize + std::fmt::Debug,
{
match aingle_middleware_bytes::encode::<Result<R, WasmError>>(&Ok(return_value)) {
Ok(bytes) => write_bytes(&bytes),
Err(e) => return_err_ptr(WasmError::Serialize(e)),
}
}
pub fn return_err_ptr(wasm_error: WasmError) -> GuestPtr {
match aingle_middleware_bytes::encode::<Result<(), WasmError>>(&Err(wasm_error)) {
Ok(bytes) => write_bytes(&bytes),
Err(e) => match aingle_middleware_bytes::encode::<Result<(), WasmError>>(&Err(
WasmError::Serialize(e),
)) {
Ok(bytes) => write_bytes(&bytes),
Err(_) => match aingle_middleware_bytes::encode::<Result<(), WasmError>>(&Err(
WasmError::ErrorWhileError,
)) {
Ok(bytes) => write_bytes(&bytes),
Err(_) => unreachable!(),
},
},
}
}
#[macro_export]
macro_rules! try_ptr {
( $e:expr, $fail:expr ) => {{
match $e {
Ok(v) => v,
Err(e) => return return_err_ptr(WasmError::Guest(format!("{}: {:?}", $fail, e))),
}
}};
}