wasmtime_environ/
hostcall.rs1use crate::BuiltinFunctionIndex;
2#[cfg(feature = "component-model")]
3use crate::component::ComponentBuiltinFunctionIndex;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub enum HostCall {
13 ArrayCall,
16
17 Builtin(BuiltinFunctionIndex),
20
21 #[cfg(feature = "component-model")]
27 ComponentLowerImport,
28
29 #[cfg(feature = "component-model")]
32 ComponentBuiltin(ComponentBuiltinFunctionIndex),
33}
34
35impl HostCall {
36 pub const fn index(&self) -> u32 {
38 match self {
39 HostCall::ArrayCall => 0,
40 HostCall::Builtin(i) => 1 + i.index(),
41 #[cfg(feature = "component-model")]
42 HostCall::ComponentLowerImport => 1 + BuiltinFunctionIndex::len(),
43 #[cfg(feature = "component-model")]
44 HostCall::ComponentBuiltin(i) => 2 + BuiltinFunctionIndex::len() + i.index(),
45 }
46 }
47
48 pub fn from_index(index: u32) -> Self {
51 let host_call = match index {
52 0 => Self::ArrayCall,
53 _ if index < 1 + BuiltinFunctionIndex::len() => {
54 Self::Builtin(BuiltinFunctionIndex::from_u32(index - 1))
55 }
56 #[cfg(feature = "component-model")]
57 _ if index == 1 + BuiltinFunctionIndex::len() => Self::ComponentLowerImport,
58 #[cfg(feature = "component-model")]
59 _ if index < 2 + BuiltinFunctionIndex::len() + ComponentBuiltinFunctionIndex::len() => {
60 Self::ComponentBuiltin(ComponentBuiltinFunctionIndex::from_u32(
61 index - 2 - BuiltinFunctionIndex::len(),
62 ))
63 }
64 _ => panic!("bad host call index: {index}"),
65 };
66 debug_assert_eq!(index, host_call.index());
67 host_call
68 }
69}
70
71impl From<BuiltinFunctionIndex> for HostCall {
72 fn from(idx: BuiltinFunctionIndex) -> HostCall {
73 HostCall::Builtin(idx)
74 }
75}
76
77#[cfg(feature = "component-model")]
78impl From<ComponentBuiltinFunctionIndex> for HostCall {
79 fn from(idx: ComponentBuiltinFunctionIndex) -> HostCall {
80 HostCall::ComponentBuiltin(idx)
81 }
82}