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
pub extern crate holochain_serialized_bytes;
pub use holochain_wasmer_common::allocation;
pub use holochain_wasmer_common::*;
#[macro_export]
macro_rules! memory_externs {
() => {
extern "C" {
fn __import_allocation(guest_allocation_ptr: RemotePtr, host_allocation_ptr: RemotePtr);
fn __import_bytes(host_allocation_ptr: RemotePtr, guest_bytes_ptr: RemotePtr);
}
};
}
memory_externs!();
#[no_mangle]
pub extern "C" fn __deallocate_return_value(return_allocation_ptr: RemotePtr) {
let _: SerializedBytes = AllocationPtr::from_remote_ptr(return_allocation_ptr).into();
}
#[macro_export]
macro_rules! host_externs {
( $( $func_name:ident ),* ) => {
extern "C" {
$( fn $func_name(guest_allocation_ptr: $crate::RemotePtr) -> $crate::RemotePtr; )*
}
};
}
pub fn map_bytes(host_allocation_ptr: RemotePtr) -> AllocationPtr {
let tmp_allocation: allocation::Allocation = [0, 0];
let tmp_allocation_ptr: AllocationPtr = AllocationPtr::from(tmp_allocation);
unsafe {
__import_allocation(tmp_allocation_ptr.as_remote_ptr(), host_allocation_ptr);
};
let [_, len]: allocation::Allocation = allocation::Allocation::from(tmp_allocation_ptr);
let guest_bytes_ptr: Ptr = allocation::allocate(len);
unsafe {
__import_bytes(host_allocation_ptr, guest_bytes_ptr);
};
let guest_allocation: allocation::Allocation = [guest_bytes_ptr, len];
AllocationPtr::from(guest_allocation)
}
#[macro_export]
macro_rules! host_args {
( $ptr:ident ) => {{
use core::convert::TryInto;
match $crate::holochain_serialized_bytes::SerializedBytes::from($crate::map_bytes($ptr))
.try_into()
{
Ok(v) => v,
Err(_) => {
return $crate::AllocationPtr::from(
$crate::holochain_serialized_bytes::SerializedBytes::try_from(
$crate::result::WasmResult::Err(
$crate::result::WasmError::ArgumentDeserializationFailed,
),
)
.unwrap(),
)
.as_remote_ptr();
}
}
}};
}
#[macro_export]
macro_rules! host_call {
( $func_name:ident, $input:expr ) => {{
use std::convert::TryInto;
let maybe_sb: std::result::Result<
$crate::holochain_serialized_bytes::SerializedBytes,
$crate::holochain_serialized_bytes::SerializedBytesError,
> = $input.try_into();
match maybe_sb {
std::result::Result::Ok(sb) => {
let input_allocation_ptr: $crate::AllocationPtr = sb.into();
let result_host_allocation_ptr: $crate::RemotePtr =
unsafe { $func_name(input_allocation_ptr.as_remote_ptr()) };
let _: $crate::holochain_serialized_bytes::SerializedBytes =
input_allocation_ptr.into();
let result_sb: $crate::holochain_serialized_bytes::SerializedBytes =
$crate::map_bytes(result_host_allocation_ptr).into();
result_sb.try_into()
}
std::result::Result::Err(e) => Err(e),
}
}};
}
#[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::AllocationPtr::from(wasm_result_sb).as_remote_ptr();
},
std::result::Result::Err(e) => {
return $crate::AllocationPtr::from($crate::holochain_serialized_bytes::SerializedBytes::try_from(
$crate::WasmResult::Err($crate::WasmError::Zome(String::from("errored while erroring (this should never happen)")))
).unwrap()).as_remote_ptr();
}
};
}};
}
#[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) => return $crate::AllocationPtr::from($crate::holochain_serialized_bytes::SerializedBytes::from(wasm_result_sb)).as_remote_ptr(),
std::result::Result::Err(e) => ret_err!(e),
};
},
std::result::Result::Err(e) => ret_err!(e),
};
}};
}
#[macro_export]
macro_rules! try_result {
( $e:expr, $fail:expr ) => {{
match $e {
Ok(v) => v,
Err(_) => $crate::ret_err!($fail),
}
}};
}