use crate::{
ExportError, Exportable, Extern,
error::RuntimeError,
store::{AsStoreMut, AsStoreRef, StoreMut, StoreRef},
value::Value,
vm::{VMExtern, VMExternGlobal},
};
use wasmer_types::{GlobalType, Mutability};
pub(crate) mod inner;
pub(crate) use inner::*;
#[derive(Debug, Clone, PartialEq, Eq, derive_more::From)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct Global(pub(crate) BackendGlobal);
impl Global {
pub fn new(store: &mut impl AsStoreMut, val: Value) -> Self {
Self(BackendGlobal::new(store, val))
}
pub fn new_mut(store: &mut impl AsStoreMut, val: Value) -> Self {
Self(BackendGlobal::new_mut(store, val))
}
fn from_value(
store: &mut impl AsStoreMut,
val: Value,
mutability: Mutability,
) -> Result<Self, RuntimeError> {
BackendGlobal::from_value(store, val, mutability).map(Self)
}
pub fn ty(&self, store: &impl AsStoreRef) -> GlobalType {
self.0.ty(store)
}
pub fn get(&self, store: &mut impl AsStoreMut) -> Value {
self.0.get(store)
}
pub fn set(&self, store: &mut impl AsStoreMut, val: Value) -> Result<(), RuntimeError> {
self.0.set(store, val)
}
pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, vm_extern: VMExternGlobal) -> Self {
Self(BackendGlobal::from_vm_extern(store, vm_extern))
}
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
self.0.is_from_store(store)
}
pub(crate) fn to_vm_extern(&self) -> VMExtern {
self.0.to_vm_extern()
}
}
impl<'a> Exportable<'a> for Global {
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
match _extern {
Extern::Global(global) => Ok(global),
_ => Err(ExportError::IncompatibleType),
}
}
}