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)]
12#[cfg_attr(test, derive(arbitrary::Arbitrary))]
13pub enum HostCall {
14 ArrayCall,
17
18 Builtin(BuiltinFunctionIndex),
21
22 #[cfg(feature = "component-model")]
28 ComponentLowerImport,
29
30 #[cfg(feature = "component-model")]
33 ComponentBuiltin(ComponentBuiltinFunctionIndex),
34}
35
36impl HostCall {
37 pub const fn index(&self) -> u32 {
39 match self {
40 HostCall::ArrayCall => 0,
41 HostCall::Builtin(i) => 1 + i.index(),
42 #[cfg(feature = "component-model")]
43 HostCall::ComponentLowerImport => 1 + BuiltinFunctionIndex::len(),
44 #[cfg(feature = "component-model")]
45 HostCall::ComponentBuiltin(i) => 2 + BuiltinFunctionIndex::len() + i.index(),
46 }
47 }
48
49 pub fn from_index(index: u32) -> Self {
52 let host_call = match index {
53 0 => Self::ArrayCall,
54 _ if index < 1 + BuiltinFunctionIndex::len() => {
55 Self::Builtin(BuiltinFunctionIndex::from_u32(index - 1))
56 }
57 #[cfg(feature = "component-model")]
58 _ if index == 1 + BuiltinFunctionIndex::len() => Self::ComponentLowerImport,
59 #[cfg(feature = "component-model")]
60 _ if index < 2 + BuiltinFunctionIndex::len() + ComponentBuiltinFunctionIndex::len() => {
61 Self::ComponentBuiltin(ComponentBuiltinFunctionIndex::from_u32(
62 index - 2 - BuiltinFunctionIndex::len(),
63 ))
64 }
65 _ => panic!("bad host call index: {index}"),
66 };
67 debug_assert_eq!(index, host_call.index());
68 host_call
69 }
70}
71
72impl From<BuiltinFunctionIndex> for HostCall {
73 fn from(idx: BuiltinFunctionIndex) -> HostCall {
74 HostCall::Builtin(idx)
75 }
76}
77
78#[cfg(feature = "component-model")]
79impl From<ComponentBuiltinFunctionIndex> for HostCall {
80 fn from(idx: ComponentBuiltinFunctionIndex) -> HostCall {
81 HostCall::ComponentBuiltin(idx)
82 }
83}