wasmer/entities/function/host/
mod.rs

1mod imp;
2
3use crate::{
4    BackendKind, WasmTypeList,
5    vm::{VMFunctionCallback, VMTrampoline},
6};
7
8/// The `HostFunction` trait represents the set of functions that
9/// can be used as host function. To uphold this statement, it is
10/// necessary for a function to be transformed into a
11/// `VMFunctionCallback`.
12pub trait HostFunction<T, Args, Rets, Kind>
13where
14    Args: WasmTypeList,
15    Rets: WasmTypeList,
16    Kind: HostFunctionKind,
17{
18    /// Get the pointer to the function body for a given runtime.
19    fn function_callback(&self, rt: BackendKind) -> crate::vm::VMFunctionCallback;
20
21    /// Get the pointer to the function body for a given runtime.
22    fn function_callback_sys(&self) -> crate::vm::VMFunctionCallback {
23        unimplemented!()
24    }
25
26    /// Get the pointer to the function call trampoline for a given runtime.
27    fn call_trampoline_address() -> crate::vm::VMTrampoline;
28}
29
30/// Empty trait to specify the kind of `HostFunction`: With or
31/// without an environment.
32///
33/// This trait is never aimed to be used by a user. It is used by
34/// the trait system to automatically generate the appropriate
35/// host functions.
36#[doc(hidden)]
37pub trait HostFunctionKind: private::HostFunctionKindSealed {}
38
39/// An empty struct to help Rust typing to determine
40/// when a `HostFunction` does have an environment.
41pub struct WithEnv;
42
43impl HostFunctionKind for WithEnv {}
44
45/// An empty struct to help Rust typing to determine
46/// when a `HostFunction` does not have an environment.
47pub struct WithoutEnv;
48
49impl HostFunctionKind for WithoutEnv {}
50
51mod private {
52    //! Sealing the HostFunctionKind because it shouldn't be implemented
53    //! by any type outside.
54    //! See:
55    //! <https://rust-lang.github.io/api-guidelines/future-proofing.html#c-sealed>
56    pub trait HostFunctionKindSealed {}
57    impl HostFunctionKindSealed for super::WithEnv {}
58    impl HostFunctionKindSealed for super::WithoutEnv {}
59}