use crate::BuiltinFunctionIndex;
#[cfg(feature = "component-model")]
use crate::component::ComponentBuiltinFunctionIndex;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
pub enum HostCall {
ArrayCall,
Builtin(BuiltinFunctionIndex),
#[cfg(feature = "component-model")]
ComponentLowerImport,
#[cfg(feature = "component-model")]
ComponentBuiltin(ComponentBuiltinFunctionIndex),
}
impl HostCall {
pub const fn index(&self) -> u32 {
match self {
HostCall::ArrayCall => 0,
HostCall::Builtin(i) => 1 + i.index(),
#[cfg(feature = "component-model")]
HostCall::ComponentLowerImport => 1 + BuiltinFunctionIndex::len(),
#[cfg(feature = "component-model")]
HostCall::ComponentBuiltin(i) => 2 + BuiltinFunctionIndex::len() + i.index(),
}
}
pub fn from_index(index: u32) -> Self {
let host_call = match index {
0 => Self::ArrayCall,
_ if index < 1 + BuiltinFunctionIndex::len() => {
Self::Builtin(BuiltinFunctionIndex::from_u32(index - 1))
}
#[cfg(feature = "component-model")]
_ if index == 1 + BuiltinFunctionIndex::len() => Self::ComponentLowerImport,
#[cfg(feature = "component-model")]
_ if index < 2 + BuiltinFunctionIndex::len() + ComponentBuiltinFunctionIndex::len() => {
Self::ComponentBuiltin(ComponentBuiltinFunctionIndex::from_u32(
index - 2 - BuiltinFunctionIndex::len(),
))
}
_ => panic!("bad host call index: {index}"),
};
debug_assert_eq!(index, host_call.index());
host_call
}
}
impl From<BuiltinFunctionIndex> for HostCall {
fn from(idx: BuiltinFunctionIndex) -> HostCall {
HostCall::Builtin(idx)
}
}
#[cfg(feature = "component-model")]
impl From<ComponentBuiltinFunctionIndex> for HostCall {
fn from(idx: ComponentBuiltinFunctionIndex) -> HostCall {
HostCall::ComponentBuiltin(idx)
}
}