wasmtime_environ/
hostcall.rs

1#[cfg(feature = "component-model")]
2use crate::component::ComponentBuiltinFunctionIndex;
3use crate::BuiltinFunctionIndex;
4
5/// Enumeration of all possible ways that wasm may execute code in the
6/// host.
7///
8/// This type is intended to be serialized into a 32-bit index (or smaller) and
9/// is used by Pulley for example to identify how transitions from the guest to
10/// the host are performed.
11pub enum HostCall {
12    /// An "array call" is being done which means that the wasm is calling the
13    /// host using the array calling convention (e.g. `VMArrayCallNative`).
14    ArrayCall,
15
16    /// A builtin function, e.g. for `memory.grow`, is being called. Each
17    /// builtin has its own ABI.
18    Builtin(BuiltinFunctionIndex),
19
20    /// A lowered component function is being called. This is done by a
21    /// trampoline generated by Cranelift and is distinct from the array calling
22    /// convention.
23    ///
24    /// This correspond to `VMLoweringCallee`.
25    #[cfg(feature = "component-model")]
26    ComponentLowerImport,
27
28    /// A builtin function, but specifically for components. For example string
29    /// transcoders.
30    #[cfg(feature = "component-model")]
31    ComponentBuiltin(ComponentBuiltinFunctionIndex),
32}
33
34impl HostCall {
35    /// Returns a 32-bit index for this hostcall.
36    pub const fn index(&self) -> u32 {
37        match self {
38            HostCall::ArrayCall => 0,
39            HostCall::Builtin(i) => 1 + i.index(),
40            #[cfg(feature = "component-model")]
41            HostCall::ComponentLowerImport => 1 + BuiltinFunctionIndex::len(),
42            #[cfg(feature = "component-model")]
43            HostCall::ComponentBuiltin(i) => 2 + BuiltinFunctionIndex::len() + i.index(),
44        }
45    }
46}
47
48impl From<BuiltinFunctionIndex> for HostCall {
49    fn from(idx: BuiltinFunctionIndex) -> HostCall {
50        HostCall::Builtin(idx)
51    }
52}
53
54#[cfg(feature = "component-model")]
55impl From<ComponentBuiltinFunctionIndex> for HostCall {
56    fn from(idx: ComponentBuiltinFunctionIndex) -> HostCall {
57        HostCall::ComponentBuiltin(idx)
58    }
59}