#![allow(missing_docs)]
use std::any::Any;
use std::cell::RefCell;
use std::fmt;
use std::rc::{Rc, Weak};
use wasmtime_runtime::VMExternRef;
#[derive(Clone)]
pub struct ExternRef {
pub(crate) inner: VMExternRef,
pub(crate) store: Weak<crate::runtime::StoreInner>,
}
impl ExternRef {
pub fn new<T>(store: &crate::Store, value: T) -> ExternRef
where
T: 'static + Any,
{
let inner = VMExternRef::new(value);
let store = store.weak();
ExternRef { inner, store }
}
pub fn store(&self) -> Option<crate::runtime::Store> {
crate::runtime::Store::upgrade(&self.store)
}
pub fn data(&self) -> &dyn Any {
&*self.inner
}
pub fn ptr_eq(&self, other: &ExternRef) -> bool {
VMExternRef::eq(&self.inner, &other.inner)
}
pub fn host_info(&self) -> Option<Rc<RefCell<dyn Any>>> {
let store = crate::Store::upgrade(&self.store)?;
store.host_info(self)
}
pub fn set_host_info<T>(&self, info: T) -> Option<Rc<RefCell<dyn Any>>>
where
T: 'static + Any,
{
let store = crate::Store::upgrade(&self.store)?;
store.set_host_info(self, Some(Rc::new(RefCell::new(info))))
}
pub fn remove_host_info(&self) -> Option<Rc<RefCell<dyn Any>>> {
let store = crate::Store::upgrade(&self.store)?;
store.set_host_info(self, None)
}
}
impl fmt::Debug for ExternRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let ExternRef { inner, store: _ } = self;
let store = self.store();
f.debug_struct("ExternRef")
.field("inner", &inner)
.field("store", &store)
.finish()
}
}