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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
pub mod allocation;
pub extern crate holochain_serialized_bytes;
pub use holochain_wasmer_common::*;
#[macro_export]
macro_rules! memory_externs {
() => {
extern "C" {
fn __import_data(guest_allocation_ptr: $crate::GuestPtr);
}
};
}
#[macro_export]
macro_rules! host_externs {
( $( $func_name:ident ),* ) => {
extern "C" {
$( pub fn $func_name(guest_allocation_ptr: $crate::GuestPtr) -> $crate::Len; )*
}
};
}
#[macro_export]
macro_rules! holochain_externs {
() => {
$crate::memory_externs!();
$crate::host_externs!(
__debug,
__hash_entry,
__unreachable,
__verify_signature,
__sign,
__decrypt,
__encrypt,
__zome_info,
__property,
__random_bytes,
__show_env,
__sys_time,
__agent_info,
__capability_claims,
__capability_grants,
__capability_info,
__get,
__get_details,
__get_links,
__get_link_details,
__get_agent_activity,
__query,
__call_remote,
__call,
__create,
__emit_signal,
__remote_signal,
__create_link,
__delete_link,
__update,
__delete,
__schedule
);
};
}
holochain_externs!();
#[macro_export]
macro_rules! host_args {
( $ptr:ident ) => {{
use core::convert::TryInto;
let bytes = match $crate::allocation::consume_bytes($ptr) {
Ok(v) => v,
Err(e) => {
let sb = $crate::holochain_serialized_bytes::SerializedBytes::try_from(
$crate::result::WasmResult::Err($crate::result::WasmError::Memory),
)
.unwrap();
return $crate::allocation::write_bytes(sb.bytes()).unwrap();
}
};
match $crate::holochain_serialized_bytes::SerializedBytes::from(
$crate::holochain_serialized_bytes::UnsafeBytes::from(bytes),
)
.try_into()
{
Ok(v) => v,
Err(e) => {
let sb = $crate::holochain_serialized_bytes::SerializedBytes::try_from(
$crate::result::WasmResult::Err($crate::result::WasmError::SerializedBytes(e)),
)
.unwrap();
return $crate::allocation::write_bytes(sb.bytes()).unwrap();
}
}
}};
}
pub fn host_call<'a, I: 'a, O>(
f: unsafe extern "C" fn(GuestPtr) -> Len,
payload: &'a I,
) -> Result<O, crate::WasmError>
where
SerializedBytes: TryFrom<&'a I, Error = SerializedBytesError>,
O: TryFrom<SerializedBytes, Error = holochain_serialized_bytes::SerializedBytesError>,
{
let sb = SerializedBytes::try_from(payload)?;
let input_guest_ptr = crate::allocation::write_bytes(sb.bytes())?;
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) };
Ok(O::try_from(SerializedBytes::from(UnsafeBytes::from(
crate::allocation::consume_bytes(output_guest_ptr)?,
)))?)
}
#[macro_export]
macro_rules! ret_err {
( $fail:expr ) => {{
use std::convert::TryInto;
let maybe_wasm_result_sb: std::result::Result<$crate::holochain_serialized_bytes::SerializedBytes, $crate::holochain_serialized_bytes::SerializedBytesError> =
$crate::WasmResult::Err($crate::WasmError::Zome(String::from($fail))).try_into();
match maybe_wasm_result_sb {
std::result::Result::Ok(wasm_result_sb) => {
return $crate::allocation::write_bytes(wasm_result_sb.bytes()).unwrap();
},
std::result::Result::Err(e) => {
return $crate::allocation::write_bytes($crate::holochain_serialized_bytes::SerializedBytes::try_from(
$crate::WasmResult::Err(
$crate::WasmError::Zome(
format!(
"errored while erroring (this should never happen): {:?}",
e
)
)
)
).unwrap().bytes()).unwrap();
}
};
}};
}
#[macro_export]
macro_rules! ret {
( $e:expr) => {{
use std::convert::TryInto;
let maybe_sb: std::result::Result<$crate::holochain_serialized_bytes::SerializedBytes, $crate::holochain_serialized_bytes::SerializedBytesError> = ($e).try_into();
match maybe_sb {
Ok(sb) => {
let maybe_wasm_result_sb: std::result::Result<$crate::holochain_serialized_bytes::SerializedBytes, $crate::holochain_serialized_bytes::SerializedBytesError> = $crate::WasmResult::Ok(sb).try_into();
match maybe_wasm_result_sb {
std::result::Result::Ok(wasm_result_sb) => match $crate::allocation::write_bytes($crate::holochain_serialized_bytes::SerializedBytes::from(wasm_result_sb).bytes()) {
Ok(guest_ptr) => return guest_ptr,
Err(e) => $crate::ret_err!(e),
},
std::result::Result::Err(e) => $crate::ret_err!(e),
};
},
std::result::Result::Err(e) => $crate::ret_err!(e),
};
}};
}
#[macro_export]
macro_rules! try_result {
( $e:expr, $fail:expr ) => {{
match $e {
Ok(v) => v,
Err(e) => $crate::ret_err!(format!("{}: {:?}", $fail, e)),
}
}};
}