use derivative::Derivative;
use std::any::Any;
use wasmer_types::RawValue;
use crate::store::InternalStoreHandle;
#[derive(Derivative)]
#[derivative(Debug)]
pub struct VMExternObj {
#[derivative(Debug = "ignore")]
contents: Box<dyn Any + Send + Sync + 'static>,
}
impl VMExternObj {
pub fn new(val: impl Any + Send + Sync + 'static) -> Self {
Self {
contents: Box::new(val),
}
}
#[allow(clippy::should_implement_trait)]
pub fn as_ref(&self) -> &(dyn Any + Send + Sync + 'static) {
&*self.contents
}
}
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct VMExternRef(pub InternalStoreHandle<VMExternObj>);
impl VMExternRef {
pub fn into_raw(self) -> RawValue {
RawValue {
funcref: self.0.index(),
}
}
pub unsafe fn from_raw(raw: RawValue) -> Option<Self> {
InternalStoreHandle::from_index(raw.externref).map(Self)
}
}