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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use crate::{
    ffi, instance::function::InnerFunc, r#async::fiber::AsyncCx, CallingFrame, FuncType,
    WasmEdgeResult, WasmValue,
};
use parking_lot::Mutex;
use std::{pin::Pin, sync::Arc};
use wasmedge_types::{
    error::{FuncError, HostFuncError, WasmEdgeError},
    NeverType,
};

/// Defines a host function.
///
/// A WasmEdge [Function] defines a WebAssembly host function described by its [type](crate::FuncType). A host function is a closure of the original function defined in either the host or the WebAssembly module.
#[derive(Debug)]
pub(crate) struct WasiFunction {
    pub(crate) inner: Arc<Mutex<InnerFunc>>,
    pub(crate) registered: bool,
}
impl WasiFunction {
    pub(crate) fn create_wasi_func<T>(
        ty: &FuncType,
        real_fn: HostFn<T>,
        ctx_data: Option<&mut T>,
        cost: u64,
    ) -> WasmEdgeResult<Self> {
        let data = match ctx_data {
            Some(d) => d as *mut T as *mut std::os::raw::c_void,
            None => std::ptr::null_mut(),
        };

        let ctx = unsafe {
            ffi::WasmEdge_FunctionInstanceCreateBinding(
                ty.inner.0,
                Some(wrap_sync_wasi_fn::<T>),
                real_fn as *mut _,
                data,
                cost,
            )
        };

        match ctx.is_null() {
            true => Err(Box::new(WasmEdgeError::Func(FuncError::Create))),
            false => Ok(Self {
                inner: Arc::new(Mutex::new(InnerFunc(ctx))),
                registered: false,
            }),
        }
    }

    pub(crate) fn create_async_wasi_func<T>(
        ty: &FuncType,
        real_fn: AsyncHostFn<T>,
        ctx_data: Option<&mut T>,
        cost: u64,
    ) -> WasmEdgeResult<Self> {
        let data = match ctx_data {
            Some(d) => d as *mut T as *mut std::os::raw::c_void,
            None => std::ptr::null_mut(),
        };

        let ctx = unsafe {
            ffi::WasmEdge_FunctionInstanceCreateBinding(
                ty.inner.0,
                Some(wrap_async_wasi_fn::<T>),
                real_fn as *mut _,
                data,
                cost,
            )
        };

        match ctx.is_null() {
            true => Err(Box::new(WasmEdgeError::Func(FuncError::Create))),
            false => Ok(Self {
                inner: Arc::new(Mutex::new(InnerFunc(ctx))),
                registered: false,
            }),
        }
    }
}
impl Clone for WasiFunction {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            registered: self.registered,
        }
    }
}

/// Defines the signature of an asynchronous host function.
pub(crate) type AsyncHostFn<T> =
    fn(
        CallingFrame,
        Vec<WasmValue>,
        Option<&'static mut T>,
    ) -> Box<dyn std::future::Future<Output = Result<Vec<WasmValue>, HostFuncError>> + Send>;

/// Defines the signature of a host function.
pub(crate) type HostFn<T> = fn(
    CallingFrame,
    Vec<WasmValue>,
    Option<&'static mut T>,
) -> Result<Vec<WasmValue>, HostFuncError>;

extern "C" fn wrap_sync_wasi_fn<T: 'static>(
    key_ptr: *mut std::ffi::c_void,
    data: *mut std::ffi::c_void,
    call_frame_ctx: *const ffi::WasmEdge_CallingFrameContext,
    params: *const ffi::WasmEdge_Value,
    param_len: u32,
    returns: *mut ffi::WasmEdge_Value,
    return_len: u32,
) -> ffi::WasmEdge_Result {
    let frame = CallingFrame::create(call_frame_ctx);

    // recover the async host function
    let real_func: HostFn<T> = unsafe { std::mem::transmute(key_ptr) };

    // recover the context data
    let data = if std::any::TypeId::of::<T>() == std::any::TypeId::of::<NeverType>() {
        None
    } else {
        let data: &'static mut T = unsafe { &mut *(data as *mut T) };
        Some(data)
    };

    // input arguments
    let input = {
        let raw_input = unsafe {
            std::slice::from_raw_parts(
                params,
                param_len
                    .try_into()
                    .expect("len of params should not greater than usize"),
            )
        };
        raw_input.iter().map(|r| (*r).into()).collect::<Vec<_>>()
    };

    // returns
    let return_len = return_len
        .try_into()
        .expect("len of returns should not greater than usize");
    let raw_returns = unsafe { std::slice::from_raw_parts_mut(returns, return_len) };

    match real_func(frame, input, data) {
        Ok(returns) => {
            assert!(returns.len() == return_len, "[wasmedge-sys] check the number of returns of host function. Expected: {}, actual: {}", return_len, returns.len());
            for (idx, wasm_value) in returns.into_iter().enumerate() {
                raw_returns[idx] = wasm_value.as_raw();
            }
            ffi::WasmEdge_Result { Code: 0 }
        }
        Err(err) => match err {
            HostFuncError::User(code) => unsafe {
                ffi::WasmEdge_ResultGen(ffi::WasmEdge_ErrCategory_UserLevelError, code)
            },
            HostFuncError::Runtime(code) => unsafe {
                ffi::WasmEdge_ResultGen(ffi::WasmEdge_ErrCategory_WASM, code)
            },
        },
    }
}

extern "C" fn wrap_async_wasi_fn<T: 'static>(
    key_ptr: *mut std::ffi::c_void,
    data: *mut std::ffi::c_void,
    call_frame_ctx: *const ffi::WasmEdge_CallingFrameContext,
    params: *const ffi::WasmEdge_Value,
    param_len: u32,
    returns: *mut ffi::WasmEdge_Value,
    return_len: u32,
) -> ffi::WasmEdge_Result {
    let frame = CallingFrame::create(call_frame_ctx);

    // recover the async host function
    let real_func: AsyncHostFn<T> = unsafe { std::mem::transmute(key_ptr) };

    // recover the context data
    let data = if std::any::TypeId::of::<T>() == std::any::TypeId::of::<NeverType>() {
        None
    } else {
        let data: &'static mut T = unsafe { &mut *(data as *mut T) };
        Some(data)
    };

    // arguments
    let input = {
        let raw_input = unsafe {
            std::slice::from_raw_parts(
                params,
                param_len
                    .try_into()
                    .expect("len of params should not greater than usize"),
            )
        };
        raw_input.iter().map(|r| (*r).into()).collect::<Vec<_>>()
    };

    // returns
    let return_len = return_len
        .try_into()
        .expect("len of returns should not greater than usize");
    let raw_returns = unsafe { std::slice::from_raw_parts_mut(returns, return_len) };

    let async_cx = AsyncCx::new();
    let mut future = Pin::from(real_func(frame, input, data));
    let result = match unsafe { async_cx.block_on(future.as_mut()) } {
        Ok(Ok(ret)) => Ok(ret),
        Ok(Err(err)) => Err(err),
        Err(_err) => Err(HostFuncError::Runtime(0x07)),
    };

    // parse result
    match result {
        Ok(returns) => {
            assert!(returns.len() == return_len, "[wasmedge-sys] check the number of returns of async host function. Expected: {}, actual: {}", return_len, returns.len());
            for (idx, wasm_value) in returns.into_iter().enumerate() {
                raw_returns[idx] = wasm_value.as_raw();
            }
            ffi::WasmEdge_Result { Code: 0 }
        }
        Err(err) => match err {
            HostFuncError::User(code) => unsafe {
                ffi::WasmEdge_ResultGen(ffi::WasmEdge_ErrCategory_UserLevelError, code)
            },
            HostFuncError::Runtime(code) => unsafe {
                ffi::WasmEdge_ResultGen(ffi::WasmEdge_ErrCategory_WASM, code)
            },
        },
    }
}