use crate::runtime::vm::component::ComponentInstance;
use crate::store::StoreId;
use core::any::TypeId;
use wasmtime_environ::component::{
AbstractResourceIndex, ComponentTypes, DefinedResourceIndex, ResourceIndex,
};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ResourceType {
kind: ResourceTypeKind,
}
impl ResourceType {
pub fn host<T: 'static>() -> ResourceType {
ResourceType {
kind: ResourceTypeKind::Host(TypeId::of::<T>()),
}
}
pub fn host_dynamic(payload: u32) -> ResourceType {
ResourceType {
kind: ResourceTypeKind::HostDynamic(payload),
}
}
pub(crate) fn guest(
store: StoreId,
instance: &ComponentInstance,
id: DefinedResourceIndex,
) -> ResourceType {
ResourceType {
kind: ResourceTypeKind::Guest {
store,
instance: instance as *const _ as usize,
id,
},
}
}
pub(crate) fn uninstantiated(types: &ComponentTypes, index: ResourceIndex) -> ResourceType {
ResourceType {
kind: ResourceTypeKind::Uninstantiated {
component: types as *const _ as usize,
index,
},
}
}
pub(crate) fn abstract_(types: &ComponentTypes, index: AbstractResourceIndex) -> ResourceType {
ResourceType {
kind: ResourceTypeKind::Abstract {
component: types as *const _ as usize,
index,
},
}
}
pub(crate) fn is_host<T: 'static>(&self) -> bool {
match self.kind {
ResourceTypeKind::Host(id) if id == TypeId::of::<T>() => true,
_ => false,
}
}
pub(crate) fn as_host_dynamic(&self) -> Option<u32> {
match self.kind {
ResourceTypeKind::HostDynamic(payload) => Some(payload),
_ => None,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum ResourceTypeKind {
Host(TypeId),
HostDynamic(u32),
Guest {
store: StoreId,
instance: usize,
id: DefinedResourceIndex,
},
Uninstantiated {
component: usize,
index: ResourceIndex,
},
Abstract {
component: usize,
index: AbstractResourceIndex,
},
}