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
//! Macros to simplify some common WASI-specific tasks.

/// Like the `try!` macro or `?` syntax: returns the value if the computation
/// succeeded or returns the error value.
macro_rules! wasi_try {
    ($expr:expr) => {{
        let res: Result<_, crate::syscalls::types::wasi::Errno> = $expr;
        match res {
            Ok(val) => {
                //tracing::trace!("wasi::wasi_try::val: {:?}", val);
                val
            }
            Err(err) => {
                //tracing::debug!("wasi::wasi_try::err: {:?}", err);
                return err;
            }
        }
    }};
}

/// Like the `try!` macro or `?` syntax: returns the value if the computation
/// succeeded or returns the error value. Results are wrapped in an Ok
macro_rules! wasi_try_ok {
    ($expr:expr) => {{
        let res: Result<_, crate::syscalls::types::wasi::Errno> = $expr;
        match res {
            Ok(val) => {
                //tracing::trace!("wasi::wasi_try_ok::val: {:?}", val);
                val
            }
            Err(err) => {
                //tracing::debug!("wasi::wasi_try_ok::err: {:?}", err);
                return Ok(err);
            }
        }
    }};
}

macro_rules! wasi_try_ok_ok {
    ($expr:expr) => {{
        let res: Result<_, crate::syscalls::types::wasi::Errno> = $expr;
        match res {
            Ok(val) => val,
            Err(err) => {
                return Ok(Err(err));
            }
        }
    }};
}

/// Like `wasi_try` but converts a `MemoryAccessError` to a `wasi::Errno`.
macro_rules! wasi_try_mem {
    ($expr:expr) => {{
        wasi_try!($expr.map_err($crate::mem_error_to_wasi))
    }};
}

/// Like `wasi_try` but converts a `MemoryAccessError` to a `wasi::Errno`.
macro_rules! wasi_try_mem_ok {
    ($expr:expr) => {{
        wasi_try_ok!($expr.map_err($crate::mem_error_to_wasi))
    }};

    ($expr:expr, $thread:expr) => {{
        wasi_try_ok!($expr.map_err($crate::mem_error_to_wasi), $thread)
    }};
}

/// Like `wasi_try` but converts a `MemoryAccessError` to a `wasi::Errno`.
macro_rules! wasi_try_mem_ok_ok {
    ($expr:expr) => {{
        wasi_try_ok_ok!($expr.map_err($crate::mem_error_to_wasi))
    }};

    ($expr:expr, $thread:expr) => {{
        wasi_try_ok_ok!($expr.map_err($crate::mem_error_to_wasi), $thread)
    }};
}

/// Reads a string from Wasm memory.
macro_rules! get_input_str {
    ($memory:expr, $data:expr, $len:expr) => {{
        wasi_try_mem!($data.read_utf8_string($memory, $len))
    }};
}

macro_rules! get_input_str_ok {
    ($memory:expr, $data:expr, $len:expr) => {{
        wasi_try_mem_ok!($data.read_utf8_string($memory, $len))
    }};
}

#[allow(unused_macros)]
macro_rules! get_input_str_bus {
    ($memory:expr, $data:expr, $len:expr) => {{
        wasi_try_mem_bus!($data.read_utf8_string($memory, $len))
    }};
}

#[allow(unused_macros)]
macro_rules! get_input_str_bus_ok {
    ($memory:expr, $data:expr, $len:expr) => {{
        wasi_try_mem_bus_ok!($data.read_utf8_string($memory, $len))
    }};
}