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 mod allocation;

pub extern crate holochain_serialized_bytes;
pub use holochain_wasmer_common::*;

use crate::allocation::consume_bytes;
use crate::allocation::write_bytes;

pub use paste::paste;

#[macro_export]
macro_rules! host_externs {
    ( $( $func_name:ident:$version:literal ),* ) => {
        $crate::paste! {
            #[no_mangle]
            extern "C" {
                $( pub fn [<__hc__ $func_name _ $version>](guest_allocation_ptr: usize, len: usize) -> $crate::DoubleUSize; )*
            }
        }
    };
}

/// Receive arguments from the host.
/// The guest sets the type `O` that the host needs to match.
/// If deserialization fails then a `GuestPtr` to a `WasmError::Deserialize` is returned.
/// The guest should __immediately__ return an `Err` back to the host.
/// The `WasmError::Deserialize` enum contains the bytes that failed to deserialize so the host can
/// unambiguously provide debug information.
#[inline(always)]
pub fn host_args<O>(ptr: usize, len: usize) -> Result<O, DoubleUSize>
where
    O: serde::de::DeserializeOwned + std::fmt::Debug,
{
    let bytes = consume_bytes(ptr, len);
    match holochain_serialized_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(wasm_error!(WasmErrorInner::Deserialize(
                bytes
            ))))
        }
    }
}

/// Given an extern that we expect the host to provide:
/// - Serialize the payload by reference
/// - Write the bytes into a new allocation on the guest side
/// - Call the host function and pass it the pointer and length to our leaked serialized data
/// - The host will consume and deallocate the bytes
/// - Deserialize whatever bytes we can import from the host after calling the host function
/// - Return a `Result` of the deserialized output type `O`
#[inline(always)]
pub fn host_call<I, O>(
    f: unsafe extern "C" fn(usize, usize) -> DoubleUSize,
    input: I,
) -> Result<O, crate::WasmError>
where
    I: serde::Serialize + std::fmt::Debug,
    O: serde::de::DeserializeOwned + std::fmt::Debug,
{
    // Call the host function and receive the length of the serialized result.
    let input_bytes = holochain_serialized_bytes::encode(&input).map_err(|e| wasm_error!(e))?;
    let input_len: usize = input_bytes.len();
    let input_guest_ptr = crate::allocation::write_bytes(input_bytes);

    let (output_guest_ptr, output_len): (usize, usize) = split_usize(unsafe {
        // This is unsafe because all host function calls in wasm are unsafe.
        // The host will call `__hc__deallocate_1` for us to free the leaked bytes from the input.
        f(input_guest_ptr, input_len)
    })?;

    // Deserialize the host bytes into the output type.
    let bytes = crate::allocation::consume_bytes(output_guest_ptr, output_len);
    match holochain_serialized_bytes::decode::<[u8], Result<O, WasmError>>(&bytes) {
        Ok(output) => Ok(output?),
        Err(e) => {
            tracing::error!(output_type = std::any::type_name::<O>(), ?bytes, "{}", e);
            Err(wasm_error!(WasmErrorInner::Deserialize(bytes)))
        }
    }
}

/// Convert any serializable value into a `GuestPtr` that can be returned to the host.
/// The host is expected to know how to consume and deserialize it.
#[inline(always)]
pub fn return_ptr<R>(return_value: R) -> DoubleUSize
where
    R: Serialize + std::fmt::Debug,
{
    match holochain_serialized_bytes::encode::<Result<R, WasmError>>(&Ok(return_value)) {
        Ok(bytes) => {
            let len: usize = bytes.len();
            match merge_usize(write_bytes(bytes), len) {
                Ok(v) => v,
                Err(e) => return_err_ptr(e),
            }
        }
        Err(e) => return_err_ptr(wasm_error!(WasmErrorInner::Serialize(e))),
    }
}

/// Convert a `WasmError` to a `GuestPtrLen` as best we can. This is not
/// necessarily straightforward as the serialization process can error recursively.
/// In the worst case we can't even serialize an enum variant, in which case we panic.
/// The casts from `usize` to `u32` are safe as long as the guest code is compiled
/// for `wasm32-unknown-unknown` target.
#[inline(always)]
pub fn return_err_ptr(wasm_error: WasmError) -> DoubleUSize {
    let bytes = match holochain_serialized_bytes::encode::<Result<(), WasmError>>(&Err(wasm_error))
    {
        Ok(bytes) => bytes,
        Err(e) => match holochain_serialized_bytes::encode::<Result<(), WasmError>>(&Err(
            wasm_error!(WasmErrorInner::Serialize(e)),
        )) {
            Ok(bytes) => bytes,
            // At this point we've errored while erroring
            Err(_) => match holochain_serialized_bytes::encode::<Result<(), WasmError>>(&Err(
                wasm_error!(WasmErrorInner::ErrorWhileError),
            )) {
                Ok(bytes) => bytes,
                // At this point we failed to serialize a unit variant so IDK ¯\_(ツ)_/¯
                Err(_) => panic!("Failed to error"),
            },
        },
    };
    let len = bytes.len();
    merge_usize(write_bytes(bytes), len).expect("Failed to build return value")
}

/// A simple macro to wrap `return_err_ptr` in an analogy to the native rust `?`.
#[macro_export]
macro_rules! try_ptr {
    ( $e:expr, $fail:expr ) => {{
        match $e {
            Ok(v) => v,
            Err(e) => return return_err_ptr(wasm_error!("{}: {:?}", $fail, e)),
        }
    }};
}

#[cfg(test)]
pub mod tests {
    use super::*;

    #[test]
    fn wasm_error_macro_guest() {
        assert_eq!(
            wasm_error!("foo").error,
            WasmErrorInner::Guest("foo".into()),
        );

        assert_eq!(
            wasm_error!("{} {}", "foo", "bar").error,
            WasmErrorInner::Guest("foo bar".into())
        );

        assert_eq!(
            wasm_error!(WasmErrorInner::Host("foo".into())).error,
            WasmErrorInner::Host("foo".into()),
        );
    }
}